-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
XFA - Add a parser for XFA files #12879
Merged
+1,049
−10
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* Copyright 2021 Mozilla Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { $buildXFAObject, NamespaceIds } from "./namespaces.js"; | ||
import { $cleanup, $onChild, XFAObject } from "./xfa_object.js"; | ||
import { NamespaceSetUp } from "./setup.js"; | ||
import { UnknownNamespace } from "./unknown.js"; | ||
import { warn } from "../../shared/util.js"; | ||
|
||
class Root extends XFAObject { | ||
constructor() { | ||
super(-1, "root", Object.create(null)); | ||
this.element = null; | ||
} | ||
|
||
[$onChild](child) { | ||
this.element = child; | ||
} | ||
} | ||
|
||
class Empty extends XFAObject { | ||
constructor() { | ||
super(-1, "", Object.create(null)); | ||
} | ||
|
||
[$onChild](_) {} | ||
} | ||
|
||
class Builder { | ||
constructor() { | ||
this._namespaceStack = []; | ||
|
||
// Each prefix has its own stack | ||
this._namespacePrefixes = new Map(); | ||
this._namespaces = new Map(); | ||
this._nextNsId = Math.max( | ||
...Object.values(NamespaceIds).map(({ id }) => id) | ||
); | ||
this._currentNamespace = new UnknownNamespace(++this._nextNsId); | ||
} | ||
|
||
buildRoot() { | ||
return new Root(); | ||
} | ||
|
||
build({ nsPrefix, name, attributes, namespace, prefixes }) { | ||
const hasNamespaceDef = namespace !== null; | ||
if (hasNamespaceDef) { | ||
// Define the current namespace to use. | ||
this._namespaceStack.push(this._currentNamespace); | ||
this._currentNamespace = this._searchNamespace(namespace); | ||
} | ||
|
||
if (prefixes) { | ||
// The xml node may have namespace prefix definitions | ||
this._addNamespacePrefix(prefixes); | ||
} | ||
|
||
const namespaceToUse = this._getNamespaceToUse(nsPrefix); | ||
const node = | ||
(namespaceToUse && namespaceToUse[$buildXFAObject](name, attributes)) || | ||
new Empty(); | ||
|
||
// In case the node has some namespace things, | ||
// we must pop the different stacks. | ||
if (hasNamespaceDef || prefixes) { | ||
node[$cleanup] = { | ||
hasNamespace: hasNamespaceDef, | ||
prefixes, | ||
}; | ||
} | ||
|
||
return node; | ||
} | ||
|
||
_searchNamespace(nsName) { | ||
let ns = this._namespaces.get(nsName); | ||
if (ns) { | ||
return ns; | ||
} | ||
for (const [name, { check }] of Object.entries(NamespaceIds)) { | ||
if (check(nsName)) { | ||
ns = NamespaceSetUp[name]; | ||
if (ns) { | ||
this._namespaces.set(nsName, ns); | ||
return ns; | ||
} | ||
// The namespace is known but not handled. | ||
break; | ||
} | ||
} | ||
|
||
ns = new UnknownNamespace(++this._nextNsId); | ||
this._namespaces.set(nsName, ns); | ||
return ns; | ||
} | ||
|
||
_addNamespacePrefix(prefixes) { | ||
for (const { prefix, value } of prefixes) { | ||
const namespace = this._searchNamespace(value); | ||
let prefixStack = this._namespacePrefixes.get(prefix); | ||
if (!prefixStack) { | ||
prefixStack = []; | ||
this._namespacePrefixes.set(prefix, prefixStack); | ||
} | ||
prefixStack.push(namespace); | ||
} | ||
} | ||
|
||
_getNamespaceToUse(prefix) { | ||
if (!prefix) { | ||
return this._currentNamespace; | ||
} | ||
const prefixStack = this._namespacePrefixes.get(prefix); | ||
if (prefixStack && prefixStack.length > 0) { | ||
return prefixStack[prefixStack.length - 1]; | ||
} | ||
|
||
warn(`Unknown namespace prefix: ${prefix}.`); | ||
return null; | ||
} | ||
|
||
clean(data) { | ||
const { hasNamespace, prefixes } = data; | ||
if (hasNamespace) { | ||
this._currentNamespace = this._namespaceStack.pop(); | ||
} | ||
if (prefixes) { | ||
prefixes.forEach(({ prefix }) => { | ||
this._namespacePrefixes.get(prefix).pop(); | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export { Builder }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
/* Copyright 2021 Mozilla Foundation | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { $buildXFAObject, NamespaceIds } from "./namespaces.js"; | ||
import { | ||
IntegerObject, | ||
OptionObject, | ||
StringObject, | ||
XFAObject, | ||
} from "./xfa_object.js"; | ||
|
||
const CONFIG_NS_ID = NamespaceIds.config.id; | ||
|
||
class Acrobat extends XFAObject { | ||
constructor(attributes) { | ||
super(CONFIG_NS_ID, "acrobat", /* hasChildren = */ true); | ||
this.acrobat7 = null; | ||
this.autoSave = null; | ||
this.common = null; | ||
this.validate = null; | ||
this.validateApprovalSignatures = null; | ||
this.submitUrl = []; | ||
} | ||
} | ||
|
||
class Acrobat7 extends XFAObject { | ||
constructor(attributes) { | ||
super(CONFIG_NS_ID, "acrobat7", /* hasChildren = */ true); | ||
this.dynamicRender = null; | ||
} | ||
} | ||
|
||
class AdobeExtensionLevel extends IntegerObject { | ||
constructor(attributes) { | ||
super(CONFIG_NS_ID, "adobeExtensionLevel", 0, n => n >= 1 && n <= 8); | ||
} | ||
} | ||
|
||
class AutoSave extends OptionObject { | ||
constructor(attributes) { | ||
super(CONFIG_NS_ID, "autoSave", ["disabled", "enabled"]); | ||
} | ||
} | ||
|
||
class Config extends XFAObject { | ||
constructor(attributes) { | ||
super(CONFIG_NS_ID, "config", /* hasChildren = */ true); | ||
this.acrobat = null; | ||
this.present = null; | ||
this.trace = null; | ||
this.agent = []; | ||
} | ||
} | ||
|
||
class DynamicRender extends OptionObject { | ||
constructor(attributes) { | ||
super(CONFIG_NS_ID, "dynamicRender", ["forbidden", "required"]); | ||
} | ||
} | ||
|
||
class Present extends XFAObject { | ||
constructor(attributes) { | ||
super(CONFIG_NS_ID, "present", /* hasChildren = */ true); | ||
this.behaviorOverride = null; | ||
this.cache = null; | ||
this.common = null; | ||
this.copies = null; | ||
this.destination = null; | ||
this.incrementalMerge = null; | ||
this.layout = null; | ||
this.output = null; | ||
this.overprint = null; | ||
this.pagination = null; | ||
this.paginationOverride = null; | ||
this.script = null; | ||
this.validate = null; | ||
this.xdp = null; | ||
this.driver = []; | ||
this.labelPrinter = []; | ||
this.pcl = []; | ||
this.pdf = []; | ||
this.ps = []; | ||
this.submitUrl = []; | ||
this.webClient = []; | ||
this.zpl = []; | ||
} | ||
} | ||
|
||
class Pdf extends XFAObject { | ||
constructor(attributes) { | ||
super(CONFIG_NS_ID, "pdf", /* hasChildren = */ true); | ||
this.name = attributes.name || ""; | ||
this.adobeExtensionLevel = null; | ||
this.batchOutput = null; | ||
this.compression = null; | ||
this.creator = null; | ||
this.encryption = null; | ||
this.fontInfo = null; | ||
this.interactive = null; | ||
this.linearized = null; | ||
this.openAction = null; | ||
this.pdfa = null; | ||
this.producer = null; | ||
this.renderPolicy = null; | ||
this.scriptModel = null; | ||
this.silentPrint = null; | ||
this.submitFormat = null; | ||
this.tagged = null; | ||
this.version = null; | ||
this.viewerPreferences = null; | ||
this.xdc = null; | ||
} | ||
} | ||
|
||
class SubmitUrl extends StringObject { | ||
constructor(attributes) { | ||
super(CONFIG_NS_ID, "submitUrl"); | ||
} | ||
} | ||
|
||
class Validate extends OptionObject { | ||
constructor(attributes) { | ||
super(CONFIG_NS_ID, "validate", [ | ||
"preSubmit", | ||
"prePrint", | ||
"preExecute", | ||
"preSave", | ||
]); | ||
} | ||
} | ||
|
||
class ConfigNamespace { | ||
static [$buildXFAObject](name, attributes) { | ||
if (ConfigNamespace.hasOwnProperty(name)) { | ||
return ConfigNamespace[name](attributes); | ||
} | ||
return undefined; | ||
} | ||
|
||
static acrobat(attrs) { | ||
return new Acrobat(attrs); | ||
} | ||
|
||
static acrobat7(attrs) { | ||
return new Acrobat7(attrs); | ||
} | ||
|
||
static adobeExtensionLevel(attrs) { | ||
return new AdobeExtensionLevel(attrs); | ||
} | ||
|
||
static autoSave(attrs) { | ||
return new AutoSave(attrs); | ||
} | ||
|
||
static config(attrs) { | ||
return new Config(attrs); | ||
} | ||
|
||
static dynamicRender(attrs) { | ||
return new DynamicRender(attrs); | ||
} | ||
|
||
static pdf(attrs) { | ||
return new Pdf(attrs); | ||
} | ||
|
||
static present(attrs) { | ||
return new Present(attrs); | ||
} | ||
|
||
static submitUrl(attrs) { | ||
return new SubmitUrl(attrs); | ||
} | ||
|
||
static validate(attrs) { | ||
return new Validate(attrs); | ||
} | ||
} | ||
|
||
export { ConfigNamespace }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this syntax work? It doesn't look like a valid function name with these special characters in it; is this some kind of way to insert a variable as the function name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$onChild
is defined inxfa_object
like this:const $onChild = Symbol();
And the way to use this symbol as a function name is to use brackets:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol#symbol_wrapper_objects_as_property_keys
The only way to call this function is to have the symbol so there's no access by name (as a string).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, learned something new then :-)
It's not entirely clear yet as to why this is needed exactly (i.e., why access by name causes conflicts here), but that requires a more in-depth look. This syntax was just something I noticed and didn't know; thanks for explaining this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the specs there is something call SOM expressions to select nodes:
http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.364.2157&rep=rep1&type=pdf#page=101&zoom=auto,-207,766
These expressions seem to be used in different places.
And so in order to make easy their interpretation, each part of the expression can be searched as a own property of the object without the need to keep a flag somewhere to check if the property exists by spec or if it is an implementation detail.
And I guess it'll avoid any bad use an attacker could do with specific expressions.
So my idea was to have objects which are the exact reflection of specs in hiding implementations details and thx to that we can enjoy the object model stuff like overriding
$onChildCheck
method when a specific node can accept a node from another namespace.So from my pov, it makes the implementation safer, simpler and reduce memory use (no need to track spec properties...).
And since it's unusual I added this
$
symbols to help to see them.