Skip to content

Commit 1609bd8

Browse files
authored
Merge commit from fork
Harden the XFA layer and restrict scripting updates to known fields
2 parents a03ec7e + 4ea07c2 commit 1609bd8

3 files changed

Lines changed: 261 additions & 6 deletions

File tree

‎src/display/xfa_layer.js‎

Lines changed: 165 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// eslint-disable-next-line max-len
1919
/** @typedef {import("../../web/pdf_link_service.js").PDFLinkService} PDFLinkService */
2020

21+
import { shadow, SVG_NS } from "../shared/util.js";
2122
import { PageViewport } from "./page_viewport.js";
2223
import { XfaText } from "./xfa_text.js";
2324

@@ -31,7 +32,122 @@ import { XfaText } from "./xfa_text.js";
3132
* @property {string} [intent] - (default value is 'display').
3233
*/
3334

35+
const disallowedRichTextStyleRegExp = /url\(|image-set\(/i;
36+
37+
const disallowedEventHandlerAttrRegExp = /^on/i;
38+
3439
class XfaLayer {
40+
static get _allowedHtmlElements() {
41+
return shadow(
42+
this,
43+
"_allowedHtmlElements",
44+
new Set([
45+
"a",
46+
"b",
47+
"br",
48+
"button",
49+
"div",
50+
"i",
51+
"img",
52+
"input",
53+
"label",
54+
"li",
55+
"ol",
56+
"option",
57+
"p",
58+
"select",
59+
"span",
60+
"sub",
61+
"sup",
62+
"textarea",
63+
"ul",
64+
])
65+
);
66+
}
67+
68+
static get _allowedSvgElements() {
69+
return shadow(
70+
this,
71+
"_allowedSvgElements",
72+
new Set(["ellipse", "line", "path", "rect", "svg"])
73+
);
74+
}
75+
76+
// The elements the rich text renderer is allowed to create: this matches the
77+
// (much smaller) set the worker can emit for rich text, see
78+
// `src/core/xfa/xhtml.js`. In particular no SVG and no interactive/form
79+
// elements are allowed here.
80+
static get _allowedRichTextElements() {
81+
return shadow(
82+
this,
83+
"_allowedRichTextElements",
84+
new Set([
85+
"a",
86+
"b",
87+
"br",
88+
"div",
89+
"i",
90+
"li",
91+
"ol",
92+
"p",
93+
"span",
94+
"sub",
95+
"sup",
96+
"ul",
97+
])
98+
);
99+
}
100+
101+
static get _allowedRichTextAttributes() {
102+
return shadow(
103+
this,
104+
"_allowedRichTextAttributes",
105+
new Set(["class", "dir", "style"])
106+
);
107+
}
108+
109+
// The CSS properties the rich text renderer is allowed to set (camel-cased,
110+
// as produced by `mapStyle` in `src/core/xfa/xhtml.js`). This mirrors that
111+
// file's `VALID_STYLES`, so it excludes anything that could load a remote
112+
// resource or escape the layout (e.g. `position`, `zIndex`, `background`).
113+
static get _allowedRichTextStyles() {
114+
return shadow(
115+
this,
116+
"_allowedRichTextStyles",
117+
new Set([
118+
"color",
119+
"font",
120+
"fontFamily",
121+
"fontSize",
122+
"fontStretch",
123+
"fontStyle",
124+
"fontWeight",
125+
"kerningMode",
126+
"letterSpacing",
127+
"lineHeight",
128+
"margin",
129+
"marginBottom",
130+
"marginLeft",
131+
"marginRight",
132+
"marginTop",
133+
"orphans",
134+
"paddingLeft",
135+
"paddingRight",
136+
"breakAfter",
137+
"breakBefore",
138+
"breakInside",
139+
"tabInterval",
140+
"tabStop",
141+
"textAlign",
142+
"textDecoration",
143+
"textIndent",
144+
"transform",
145+
"verticalAlign",
146+
"widows",
147+
])
148+
);
149+
}
150+
35151
static setupStorage(html, id, element, storage, intent) {
36152
const storedData = storage.getValue(id, { value: null });
37153
switch (element.name) {
@@ -117,6 +233,14 @@ class XfaLayer {
117233
continue;
118234
}
119235

236+
if (disallowedEventHandlerAttrRegExp.test(key)) {
237+
continue;
238+
}
239+
240+
if (intent === "richText" && !this._allowedRichTextAttributes.has(key)) {
241+
continue;
242+
}
243+
120244
switch (key) {
121245
case "class":
122246
if (value.length) {
@@ -132,7 +256,19 @@ class XfaLayer {
132256
html.setAttribute("data-element-id", value);
133257
break;
134258
case "style":
135-
Object.assign(html.style, value);
259+
if (intent === "richText") {
260+
const allowedStyles = this._allowedRichTextStyles;
261+
for (const [styleName, styleValue] of Object.entries(value)) {
262+
if (
263+
allowedStyles.has(styleName) &&
264+
!disallowedRichTextStyleRegExp.test(styleValue)
265+
) {
266+
html.style[styleName] = styleValue;
267+
}
268+
}
269+
} else {
270+
Object.assign(html.style, value);
271+
}
136272
break;
137273
case "textContent":
138274
html.textContent = value;
@@ -145,7 +281,7 @@ class XfaLayer {
145281
}
146282

147283
if (isHTMLAnchorElement) {
148-
linkService.addLinkAttributes(
284+
linkService?.addLinkAttributes(
149285
html,
150286
attributes.href,
151287
attributes.newWindow
@@ -158,6 +294,22 @@ class XfaLayer {
158294
}
159295
}
160296

297+
static #createElement(name, xmlns, intent) {
298+
if (intent === "richText") {
299+
return !xmlns && this._allowedRichTextElements.has(name)
300+
? document.createElement(name)
301+
: null;
302+
}
303+
if (xmlns) {
304+
return xmlns === SVG_NS && this._allowedSvgElements.has(name)
305+
? document.createElementNS(SVG_NS, name)
306+
: null;
307+
}
308+
return this._allowedHtmlElements.has(name)
309+
? document.createElement(name)
310+
: null;
311+
}
312+
161313
/**
162314
* Render the XFA layer.
163315
*
@@ -168,7 +320,9 @@ class XfaLayer {
168320
const linkService = parameters.linkService;
169321
const root = parameters.xfaHtml;
170322
const intent = parameters.intent || "display";
171-
const rootHtml = document.createElement(root.name);
323+
const rootHtml =
324+
this.#createElement(root.name, root.attributes?.xmlns, intent) ??
325+
document.createElement("div");
172326
if (root.attributes) {
173327
this.setAttributes({
174328
html: rootHtml,
@@ -230,9 +384,14 @@ class XfaLayer {
230384
continue;
231385
}
232386

233-
const childHtml = child?.attributes?.xmlns
234-
? document.createElementNS(child.attributes.xmlns, name)
235-
: document.createElement(name);
387+
const childHtml = this.#createElement(
388+
name,
389+
child.attributes?.xmlns,
390+
intent
391+
);
392+
if (!childHtml) {
393+
continue;
394+
}
236395

237396
html.append(childHtml);
238397
if (child.attributes) {

‎test/unit/display_utils_spec.js‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -440,5 +440,82 @@ describe("display_utils", function () {
440440
)
441441
);
442442
});
443+
444+
it("should only keep the supported rich text elements", function () {
445+
if (isNodeJS) {
446+
pending("DOM is not supported in Node.js.");
447+
}
448+
const container = document.createElement("div");
449+
const xfaHtml = {
450+
name: "div",
451+
children: [
452+
{ name: "p", value: "kept" },
453+
{
454+
name: "section",
455+
children: [{ name: "span", value: "removed" }],
456+
},
457+
],
458+
};
459+
renderRichText(
460+
{ html: xfaHtml, dir: "ltr", className: "foo" },
461+
container
462+
);
463+
464+
expect(container.querySelector("p")).not.toBeNull();
465+
expect(container.querySelector("section")).toBeNull();
466+
expect(container.querySelector("span")).toBeNull();
467+
expect(container.textContent).toEqual("kept");
468+
});
469+
470+
it("should only keep the supported rich text attributes", function () {
471+
if (isNodeJS) {
472+
pending("DOM is not supported in Node.js.");
473+
}
474+
const container = document.createElement("div");
475+
const xfaHtml = {
476+
name: "div",
477+
children: [
478+
{
479+
name: "p",
480+
attributes: { class: ["bar"], dir: "rtl", title: "unsupported" },
481+
value: "text",
482+
},
483+
],
484+
};
485+
renderRichText(
486+
{ html: xfaHtml, dir: "ltr", className: "foo" },
487+
container
488+
);
489+
const p = container.querySelector("p");
490+
491+
expect(p.getAttribute("class")).toEqual("bar");
492+
expect(p.getAttribute("dir")).toEqual("rtl");
493+
expect(p.hasAttribute("title")).toEqual(false);
494+
});
495+
496+
it("should only apply the supported rich text style properties", function () {
497+
if (isNodeJS) {
498+
pending("DOM is not supported in Node.js.");
499+
}
500+
const container = document.createElement("div");
501+
const xfaHtml = {
502+
name: "div",
503+
children: [
504+
{
505+
name: "span",
506+
attributes: { style: { color: "green", width: "100px" } },
507+
value: "text",
508+
},
509+
],
510+
};
511+
renderRichText(
512+
{ html: xfaHtml, dir: "ltr", className: "foo" },
513+
container
514+
);
515+
const span = container.querySelector("span");
516+
517+
expect(span.style.color).toEqual("green");
518+
expect(span.style.width).toEqual("");
519+
});
443520
});
444521
});

‎web/pdf_scripting_manager.js‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class PDFScriptingManager {
4545

4646
#externalServices = null;
4747

48+
#objectIds = null;
49+
4850
#pdfDocument = null;
4951

5052
#pdfViewer = null;
@@ -104,6 +106,18 @@ class PDFScriptingManager {
104106
if (pdfDocument !== this.#pdfDocument) {
105107
return; // The document was closed while the data resolved.
106108
}
109+
110+
// Collect the ids of every field annotation, so that any sandbox message
111+
// targeting an unknown id can be ignored.
112+
if (objects) {
113+
this.#objectIds = new Set();
114+
for (const fields of Object.values(objects)) {
115+
for (const { id } of fields) {
116+
this.#objectIds.add(id);
117+
}
118+
}
119+
}
120+
107121
try {
108122
this.#scripting = this.#initScripting();
109123
} catch (error) {
@@ -368,6 +382,9 @@ class PDFScriptingManager {
368382

369383
const ids = siblings ? [id, ...siblings] : [id];
370384
for (const elementId of ids) {
385+
if (!this.#objectIds?.has(elementId)) {
386+
continue;
387+
}
371388
const element = document.querySelector(
372389
`[data-element-id="${elementId}"]`
373390
);
@@ -456,6 +473,8 @@ class PDFScriptingManager {
456473
}
457474

458475
async #destroyScripting() {
476+
this.#objectIds = null;
477+
459478
if (!this.#scripting) {
460479
this.#pdfDocument = null;
461480

0 commit comments

Comments
 (0)