Skip to content

Commit

Permalink
Improve the implementation of the PDFDocument.fingerprints-getter
Browse files Browse the repository at this point in the history
 - Add explicit `length` validation of the /ID entries. Given the `EMPTY_FINGERPRINT` constant we're already *implicitly* assuming a particular length.

 - Move the constants into the `fingerprints`-getter, since they're not used anywhere else.

 - Replace the `hexString` helper function with the standard `Uint8Array.prototype.toHex` method; see https://github.com/tc39/proposal-arraybuffer-base64
  • Loading branch information
Snuffleupagus committed Oct 29, 2024
1 parent 3a85479 commit f9fc477
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -862,10 +862,6 @@ const STARTXREF_SIGNATURE = new Uint8Array([
]);
const ENDOBJ_SIGNATURE = new Uint8Array([0x65, 0x6e, 0x64, 0x6f, 0x62, 0x6a]);

const FINGERPRINT_FIRST_BYTES = 1024;
const EMPTY_FINGERPRINT =
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";

function find(stream, signature, limit = 1024, backwards = false) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
assert(limit > 0, 'The "limit" must be a positive integer.');
Expand Down Expand Up @@ -1548,30 +1544,24 @@ class PDFDocument {
}

get fingerprints() {
const FINGERPRINT_FIRST_BYTES = 1024;
const EMPTY_FINGERPRINT = "\x00".repeat(16);

function validate(data) {
return (
typeof data === "string" &&
data.length > 0 &&
data.length === 16 &&
data !== EMPTY_FINGERPRINT
);
}

function hexString(hash) {
const buf = [];
for (const num of hash) {
const hex = num.toString(16);
buf.push(hex.padStart(2, "0"));
}
return buf.join("");
}

const idArray = this.xref.trailer.get("ID");
const id = this.xref.trailer.get("ID");
let hashOriginal, hashModified;
if (Array.isArray(idArray) && validate(idArray[0])) {
hashOriginal = stringToBytes(idArray[0]);
if (Array.isArray(id) && validate(id[0])) {
hashOriginal = stringToBytes(id[0]);

if (idArray[1] !== idArray[0] && validate(idArray[1])) {
hashModified = stringToBytes(idArray[1]);
if (id[1] !== id[0] && validate(id[1])) {
hashModified = stringToBytes(id[1]);
}
} else {
hashOriginal = calculateMD5(
Expand All @@ -1582,8 +1572,8 @@ class PDFDocument {
}

return shadow(this, "fingerprints", [
hexString(hashOriginal),
hashModified ? hexString(hashModified) : null,
hashOriginal.toHex(),
hashModified?.toHex() ?? null,
]);
}

Expand Down

0 comments on commit f9fc477

Please sign in to comment.