Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
- uses: actions/checkout@f548e57e544e1ff5a4c46bf1e1b8685f8e4a348a # v7.0.1
- uses: google/osv-scanner-action/osv-scanner-action@f4cfcc01edc9c8b756a9b873b7a623ca674da51e # v2.5.0
with:
scan-args: --recursive ./
scan-args: --config=osv-scanner.toml --recursive ./

scorecard:
name: OpenSSF Scorecard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,17 @@ public final class PdfCompressor {
private static final byte[][] PDFA_MARKERS = {
"pdfaid:part>".getBytes(StandardCharsets.US_ASCII),
"pdfaid:part=\"".getBytes(StandardCharsets.US_ASCII),
"pdfaid:part='".getBytes(StandardCharsets.US_ASCII),
"pdfaid:conformance>".getBytes(StandardCharsets.US_ASCII),
"pdfaid:conformance=\"".getBytes(StandardCharsets.US_ASCII),
"pdfaid:conformance='".getBytes(StandardCharsets.US_ASCII),
"GTS_PDFA1".getBytes(StandardCharsets.US_ASCII),
};
// PDF/A-1 forbids object streams and cross-reference streams; later parts allow them.
private static final byte[][] PDFA_PART1_MARKERS = {
"pdfaid:part>1<".getBytes(StandardCharsets.US_ASCII),
"pdfaid:part=\"1\"".getBytes(StandardCharsets.US_ASCII),
"pdfaid:part='1'".getBytes(StandardCharsets.US_ASCII),
"GTS_PDFA1".getBytes(StandardCharsets.US_ASCII),
};
// Catalog /Metadata reference. XMP streams are often Flate-compressed, so a
Expand Down Expand Up @@ -112,18 +115,21 @@ public final class PdfCompressor {
// instead of a full pass per marker.
private static final int N_PDFA_PART = 0;
private static final int N_PDFA_PART_ATTR = 1;
private static final int N_PDFA_CONF = 2;
private static final int N_PDFA_CONF_ATTR = 3;
private static final int N_GTS_PDFA1 = 4;
private static final int N_PDFA1_PART = 5;
private static final int N_PDFA1_PART_ATTR = 6;
private static final int N_METADATA = 7;
private static final int N_BYTE_RANGE = 8;
private static final int N_TYPE_SIG = 9;
private static final int N_TYPE_SPACE_SIG = 10;
private static final int N_SUBFILTER = 11;
private static final int N_OBJSTM = 12;
private static final int N_NEEDLES = 13;
private static final int N_PDFA_PART_SQ = 2;
private static final int N_PDFA_CONF = 3;
private static final int N_PDFA_CONF_ATTR = 4;
private static final int N_PDFA_CONF_SQ = 5;
private static final int N_GTS_PDFA1 = 6;
private static final int N_PDFA1_PART = 7;
private static final int N_PDFA1_PART_ATTR = 8;
private static final int N_PDFA1_PART_SQ = 9;
private static final int N_METADATA = 10;
private static final int N_BYTE_RANGE = 11;
private static final int N_TYPE_SIG = 12;
private static final int N_TYPE_SPACE_SIG = 13;
private static final int N_SUBFILTER = 14;
private static final int N_OBJSTM = 15;
private static final int N_NEEDLES = 16;

// The /ID key is matched byte-by-byte and its range is packed into a long, so the
// signed fast path performs no allocation of its own.
Expand All @@ -140,8 +146,11 @@ public final class PdfCompressor {
PDFA_MARKERS[2],
PDFA_MARKERS[3],
PDFA_MARKERS[4],
PDFA_MARKERS[5],
PDFA_MARKERS[6],
PDFA_PART1_MARKERS[0],
PDFA_PART1_MARKERS[1],
PDFA_PART1_MARKERS[2],
METADATA_MARKER,
BYTE_RANGE,
SIG_MARKERS[0],
Expand Down Expand Up @@ -426,8 +435,9 @@ && serializesIdentically(doc, sourceBytes, openStateBytes)) {
// PDF/A-1 forbids object streams and cross-reference streams, so a
// generated-xref result would break the conformance it claims.
byte[] opt = pdfA1
? PdfOptimizer.optimize(resultBytes, PdfOptimizer.DEFAULT, 9,
PdfOptimizer.OBJECT_STREAMS_PRESERVE,
? PdfOptimizer.optimize(resultBytes,
PdfOptimizer.RECOMPRESS_FLATE | PdfOptimizer.COMPRESS_STREAMS, 9,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major · Data Integrity & Integration · Heavy lift

Supported by static analysis

Script executed:

rg -n -i 'XStep|YStep|applyDocumentOptions|clamp' native/bridge/src/jpdfium_qpdf.cpp
sed -n 425,475p jpdfium/src/main/java/stirling/software/jpdfium/doc/PdfCompressor.java

Repository: Stirling-Tools/JPDFium

Length of output: 4477


Keep tiling-pattern steps unchanged in the PDF/A-1 stream pass.

The stream pass calls PdfOptimizer.optimize with RECOMPRESS_FLATE | COMPRESS_STREAMS. Native applyDocumentOptions runs clampPatternSteps before it checks any flag. That clamp replaces a tiling-pattern /XStep or /YStep with an absolute value below 1.0 by 1.0. A pattern with /XStep 0.5 therefore renders with different tile spacing after this pass.

The Java code adopts the output whenever it is smaller (opt.length < resultBytes.length). It does not compare rendering, so the changed pattern is accepted without a check.

This pass must leave tiling-pattern steps unchanged, or reject its output when they change. Keep the overflow clamp for non-finite and out-of-range steps, because the native source documents that protection.

Add a regression. Render a PDF/A-1 page that contains a /XStep 0.5 tiling pattern before and after this pass, and assert that the rendering matches.

PdfOptimizer.OBJECT_STREAMS_DISABLE,
PdfOptimizer.STREAM_DATA_COMPRESS,
PdfOptimizer.DECODE_LEVEL_GENERALIZED)
: PdfOptimizer.optimize(resultBytes,
Expand All @@ -443,21 +453,22 @@ && serializesIdentically(doc, sourceBytes, openStateBytes)) {
resultBytes = opt;
streamsOptimized = true;
actions.add(pdfA1
? "qpdf: recompressed streams (object streams preserved for PDF/A-1)"
? "qpdf: recompressed streams (object streams disabled for PDF/A-1)"
: "qpdf: recompressed streams and generated object streams");
} else {
warnings.add("qpdf stream pass did not reduce size; kept original bytes");
}
} else if (!signed && opts.removeUnusedObjects()) {
// Compact without restructuring object streams. Explicit flags are
// required: the all-DEFAULT form measured worse than the source on
// some inputs (inflating streams instead of recompressing them).
// Compact without restructuring object streams (disable for PDF/A-1).
// Explicit flags are required: the all-DEFAULT form measured worse than the
// source on some inputs (inflating streams instead of recompressing them).
byte[] opt = PdfOptimizer.optimize(resultBytes,
PdfOptimizer.RECOMPRESS_FLATE | PdfOptimizer.COMPRESS_STREAMS
| PdfOptimizer.DEDUPLICATE_IMAGES
| (jbig2 ? PdfOptimizer.JBIG2 : 0)
| (opts.ccittG4() ? PdfOptimizer.CCITT_G4 : 0), 9,
PdfOptimizer.OBJECT_STREAMS_PRESERVE,
pdfA1 ? PdfOptimizer.OBJECT_STREAMS_DISABLE
: PdfOptimizer.OBJECT_STREAMS_PRESERVE,
PdfOptimizer.STREAM_DATA_COMPRESS,
PdfOptimizer.DECODE_LEVEL_GENERALIZED);
if (opt != null && opt.length < resultBytes.length) {
Expand Down Expand Up @@ -646,7 +657,8 @@ private static int pdfaPart(byte[] pdf, Markers m) {
return -1;
}
boolean rawPdfa = m.first[N_PDFA_PART] >= 0 || m.first[N_PDFA_PART_ATTR] >= 0
|| m.first[N_PDFA_CONF] >= 0 || m.first[N_PDFA_CONF_ATTR] >= 0
|| m.first[N_PDFA_PART_SQ] >= 0 || m.first[N_PDFA_CONF] >= 0
|| m.first[N_PDFA_CONF_ATTR] >= 0 || m.first[N_PDFA_CONF_SQ] >= 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · Data Integrity & Integration · Quick win

Resolve the PDF/A part after a conformance-only match.

If compressed catalog XMP claims part 1, an Info string containing pdfaid:conformance='B' now makes rawPdfa true. With no raw part-1 or GTS_PDFA1 marker, this skips metadata decoding and returns 2 instead of the previous 1. Compression then selects object-stream generation and can retain that output when smaller. (raw.githubusercontent.com)

When the raw match does not establish the part, inspect the catalog metadata before selecting the structural policy. Add a regression with this combination.

|| m.first[N_GTS_PDFA1] >= 0;
byte[] xmp = null;
if (!rawPdfa) {
Expand All @@ -656,7 +668,7 @@ private static int pdfaPart(byte[] pdf, Markers m) {
}
}
boolean part1 = m.first[N_PDFA1_PART] >= 0 || m.first[N_PDFA1_PART_ATTR] >= 0
|| m.first[N_GTS_PDFA1] >= 0
|| m.first[N_PDFA1_PART_SQ] >= 0 || m.first[N_GTS_PDFA1] >= 0
|| (xmp != null && containsAny(xmp, PDFA_PART1_MARKERS));
return part1 ? 1 : 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import stirling.software.jpdfium.exception.JPDFiumException;
import stirling.software.jpdfium.panama.AnnotationBindings;
import stirling.software.jpdfium.panama.NativeRuntime;
import stirling.software.jpdfium.panama.AnnotationBindings;
import stirling.software.jpdfium.panama.PageEditBindings;
import stirling.software.jpdfium.panama.QpdfLib;
import stirling.software.jpdfium.util.NativeJsonParser;
Expand Down Expand Up @@ -574,6 +575,167 @@
}
}

/**
* Font base names used by text objects that are actually painted. Text in
* rendering mode 3 (invisible OCR layers) is excluded. Nested form XObjects
* are traversed when the native build supports it.
*/
public static Set<String> visibleFontKeys(PdfDocument doc) {
Set<String> keys = new LinkedHashSet<>();
for (int i = 0; i < doc.pageCount(); i++) {
try (PdfPage page = doc.page(i)) {
MemorySegment raw = page.rawHandleForRead();
int annotations =
(int) AnnotationBindings.FPDFPage_GetAnnotCount.invokeExact(raw);
// Annotation appearance streams are not walked here, so the
// visible set would be incomplete.
if (annotations > 0) return null;
int count = (int) PageEditBindings.FPDFPage_CountObjects.invokeExact(raw);
for (int j = 0; j < count; j++) {
MemorySegment obj =
(MemorySegment) PageEditBindings.FPDFPage_GetObject.invokeExact(raw, j);
collectVisibleFonts(obj, keys, 0);
}
} catch (Throwable t) {
NativeRuntime.rethrowFatal(t);
// A page that cannot be walked tells us nothing; signal an
// incomplete result instead of hiding fonts.
return null;
}
Comment on lines +599 to +604

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major · Functional Correctness · Quick win

Discard incomplete visibility results.

If page 0 contributes font A and enumeration of page 1 fails, this returns {A}. embedMissing then treats page 1's font B as invisible and skips it. Child-scan failures have the same effect because collectVisibleFonts keeps partial results.

Return an explicit incomplete result, or disable suppression for the whole document after any scan failure. Keep suppression enabled only after a complete scan.

}
return keys;
}

/**
* Embedded font base names whose stored program is empty, unreadable, or
* does not start with a recognized font magic. Such a font renders with a
* fallback and should be replaced when an embeddable substitute exists.
*/
public static Set<String> corruptEmbeddedKeys(PdfDocument doc) {
Set<String> keys = new LinkedHashSet<>();
for (int i = 0; i < doc.pageCount(); i++) {
try (PdfPage page = doc.page(i)) {
MemorySegment raw = page.rawHandleForRead();
int count = (int) PageEditBindings.FPDFPage_CountObjects.invokeExact(raw);
for (int j = 0; j < count; j++) {
MemorySegment obj =
(MemorySegment) PageEditBindings.FPDFPage_GetObject.invokeExact(raw, j);
collectCorruptFonts(obj, keys, 0);
}
} catch (Throwable t) {
NativeRuntime.rethrowFatal(t);
// Best effort; leave unknown fonts alone.
}
}
return keys;
}

/**
* Names of fonts whose descriptor points at an embedded program that is not a
* recognizable font program. Uses the qpdf inventory so a program PDFium refuses to expose
* is still detected.
*/
static List<String> corruptEmbeddedKeys(byte[] pdf) {
if (pdf == null || pdf.length == 0 || !QpdfLib.isFontInventorySupported()) return List.of();
String json = QpdfLib.inspectFonts(pdf);
if (json == null || json.length() <= 2) return List.of();
Set<String> names = new LinkedHashSet<>();
for (Map<String, String> field : NativeJsonParser.parseArray(json)) {
if ("true".equals(field.get("program"))
&& "false".equals(field.get("programValid"))) {
String name = field.getOrDefault("baseFont", "");
if (!name.isEmpty()) names.add(name);
}
}
return List.copyOf(names);
}

private static void collectCorruptFonts(MemorySegment obj, Set<String> keys, int depth) {
if (depth > 64 || obj == null || obj.equals(MemorySegment.NULL)) return;
try {
int type = (int) PageEditBindings.FPDFPageObj_GetType.invokeExact(obj);
if (type == 1) {
MemorySegment font =
(MemorySegment) PageEditBindings.FPDFTextObj_GetFont.invokeExact(obj);
String name = getFontBaseName(font);
if (name.isEmpty() || "<unknown>".equals(name)) return;
if ((int) PageEditBindings.FPDFFont_GetIsEmbedded.invokeExact(font) == 1) {
byte[] data = getFontProgram(font);
if (!looksLikeFontProgram(data)) {
keys.add(name);
}
}
} else if (type == 5 && PageEditBindings.formTraversalAvailable()) {
int children =
(int) PageEditBindings.FPDFFormObj_CountObjects.invokeExact(obj);
for (int i = 0; i < children; i++) {
collectCorruptFonts(PageEditBindings.formObject(obj, i), keys, depth + 1);
}
}
} catch (Throwable t) {
NativeRuntime.rethrowFatal(t);
// Best effort.
}
}

private static byte[] getFontProgram(MemorySegment font) {
try {
long needed = getFontDataSize(font);
if (needed <= 0 || needed > 64L * 1024 * 1024) return null;
try (Arena arena = Arena.ofConfined()) {
MemorySegment buf = arena.allocate(needed);
MemorySegment outLen = arena.allocate(ValueLayout.JAVA_LONG);
int ok = (int) PageEditBindings.FPDFFont_GetFontData.invokeExact(
font, buf, needed, outLen);
if (ok == 0) return null;
long len = Math.min(needed, outLen.get(ValueLayout.JAVA_LONG, 0));
if (len <= 0) return null;
return buf.asSlice(0, len).toArray(ValueLayout.JAVA_BYTE);
}
} catch (Throwable t) {
NativeRuntime.rethrowFatal(t);
return null;
}
}

static boolean looksLikeFontProgram(byte[] data) {
if (data == null || data.length < 4) return false;
if (data[0] == 0x00 && data[1] == 0x01 && data[2] == 0x00 && data[3] == 0x00) return true;
if (data[0] == 0x01 && data[1] == 0x00 && data[2] == 0x04 && data[3] == 0x04) return true;
String head = new String(data, 0, Math.min(4, data.length), StandardCharsets.ISO_8859_1);
if ("OTTO".equals(head) || "true".equals(head) || "ttcf".equals(head)) return true;
if (data.length >= 4 && data[0] == '%' && data[1] == '!') return true;
return data.length >= 2 && (data[0] & 0xFF) == 0x80 && data[1] == 0x01;
}

private static void collectVisibleFonts(MemorySegment obj, Set<String> keys, int depth) {
if (depth > 64 || obj == null || obj.equals(MemorySegment.NULL)) return;
try {
int type = (int) PageEditBindings.FPDFPageObj_GetType.invokeExact(obj);
if (type == 1) {
int renderMode =
(int) PageEditBindings.FPDFTextObj_GetTextRenderMode.invokeExact(obj);
if (renderMode != 3) {
MemorySegment font =
(MemorySegment) PageEditBindings.FPDFTextObj_GetFont.invokeExact(obj);
String name = getFontBaseName(font);
if (!name.isEmpty() && !"<unknown>".equals(name)) {
keys.add(PdfFontEmbedder.key(name));
}
}
} else if (type == 5 && PageEditBindings.formTraversalAvailable()) {
int children =
(int) PageEditBindings.FPDFFormObj_CountObjects.invokeExact(obj);
for (int i = 0; i < children; i++) {
MemorySegment child = PageEditBindings.formObject(obj, i);
collectVisibleFonts(child, keys, depth + 1);
}
}
} catch (Throwable _) {
// Best-effort scan; keep whatever was collected.
}
}

private static String getFontBaseName(MemorySegment font) {
try (Arena arena = Arena.ofConfined()) {
// First call: get required buffer size
Expand Down
Loading
Loading