#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
PR-S4: shadow-mode hardening (review follow-ups) (#6523)
ConnorYoh84aca1205559
diff --git a/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygChargeInterceptor.java b/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygChargeInterceptor.javaindex febcbeb783..66aaf5ca7a 100644--- a/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygChargeInterceptor.java+++ b/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygChargeInterceptor.java@@ -20,6 +20,7 @@ import org.springframework.web.method.HandlerMethod;import org.springframework.web.multipart.MultipartFile;import org.springframework.web.multipart.MultipartHttpServletRequest;import org.springframework.web.servlet.AsyncHandlerInterceptor;+import org.springframework.web.servlet.HandlerMapping;import io.micrometer.core.instrument.Counter;import io.micrometer.core.instrument.MeterRegistry;@@ -82,6 +83,17 @@ public class PaygChargeInterceptor implements AsyncHandlerInterceptor {private static final String AUTOMATION_HEADER = "X-Stirling-Automation";+ /**+ * Optional header the Tauri desktop shell sets so saas-side traffic from the embedded client+ * can be classified as {@link JobSource#DESKTOP_APP} instead of {@code WEB}. No anti-spoof —+ * V12 step limits for DESKTOP_APP and WEB are identical, so the worst-case abuse value is zero+ * today. Tighten if/when their limits diverge.+ */+ private static final String DESKTOP_CLIENT_HEADER = "X-Stirling-Client";++ /** Matches {@code processing_job_step.tool_id} column width (VARCHAR(128)). */+ private static final int TOOL_ID_MAX_LENGTH = 128;+private final JobChargeService chargeService;private final JobService jobService;private final UserRepository userRepository;@@ -94,7 +106,12 @@ public class PaygChargeInterceptor implements AsyncHandlerInterceptor {private final Counter callsJoined;private final Counter callsShortCircuit;private final Counter refundsCounter;- private final Timer durationTimer;++ /** preHandle wall-clock per request. Separate from afterCompletion — different populations. */+ private final Timer preHandleTimer;++ /** afterCompletion wall-clock per request. Includes response hashing + step append + refund. */+ private final Timer afterCompletionTimer;public PaygChargeInterceptor(JobChargeService chargeService,@@ -131,9 +148,15 @@ public class PaygChargeInterceptor implements AsyncHandlerInterceptor {Counter.builder("payg.filter.refunds").description("First-step 5xx refunds applied to shadow rows").register(meterRegistry);- this.durationTimer =+ this.preHandleTimer =+ Timer.builder("payg.filter.duration")+ .tag("phase", "preHandle")+ .description("preHandle wall-clock per request")+ .register(meterRegistry);+ this.afterCompletionTimer =Timer.builder("payg.filter.duration")- .description("preHandle + afterCompletion wall-clock per request")+ .tag("phase", "afterCompletion")+ .description("afterCompletion wall-clock per request").register(meterRegistry);}@@ -160,7 +183,7 @@ public class PaygChargeInterceptor implements AsyncHandlerInterceptor {}return true;} finally {- sample.stop(durationTimer);+ sample.stop(preHandleTimer);}}@@ -218,7 +241,7 @@ public class PaygChargeInterceptor implements AsyncHandlerInterceptor {// async-dispatch), and (b) hard-fails any future caller that tries to mutate it.request.setAttribute(ATTR_INPUT_TEMP_FILES, Collections.unmodifiableList(tempFiles));request.setAttribute(ATTR_INPUT_BYTES, totalInputBytes);- request.setAttribute(ATTR_TOOL_ID, request.getRequestURI());+ request.setAttribute(ATTR_TOOL_ID, resolveToolId(request));ChargeContext ctx =new ChargeContext(@@ -274,7 +297,7 @@ public class PaygChargeInterceptor implements AsyncHandlerInterceptor {closeWrapper(request);}} finally {- sample.stop(durationTimer);+ sample.stop(afterCompletionTimer);}}@@ -412,9 +435,42 @@ public class PaygChargeInterceptor implements AsyncHandlerInterceptor {if (automationHeader != null && "true".equalsIgnoreCase(automationHeader.trim())) {return JobSource.PIPELINE;}+ String desktopHeader = request.getHeader(DESKTOP_CLIENT_HEADER);+ if (desktopHeader != null && "desktop".equalsIgnoreCase(desktopHeader.trim())) {+ return JobSource.DESKTOP_APP;+ }if (auth instanceof ApiKeyAuthenticationToken) {return JobSource.API;}return JobSource.WEB;}++ /**+ * Resolves the {@code tool_id} value stored on {@code processing_job_step}. Prefers the route+ * pattern (e.g. {@code /api/v1/security/add-password}) over the raw URI so audit rollups+ * aggregate by endpoint rather than by request — path variables, query strings, and matrix+ * params don't pollute the column. Falls back to the raw URI when the pattern isn't available+ * (non-Spring-MVC dispatches, async re-dispatch edges).+ *+ * <p>Truncates to {@link #TOOL_ID_MAX_LENGTH} to match the column's {@code VARCHAR(128)} width.+ * Logs at WARN + increments {@link #errorsCounter} when truncation actually happens so support+ * notices the {@code tool_id} they expected isn't what we stored.+ */+ private String resolveToolId(HttpServletRequest request) {+ Object pattern = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);+ String value = pattern instanceof String s ? s : request.getRequestURI();+ if (value == null) {+ return "unknown";+ }+ if (value.length() <= TOOL_ID_MAX_LENGTH) {+ return value;+ }+ log.warn(+ "tool_id length {} exceeds column max {}; truncating. value='{}'",+ value.length(),+ TOOL_ID_MAX_LENGTH,+ value);+ errorsCounter.increment();+ return value.substring(0, TOOL_ID_MAX_LENGTH);+ }}diff --git a/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygFilterProperties.java b/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygFilterProperties.javaindex 0df2bf01b2..89a24e3881 100644--- a/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygFilterProperties.java+++ b/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygFilterProperties.java@@ -38,7 +38,13 @@ public class PaygFilterProperties {/** 10 MiB. Tiny responses stay in RAM; large responses spill. */private long inMemoryThresholdBytes = 10L * 1024L * 1024L;- /** Optional ceiling: when set, OUTPUT recording is skipped past this size. */- private Long maxBytes;+ /**+ * Ceiling for OUTPUT recording. Responses larger than this skip the per-PDF hash + ZIP+ * unpack — the bytes still flowed through to the client unmodified, only lineage capture is+ * dropped. Default 500 MiB is generous for the largest realistic Stirling responses (full+ * split-to-ZIP on a 1000-page document) while preventing pathological cases from tying up+ * the interceptor for minutes. Set to {@code null} for "no ceiling at all".+ */+ private Long maxBytes = 500L * 1024L * 1024L;}}diff --git a/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygOutputExtractor.java b/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygOutputExtractor.javaindex 86b5cb6026..e413d6c73d 100644--- a/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygOutputExtractor.java+++ b/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygOutputExtractor.java@@ -79,35 +79,79 @@ public class PaygOutputExtractor {}String mediaType = stripParameters(contentType);- // Stirling-PDF tool endpoints sometimes set Content-Type to- // application/octet-stream (or no header at all) even when the body- // is a real PDF or ZIP — Spring's default StreamingResponseBody path- // doesn't always negotiate content type. When the declared- // Content-Type is missing or generic, sniff magic bytes to recover- // lineage capture.- boolean missingOrGeneric =- mediaType == null || OCTET_STREAM_CONTENT_TYPE.equalsIgnoreCase(mediaType);- boolean isPdf = PDF_CONTENT_TYPE.equalsIgnoreCase(mediaType);- boolean isZip = ZIP_CONTENT_TYPE.equalsIgnoreCase(mediaType);-- if (missingOrGeneric) {- if (isMagic(bodyPath, PDF_MAGIC)) {- isPdf = true;- } else if (isMagic(bodyPath, ZIP_MAGIC)) {- isZip = true;+ if (PDF_CONTENT_TYPE.equalsIgnoreCase(mediaType)) {+ // Wrapper-owned path. Don't claim ownership. Magic-byte check protects against tools+ // that emit application/pdf for non-PDF payloads (mirrors the ZIP-entry path below).+ if (!isPdfMagic(bodyPath)) {+ log.debug(+ "Response advertised application/pdf but content does not start with"+ + " %PDF- magic bytes; skipping OUTPUT recording. body={}",+ bodyPath);+ return List.of();}- }-- if (isPdf) {- // Wrapper-owned path. Don't claim ownership.return List.of(new ExtractedPdf(bodyPath, null));}- if (isZip) {+ if (ZIP_CONTENT_TYPE.equalsIgnoreCase(mediaType)) {return extractZip(bodyPath);}+ // Stirling-PDF tool endpoints sometimes set Content-Type to application/octet-stream (or+ // no header at all) even when the body is a real PDF or ZIP — Spring's default+ // StreamingResponseBody path doesn't always negotiate content type. When the declared+ // Content-Type is missing or generic, sniff magic bytes in a single head-read so we don't+ // open the body twice on the common negative path.+ if (mediaType == null || OCTET_STREAM_CONTENT_TYPE.equalsIgnoreCase(mediaType)) {+ BodyMagic magic = sniffMagic(bodyPath);+ if (magic == BodyMagic.PDF) {+ return List.of(new ExtractedPdf(bodyPath, null));+ }+ if (magic == BodyMagic.ZIP) {+ return extractZip(bodyPath);+ }+ }return List.of();}+ /** Discriminator returned by {@link #sniffMagic(Path)}. */+ private enum BodyMagic {+ PDF,+ ZIP,+ NEITHER+ }++ /**+ * Single-pass magic-byte sniff used by the generic Content-Type branch. Opens {@code path}+ * once, reads enough bytes to compare against both PDF and ZIP magics, returns the first match+ * (or {@link BodyMagic#NEITHER} if neither matched / read failed). Replaces two consecutive+ * {@link #isMagic} calls that would have opened the file twice on the common negative path.+ */+ private BodyMagic sniffMagic(Path path) {+ int needed = Math.max(PDF_MAGIC.length, ZIP_MAGIC.length);+ byte[] head = new byte[needed];+ int read;+ try (InputStream in = Files.newInputStream(path)) {+ read = in.read(head);+ } catch (IOException e) {+ log.debug("Magic-byte sniff failed for {}", path, e);+ return BodyMagic.NEITHER;+ }+ if (read >= PDF_MAGIC.length && startsWith(head, PDF_MAGIC)) {+ return BodyMagic.PDF;+ }+ if (read >= ZIP_MAGIC.length && startsWith(head, ZIP_MAGIC)) {+ return BodyMagic.ZIP;+ }+ return BodyMagic.NEITHER;+ }++ private static boolean startsWith(byte[] buf, byte[] prefix) {+ for (int i = 0; i < prefix.length; i++) {+ if (buf[i] != prefix[i]) {+ return false;+ }+ }+ return true;+ }+private List<ExtractedPdf> extractZip(Path bodyPath) {List<ExtractedPdf> results = new ArrayList<>();try (InputStream rawIn = Files.newInputStream(bodyPath);diff --git a/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygResponseBodyWrapper.java b/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygResponseBodyWrapper.javaindex 2a3c38336c..25450c54f1 100644--- a/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygResponseBodyWrapper.java+++ b/app/saas/src/main/java/stirling/software/saas/payg/filter/PaygResponseBodyWrapper.java@@ -1,5 +1,6 @@package stirling.software.saas.payg.filter;+import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.OutputStream;@@ -274,9 +275,16 @@ public class PaygResponseBodyWrapper extends HttpServletResponseWrapper implemenbytesWritten += len;}+ /** Spill stream buffer size — coalesces Tomcat's per-chunk syscalls into 64 KiB writes. */+ private static final int SPILL_BUFFER_SIZE = 64 * 1024;+private void spillToDisk() throws IOException {spillFile = tempFileManager.createManagedTempFile(".body");- spillStream = Files.newOutputStream(spillFile.getPath());+ // Wrap in BufferedOutputStream — without this every Tomcat chunk (default 8 KiB) was a+ // separate syscall to the temp file, which dominates wall-clock on big spilled responses.+ spillStream =+ new BufferedOutputStream(+ Files.newOutputStream(spillFile.getPath()), SPILL_BUFFER_SIZE);memoryBuffer.writeTo(spillStream);memoryBuffer = null; // help GC of potentially large bufferspilled = true;diff --git a/app/saas/src/test/java/stirling/software/saas/payg/filter/PaygChargeInterceptorTest.java b/app/saas/src/test/java/stirling/software/saas/payg/filter/PaygChargeInterceptorTest.javaindex 318dc72ff4..72af9368ee 100644--- a/app/saas/src/test/java/stirling/software/saas/payg/filter/PaygChargeInterceptorTest.java+++ b/app/saas/src/test/java/stirling/software/saas/payg/filter/PaygChargeInterceptorTest.java@@ -315,6 +315,69 @@ class PaygChargeInterceptorTest {verify(jobService, never()).recordOutput(any(), any());}+ @Test+ void preHandle_desktopClientHeader_setsJobSourceDesktopApp() throws Exception {+ authenticateWithUser(makeUser(7L, 42L));+ UUID jobId = UUID.randomUUID();+ when(chargeService.openProcess(any(), anyList()))+ .thenReturn(new ChargeOutcome(jobId, 1, ChargeOutcome.Disposition.OPENED));+ org.mockito.ArgumentCaptor<stirling.software.saas.payg.charge.ChargeContext> ctxCaptor =+ org.mockito.ArgumentCaptor.forClass(+ stirling.software.saas.payg.charge.ChargeContext.class);++ MockMultipartHttpServletRequest req = newMultipart();+ req.addFile(new MockMultipartFile("file", "x.pdf", "application/pdf", "abc".getBytes()));+ req.addHeader("X-Stirling-Client", "desktop");++ interceptor.preHandle(req, new MockHttpServletResponse(), handlerMethodForFakeController());++ verify(chargeService).openProcess(ctxCaptor.capture(), anyList());+ assertThat(ctxCaptor.getValue().source())+ .isEqualTo(stirling.software.saas.payg.model.JobSource.DESKTOP_APP);+ }++ @Test+ void preHandle_toolId_prefersBestMatchingPattern() throws Exception {+ authenticateWithUser(makeUser(7L, 42L));+ UUID jobId = UUID.randomUUID();+ when(chargeService.openProcess(any(), anyList()))+ .thenReturn(new ChargeOutcome(jobId, 1, ChargeOutcome.Disposition.OPENED));++ MockMultipartHttpServletRequest req = newMultipart();+ // Raw URI contains a path variable; the matched pattern is what audit rollups want.+ req.setRequestURI("/api/v1/security/add-password/extra/segment");+ req.setAttribute(+ org.springframework.web.servlet.HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE,+ "/api/v1/security/add-password");+ req.addFile(new MockMultipartFile("file", "x.pdf", "application/pdf", "abc".getBytes()));++ interceptor.preHandle(req, new MockHttpServletResponse(), handlerMethodForFakeController());++ assertThat(req.getAttribute(PaygChargeInterceptor.ATTR_TOOL_ID))+ .isEqualTo("/api/v1/security/add-password");+ }++ @Test+ void preHandle_toolId_truncatesAndCountsWhenLongerThan128() throws Exception {+ authenticateWithUser(makeUser(7L, 42L));+ UUID jobId = UUID.randomUUID();+ when(chargeService.openProcess(any(), anyList()))+ .thenReturn(new ChargeOutcome(jobId, 1, ChargeOutcome.Disposition.OPENED));++ MockMultipartHttpServletRequest req = newMultipart();+ String oversized = "/api/v1/" + "x".repeat(200);+ req.setRequestURI(oversized);+ // No matching pattern attribute — falls back to URI.+ req.addFile(new MockMultipartFile("file", "x.pdf", "application/pdf", "abc".getBytes()));++ interceptor.preHandle(req, new MockHttpServletResponse(), handlerMethodForFakeController());++ Object stored = req.getAttribute(PaygChargeInterceptor.ATTR_TOOL_ID);+ assertThat(stored).isInstanceOf(String.class);+ assertThat(((String) stored)).hasSize(128);+ assertThat(meterRegistry.counter("payg.filter.errors").count()).isEqualTo(1.0);+ }+@Testvoid preHandle_pipelineHeader_setsJobSourcePipeline() throws Exception {authenticateWithUser(makeUser(7L, 42L));diff --git a/app/saas/src/test/java/stirling/software/saas/payg/filter/PaygOutputExtractorTest.java b/app/saas/src/test/java/stirling/software/saas/payg/filter/PaygOutputExtractorTest.javaindex 688377ad3a..81684b389c 100644--- a/app/saas/src/test/java/stirling/software/saas/payg/filter/PaygOutputExtractorTest.java+++ b/app/saas/src/test/java/stirling/software/saas/payg/filter/PaygOutputExtractorTest.java@@ -43,6 +43,15 @@ class PaygOutputExtractorTest {assertThat(out).hasSize(1);}+ @Test+ void pdfContentType_butBodyMissingPdfMagic_returnsEmpty(@TempDir Path tmp) throws IOException {+ // Tool sets application/pdf but writes a non-PDF payload — we should NOT record it as+ // OUTPUT lineage. Mirrors the magic-byte gate already enforced on the ZIP branch.+ Path body = tmp.resolve("misleading.pdf");+ Files.write(body, "this is not actually a pdf".getBytes(StandardCharsets.UTF_8));+ assertThat(extractor.extract("application/pdf", body)).isEmpty();+ }+@Testvoid zipContentType_extractsOnlyPdfEntriesWithValidMagicBytes(@TempDir Path tmp)throws IOException {diff --git a/testing/compose/payg/saas-seed.sql b/testing/compose/payg/saas-seed.sqlindex 612cbc9d1c..b5a8f74364 100644--- a/testing/compose/payg/saas-seed.sql+++ b/testing/compose/payg/saas-seed.sql@@ -13,14 +13,19 @@-- no row has is_default = TRUE. Explicit timestamps because Hibernate's-- @CreationTimestamp is application-side; direct INSERTs bypass it.-- ---------------------------------------------------------------------------+-- NOTE: free_tier_units_per_cycle is supplied explicitly because the cucumber+-- harness disables Flyway (see docker-compose-saas.yml). Hibernate's DDL+-- emits NOT NULL without a SQL DEFAULT (the JPA field default `= 0L` is+-- JVM-side only), so the column would otherwise reject this INSERT.+-- 500 matches the launch free-tier (PAYG_DESIGN §3.10 revised).INSERT INTO stirling_pdf.pricing_policy (version, effective_from, doc_pages_per_unit, doc_bytes_per_unit,- min_charge_units, file_unit_cap, is_default, notes, created_by,- created_at+ min_charge_units, file_unit_cap, free_tier_units_per_cycle, is_default,+ notes, created_by, created_at)SELECT'v1-cucumber', CURRENT_TIMESTAMP, 25, 5242880,- 1, 1000, TRUE,+ 1, 1000, 500, TRUE,'Cucumber test default policy', 'system',CURRENT_TIMESTAMPWHERE NOT EXISTS (