Skip to content

Commit 4155c48

Browse files
sofianeelhorpeaktwilightHackbrettXXX
authored
Merge commit from fork
* Fix FreeText annotation style string escaping * Remove dist artifacts from FreeText fix PR * Harden FreeText color: add hex validation, fix double #, expand tests - Validate color as hex pattern (3-8 hex chars), fallback to 000000 for non-hex input as defense-in-depth alongside pdfEscape - Strip leading # before concatenation to prevent double ## in output - Add tests: injection rejection, backslash bypass, valid hex colors, double # prevention, non-hex fallback * Update freetext.pdf reference for double # fix The reference file had color:##ff0000 (double #) which was a pre-existing bug. Now that we strip the leading # before concatenation, the output is color:#ff0000 and the reference must match. * Revert "Update freetext.pdf reference for double # fix" This reverts commit b6139558ededb872a663f62898d68f0f2d35bde5. * Revert "Harden FreeText color: add hex validation, fix double #, expand tests" This reverts commit 0b8baf967c5089ec40f0a86c3d59cb47fcc0823e. --------- Co-authored-by: Doruk <peak@peaktwilight.com> Co-authored-by: Lukas Holländer <lukas.hollaender@yworks.com>
1 parent 87a40bb commit 4155c48

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/modules/annotations.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ import { jsPDF } from "../jspdf.js";
191191
getVerticalCoordinateString(anno.bounds.y + anno.bounds.h) +
192192
"] ";
193193
var color = anno.color || "#000000";
194+
var defaultStyle =
195+
"font: Helvetica,sans-serif 12.0pt; text-align:left; color:#" +
196+
color;
194197
line =
195198
"<</Type /Annot /Subtype /" +
196199
"FreeText" +
@@ -199,10 +202,7 @@ import { jsPDF } from "../jspdf.js";
199202
"/Contents (" +
200203
escape(encryptor(anno.contents)) +
201204
")";
202-
line +=
203-
" /DS(font: Helvetica,sans-serif 12.0pt; text-align:left; color:#" +
204-
color +
205-
")";
205+
line += " /DS(" + escape(encryptor(defaultStyle)) + ")";
206206
line += " /Border [0 0 0]";
207207
line += " >>";
208208
this.internal.write(line);

test/specs/annotations.spec.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ describe("Module: Annotations", () => {
5555
});
5656
comparePdf(doc.output(), "freetext.pdf", "annotations");
5757
});
58+
it("should escape free text annotation style strings", () => {
59+
const doc = jsPDF({ floatPrecision: 2 });
60+
doc.createAnnotation({
61+
type: "freetext",
62+
bounds: {
63+
x: 0,
64+
y: 10,
65+
w: 200,
66+
h: 20
67+
},
68+
contents: "This is a freetext annotation",
69+
color: '000000) /AA <</E <</S /JavaScript /JS (alert("x"))>>>> ('
70+
});
71+
72+
const output = doc.output();
73+
const dsMatch = output.match(/\/DS\((.*)\) \/Border \[0 0 0\]/);
74+
75+
expect(dsMatch).not.toBeNull();
76+
expect(dsMatch[1]).toContain(
77+
'color:#000000\\) /AA <</E <</S /JavaScript /JS \\(alert\\("x"\\)\\)>>>> \\('
78+
);
79+
});
5880
it("should draw a link on the text with link after add page", () => {
5981
const doc = new jsPDF({
6082
unit: "px",

0 commit comments

Comments
 (0)