From 7fe73cc60c3064d4f6d8eecca8df87454e47fc4a Mon Sep 17 00:00:00 2001 From: KatyaKomar Date: Wed, 30 Aug 2023 15:33:44 +0300 Subject: [PATCH] Add limit max size of PDF --- README.md | 11 +++- pom.xml | 2 +- server.yml | 1 + .../rest/app/VeraPdfRestApplication.java | 15 ++++- .../rest/app/VeraPdfRestConfiguration.java | 12 +++- .../verapdf/rest/resources/ApiResource.java | 8 +-- .../rest/resources/ValidateResource.java | 64 +++++++++++-------- 7 files changed, 75 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 499efc65..bdd4bcff 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ To run the veraPDF rest image from DockerHub: docker run -d -p 8080:8080 -p 8081:8081 verapdf/rest:latest ``` -Port 8080 serves both the veraPDF web interface and the veraPDF Rest API. Port 8081 serves the DropWizard diagnostics. +Port 8080 serves both the veraPDF web interface and the veraPDF Rest API. Port 8081 serves the DropWizard diagnostics. Building and running locally -------------------- @@ -199,4 +199,11 @@ curl -F "url=file:///home/folder/pdf.pdf" localhost:8080/api/validate/url/1b ``` ### Configuration files -Configuration parameters are located in `/opt/verapdf-rest/config` folder of the container file system. The details on the veraPDF parameters are available at https://docs.verapdf.org/cli/config/. Specific verapdf-rest server configuration parameters are located in server.yml. +Configuration parameters are located in `/opt/verapdf-rest/config` folder of the container file system. The details on the veraPDF parameters are available at https://docs.verapdf.org/cli/config/. +Specific verapdf-rest server configuration parameters are located in server.yml. + +To set the maximum file size of PDF, change maxFileSize in server.yml file or run docker image: +``` +docker run -d -p 8080:8080 -p 8081:8081 -e VERAPDF_MAX_FILE_SIZE=1 verapdf/rest:latest +``` +where VERAPDF_MAX_FILE_SIZE is 1 MB. The default maximum file size is 100 MB. diff --git a/pom.xml b/pom.xml index 7869ac74..eb8ebec3 100644 --- a/pom.xml +++ b/pom.xml @@ -44,7 +44,7 @@ 2.1.7 2.10.0 - [1.24.0,1.25.0) + [1.25.0,1.26.0-RC) diff --git a/server.yml b/server.yml index 02c21de4..026ce155 100644 --- a/server.yml +++ b/server.yml @@ -26,3 +26,4 @@ logging: immediateFlush: true appenders: - type: console +maxFileSize: ${VERAPDF_MAX_FILE_SIZE:-100} diff --git a/src/main/java/org/verapdf/rest/app/VeraPdfRestApplication.java b/src/main/java/org/verapdf/rest/app/VeraPdfRestApplication.java index 95a68f1e..0964aa83 100644 --- a/src/main/java/org/verapdf/rest/app/VeraPdfRestApplication.java +++ b/src/main/java/org/verapdf/rest/app/VeraPdfRestApplication.java @@ -1,5 +1,5 @@ /** - * + * */ package org.verapdf.rest.app; @@ -10,9 +10,12 @@ import io.federecio.dropwizard.swagger.SwaggerBundle; import io.federecio.dropwizard.swagger.SwaggerBundleConfiguration; +import io.dropwizard.configuration.EnvironmentVariableSubstitutor; +import io.dropwizard.configuration.SubstitutingSourceProvider; import org.eclipse.jetty.servlets.CrossOriginFilter; import org.verapdf.rest.resources.ApiResource; import org.verapdf.rest.resources.HomePageResource; +import org.verapdf.rest.resources.ValidateResource; import org.verapdf.rest.resources.ValidationExceptionMapper; import io.dropwizard.Application; @@ -33,7 +36,7 @@ public class VeraPdfRestApplication extends ApplicationCarl Wilson @@ -58,6 +58,15 @@ public class ValidateResource { // java.security.digest name for the SHA-1 algorithm private static final String SHA1_NAME = "SHA-1"; //$NON-NLS-1$ private static final String AUTODETECT_PROFILE = "auto"; + private static int maxFileSize; + private static ValidateResource validateResource; + + public static synchronized ValidateResource getValidateResource() { + if (validateResource == null) { + validateResource = new ValidateResource(); + } + return validateResource; + } private static ConfigManager configManager; @@ -123,7 +132,7 @@ public static InputStream validateXml(@Parameter(description = "the String id of @Parameter(hidden = true) @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) throws VeraPDFException { - return validate(uploadedInputStream, profileId, FormatOption.XML); + return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, FormatOption.XML); } @POST @@ -147,7 +156,7 @@ public static InputStream validateXml(@Parameter(description = "the String id of @FormDataParam("url") String urlLink) throws VeraPDFException { InputStream uploadedInputStream = getInputStreamByUrlLink(urlLink); - return validate(uploadedInputStream, profileId, FormatOption.XML); + return validate(uploadedInputStream, urlLink, profileId, FormatOption.XML); } /** @@ -181,7 +190,7 @@ public static InputStream validateJson(@Parameter(description = "the String id o @Parameter(hidden = true) @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) throws VeraPDFException { - return validate(uploadedInputStream, profileId, FormatOption.JSON); + return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, FormatOption.JSON); } @POST @@ -195,7 +204,7 @@ public static InputStream validateJson(@Parameter(description = "the String id o @FormDataParam("url") String urlLink) throws VeraPDFException { InputStream uploadedInputStream = getInputStreamByUrlLink(urlLink); - return validate(uploadedInputStream, profileId, FormatOption.JSON); + return validate(uploadedInputStream, urlLink, profileId, FormatOption.JSON); } /** @@ -225,7 +234,7 @@ public static InputStream validateHtml(@PathParam("profileId") String profileId, @FormDataParam("file") InputStream uploadedInputStream, @Parameter(hidden = true) @FormDataParam("file") final FormDataContentDisposition contentDispositionHeader) throws VeraPDFException { - return validate(uploadedInputStream, profileId, FormatOption.HTML); + return validate(uploadedInputStream, contentDispositionHeader.getFileName(), profileId, FormatOption.HTML); } @POST @@ -239,7 +248,7 @@ public static InputStream validateHtml(@Parameter(description = "the String id o @FormDataParam("url") String urlLink) throws VeraPDFException { InputStream uploadedInputStream = getInputStreamByUrlLink(urlLink); - return validate(uploadedInputStream, profileId, FormatOption.HTML); + return validate(uploadedInputStream, urlLink, profileId, FormatOption.HTML); } /** @@ -280,29 +289,27 @@ public static ValidationReport validatePut(@Parameter(description = "the String } - private static InputStream validate(InputStream uploadedInputStream, String profileId, FormatOption formatOption) throws VeraPDFException { - File file; + public static void setMaxFileSize(Integer maxFileSize) { + ValidateResource.maxFileSize = maxFileSize; + } + + private static InputStream validate(InputStream uploadedInputStream, String fileName, String profileId, FormatOption formatOption) throws VeraPDFException { + SeekableInputStream seekableInputStream; try { - file = File.createTempFile("cache", ""); - } catch (IOException exception) { - throw new VeraPDFException("IOException creating a temp file", exception); //$NON-NLS-1$ - } - try (OutputStream fos = new FileOutputStream(file);) { - IOUtils.copy(uploadedInputStream, fos); - uploadedInputStream.close(); - } catch (IOException excep) { - throw new VeraPDFException("IOException creating a temp file", excep); //$NON-NLS-1$ + seekableInputStream = SeekableInputStream.getSeekableStream(uploadedInputStream, 1000000 * maxFileSize); + } catch (VeraPDFParserException e) { + throw new VeraPDFException("Maximum allowed file size exceeded: " + maxFileSize + " MB"); + } catch (IOException e) { + throw new VeraPDFException(e.getMessage()); } - PDFAFlavour flavour = PDFAFlavour.byFlavourId(profileId); ValidatorConfig validatorConfig = configManager.getValidatorConfig(); validatorConfig.setFlavour(flavour); ProcessorConfig config = createProcessorConfig(validatorConfig); - byte[] outputBytes; try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { VeraAppConfig appConfig = configManager.getApplicationConfig(); - processFile(file, config, outputStream, appConfig, formatOption); + processStream(seekableInputStream, fileName, config, outputStream, appConfig, formatOption); outputBytes = outputStream.toByteArray(); } catch (IOException excep) { throw new VeraPDFException("Some Java Exception while validating", excep); //$NON-NLS-1$ @@ -370,15 +377,18 @@ private static InputStream getInputStreamByUrlLink(String urlLink) throws VeraPD } } - private static BatchSummary processFile(File file, ProcessorConfig config, OutputStream stream, - VeraAppConfig appConfig, FormatOption formatOption) + private static BatchSummary processStream(SeekableInputStream inputStream, String fileName, ProcessorConfig config, OutputStream stream, + VeraAppConfig appConfig, FormatOption formatOption) throws VeraPDFException, IOException { - List files = Arrays.asList(file); BatchSummary summary; try (BatchProcessor processor = ProcessorFactory.fileBatchProcessor(config)) { - summary = processor.process(files, + summary = processor.process(ItemDetails.fromValues(fileName, inputStream.getStreamLength()), inputStream, ProcessorFactory.getHandler(formatOption, appConfig.isVerbose(), stream, config.getValidatorConfig().isRecordPasses(), appConfig.getWikiPath())); + } finally { + if (inputStream != null) { + inputStream.close(); + } } return summary; }