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 Apr 1, 2024
1 parent 3b87c31 commit 4fd2419
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -816,10 +816,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 @@ -1502,23 +1498,17 @@ 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");
let hashOriginal, hashModified;
if (Array.isArray(idArray) && validate(idArray[0])) {
Expand All @@ -1536,8 +1526,8 @@ class PDFDocument {
}

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

Expand Down

0 comments on commit 4fd2419

Please sign in to comment.