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" ;
2122import { PageViewport } from "./page_viewport.js" ;
2223import { 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 = / u r l \( | i m a g e - s e t \( / i;
36+
37+ const disallowedEventHandlerAttrRegExp = / ^ o n / i;
38+
3439class 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 ) {
0 commit comments