forked from NaturalIntelligence/fast-xml-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c3fb07f
commit ec640f6
Showing
5 changed files
with
147 additions
and
1 deletion.
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
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
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,135 @@ | ||
|
||
Entities are the variables that can be used in XML content to maintain consistency. Eg, | ||
|
||
```xml | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!DOCTYPE note [ | ||
<!ENTITY nbsp " "> | ||
<!ENTITY writer "Writer: Donald Duck."> | ||
<!ENTITY copyright "Copyright: W3Schools."> | ||
]> | ||
|
||
<note> | ||
<to>Tove</to> | ||
<from>Jani</from> | ||
<heading>Reminder</heading> | ||
<body attr="&writer;">Don't forget me this weekend!</body> | ||
<footer>&writer; ©right;</footer> | ||
</note> | ||
``` | ||
|
||
You can define your own entities using DOCTYPE. FXP by default supports following XML entities; | ||
|
||
| Entity name | Character | Decimal reference | Hexadecimal reference | | ||
| :---------- | :-------- | :---------------- | :-------------------- | | ||
| quot | " | " | " | | ||
| amp | & | & | & | | ||
| apos | ' | ' | ' | | ||
| lt | < | < | < | | ||
| gt | > | > | > | | ||
|
||
However, since the entity processing can impact the parser's performance drastically, you can use `processEntities: false` to disable it. | ||
|
||
XML Builder decodes default entities value. Eg | ||
```js | ||
const jsObj = { | ||
"note": { | ||
"@heading": "Reminder > \"Alert", | ||
"body": { | ||
"#text": " 3 < 4", | ||
"attr": "Writer: Donald Duck." | ||
}, | ||
} | ||
}; | ||
|
||
const options = { | ||
attributeNamePrefix: "@", | ||
ignoreAttributes: false, | ||
// processEntities: false | ||
}; | ||
const builder = new XMLBuilder(options); | ||
const output = builder.build(jsObj); | ||
``` | ||
Output: | ||
```xml | ||
<note heading="Reminder > "Alert"> | ||
<body> | ||
3 < 4 | ||
<attr>Writer: Donald Duck.</attr> | ||
</body> | ||
</note> | ||
``` | ||
|
||
## Side effects | ||
|
||
Though FXP doesn't silently ignores entities with `&` in the values, following side efftcts are possible | ||
|
||
```xml | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<!DOCTYPE note [ | ||
<!ENTITY nbsp "writer;"> | ||
<!ENTITY writer "Writer: Donald Duck."> | ||
<!ENTITY copyright "Copyright: W3Schools."> | ||
]> | ||
|
||
<note> | ||
<heading>Reminder</heading> | ||
<body attr="&writer;">Don't forget me this weekend!</body> | ||
<footer>&writer;& ©right;</footer> | ||
</note> | ||
``` | ||
|
||
Output | ||
|
||
```js | ||
{ | ||
"note": { | ||
"heading": "Reminder", | ||
"body": { | ||
"#text": "Don't forget me this weekend!", | ||
"attr": "Writer: Donald Duck." | ||
}, | ||
"footer": "Writer: Donald Duck.Writer: Donald Duck.Copyright: W3Schools." | ||
} | ||
} | ||
``` | ||
|
||
To deal with such situation, use `&` instead of `&` in XML document. | ||
|
||
## Attacks | ||
|
||
Following attacks are possible due to entity processing | ||
|
||
* Denial-of-Service Attacks | ||
* Classic XXE | ||
* Advanced XXE | ||
* Server-Side Requst Forgery (SSRF) | ||
* XInclude | ||
* XSLT | ||
|
||
Since FXP doesn't allow entities with `&` in the values, above attacks should not work. | ||
|
||
## HTML Entities | ||
|
||
Following HTML entities are supported by the parser by default when `htmlEntities: true`. | ||
|
||
| Result | Description | Entity Name | Entity Number | | ||
| :----- | :--------------------------------- | :---------- | :------------ | | ||
| | non-breaking space | |   | | ||
| < | less than | < | < | | ||
| > | greater than | > | > | | ||
| & | ampersand | & | & | | ||
| " | double quotation mark | " | " | | ||
| ' | single quotation mark (apostrophe) | ' | ' | | ||
| ¢ | cent | ¢ | ¢ | | ||
| £ | pound | £ | £ | | ||
| ¥ | yen | ¥ | ¥ | | ||
| € | euro | € | € | | ||
| © | copyright | © | © | | ||
| ® | registered trademark | ® | ® | | ||
| ₹ | Indian Rupee | &inr; | ₹ | | ||
--- | ||
|
||
In future version of FXP, we'll be supporting more features of DOCTYPE such as `ELEMENT`, reading content for an entity from a file etc. |