@xmldom/xmldom 0.8.10 → 0.8.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,26 @@ All notable changes to this project will be documented in this file.
4
4
 
5
5
  This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.8.12](https://github.com/xmldom/xmldom/compare/0.8.11...0.8.12)
8
+
9
+ ### Fixed
10
+
11
+ - Security: `createCDATASection` now throws `InvalidCharacterError` when `data` contains `"]]>"`, as required by the [WHATWG DOM spec](https://dom.spec.whatwg.org/#dom-document-createcdatasection). [`GHSA-wh4c-j3r5-mjhp`](https://github.com/xmldom/xmldom/security/advisories/GHSA-wh4c-j3r5-mjhp)
12
+ - Security: `XMLSerializer` now splits CDATASection nodes whose data contains `"]]>"` into adjacent CDATA sections at serialization time, preventing XML injection via mutation methods (`appendData`, `replaceData`, `.data =`, `.textContent =`). [`GHSA-wh4c-j3r5-mjhp`](https://github.com/xmldom/xmldom/security/advisories/GHSA-wh4c-j3r5-mjhp)
13
+
14
+ Code that passes a string containing `"]]>"` to `createCDATASection` and relied on the previously unsafe behavior will now receive `InvalidCharacterError`. Use a mutation method such as `appendData` if you intentionally need `"]]>"` in a CDATASection node's data.
15
+
16
+ Thank you, [@thesmartshadow](https://github.com/thesmartshadow), for your contributions
17
+
18
+ ## [0.8.11](https://github.com/xmldom/xmldom/compare/0.8.10...0.8.11)
19
+
20
+ ### Fixed
21
+
22
+ - update `ownerDocument` when moving nodes between documents [`#933`](https://github.com/xmldom/xmldom/pull/933) / [`#932`](https://github.com/xmldom/xmldom/issues/932)
23
+
24
+ Thank you, [@shunkica](https://github.com/shunkica), for your contributions
25
+
26
+
7
27
  ## [0.8.10](https://github.com/xmldom/xmldom/compare/0.8.9...0.8.10)
8
28
 
9
29
  ### Fixed
package/index.d.ts CHANGED
@@ -23,6 +23,16 @@ declare module "@xmldom/xmldom" {
23
23
  }
24
24
 
25
25
  interface XMLSerializer {
26
+ /**
27
+ * Returns the result of serializing `node` to XML.
28
+ *
29
+ * __This implementation differs from the specification:__
30
+ * - CDATASection nodes whose data contains `]]>` are serialized by splitting the section
31
+ * at each `]]>` occurrence (following W3C DOM Level 3 Core `split-cdata-sections`
32
+ * default behaviour). A configurable option is not yet implemented.
33
+ *
34
+ * @see https://html.spec.whatwg.org/#dom-xmlserializer-serializetostring
35
+ */
26
36
  serializeToString(node: Node): string;
27
37
  }
28
38
 
package/lib/dom.js CHANGED
@@ -980,6 +980,9 @@ function _insertBefore(parent, node, child, _inDocumentAssertion) {
980
980
  }
981
981
  do{
982
982
  newFirst.parentNode = parent;
983
+ // Update ownerDocument for each node being inserted
984
+ var targetDoc = parent.ownerDocument || parent;
985
+ _updateOwnerDocument(newFirst, targetDoc);
983
986
  }while(newFirst !== newLast && (newFirst= newFirst.nextSibling))
984
987
  _onUpdateChild(parent.ownerDocument||parent, parent);
985
988
  //console.log(parent.lastChild.nextSibling == null)
@@ -989,6 +992,37 @@ function _insertBefore(parent, node, child, _inDocumentAssertion) {
989
992
  return node;
990
993
  }
991
994
 
995
+ /**
996
+ * Recursively updates the ownerDocument property for a node and all its descendants
997
+ * @param {Node} node
998
+ * @param {Document} newOwnerDocument
999
+ * @private
1000
+ */
1001
+ function _updateOwnerDocument(node, newOwnerDocument) {
1002
+ if (node.ownerDocument === newOwnerDocument) {
1003
+ return;
1004
+ }
1005
+
1006
+ node.ownerDocument = newOwnerDocument;
1007
+
1008
+ // Update attributes if this is an element
1009
+ if (node.nodeType === ELEMENT_NODE && node.attributes) {
1010
+ for (var i = 0; i < node.attributes.length; i++) {
1011
+ var attr = node.attributes.item(i);
1012
+ if (attr) {
1013
+ attr.ownerDocument = newOwnerDocument;
1014
+ }
1015
+ }
1016
+ }
1017
+
1018
+ // Recursively update child nodes
1019
+ var child = node.firstChild;
1020
+ while (child) {
1021
+ _updateOwnerDocument(child, newOwnerDocument);
1022
+ child = child.nextSibling;
1023
+ }
1024
+ }
1025
+
992
1026
  /**
993
1027
  * Appends `newChild` to `parentNode`.
994
1028
  * If `newChild` is already connected to a `parentNode` it is first removed from it.
@@ -1014,6 +1048,11 @@ function _appendSingleChild (parentNode, newChild) {
1014
1048
  }
1015
1049
  parentNode.lastChild = newChild;
1016
1050
  _onUpdateChild(parentNode.ownerDocument, parentNode, newChild);
1051
+
1052
+ // Update ownerDocument for the new child and all its descendants
1053
+ var targetDoc = parentNode.ownerDocument || parentNode;
1054
+ _updateOwnerDocument(newChild, targetDoc);
1055
+
1017
1056
  return newChild;
1018
1057
  }
1019
1058
 
@@ -1042,7 +1081,7 @@ Document.prototype = {
1042
1081
  return newChild;
1043
1082
  }
1044
1083
  _insertBefore(this, newChild, refChild);
1045
- newChild.ownerDocument = this;
1084
+ _updateOwnerDocument(newChild, this);
1046
1085
  if (this.documentElement === null && newChild.nodeType === ELEMENT_NODE) {
1047
1086
  this.documentElement = newChild;
1048
1087
  }
@@ -1058,7 +1097,7 @@ Document.prototype = {
1058
1097
  replaceChild: function (newChild, oldChild) {
1059
1098
  //raises
1060
1099
  _insertBefore(this, newChild, oldChild, assertPreReplacementValidityInDocument);
1061
- newChild.ownerDocument = this;
1100
+ _updateOwnerDocument(newChild, this);
1062
1101
  if (oldChild) {
1063
1102
  this.removeChild(oldChild);
1064
1103
  }
@@ -1158,7 +1197,22 @@ Document.prototype = {
1158
1197
  node.appendData(data)
1159
1198
  return node;
1160
1199
  },
1200
+ /**
1201
+ * Returns a new CDATASection node whose data is `data`.
1202
+ *
1203
+ * __This implementation differs from the specification:__
1204
+ * - calling this method on an HTML document does not throw `NotSupportedError`.
1205
+ *
1206
+ * @param {string} data
1207
+ * @returns {CDATASection}
1208
+ * @throws DOMException with code `INVALID_CHARACTER_ERR` if `data` contains `"]]>"`.
1209
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createCDATASection
1210
+ * @see https://dom.spec.whatwg.org/#dom-document-createcdatasection
1211
+ */
1161
1212
  createCDATASection : function(data){
1213
+ if (data.indexOf(']]>') !== -1) {
1214
+ throw new DOMException(INVALID_CHARACTER_ERR, 'data contains "]]>"');
1215
+ }
1162
1216
  var node = new CDATASection();
1163
1217
  node.ownerDocument = this;
1164
1218
  node.appendData(data)
@@ -1427,6 +1481,20 @@ function ProcessingInstruction() {
1427
1481
  ProcessingInstruction.prototype.nodeType = PROCESSING_INSTRUCTION_NODE;
1428
1482
  _extends(ProcessingInstruction,Node);
1429
1483
  function XMLSerializer(){}
1484
+ /**
1485
+ * Returns the result of serializing `node` to XML.
1486
+ *
1487
+ * __This implementation differs from the specification:__
1488
+ * - CDATASection nodes whose data contains `]]>` are serialized by splitting the section
1489
+ * at each `]]>` occurrence (following W3C DOM Level 3 Core `split-cdata-sections`
1490
+ * default behaviour). A configurable option is not yet implemented.
1491
+ *
1492
+ * @param {Node} node
1493
+ * @param {boolean} [isHtml]
1494
+ * @param {function} [nodeFilter]
1495
+ * @returns {string}
1496
+ * @see https://html.spec.whatwg.org/#dom-xmlserializer-serializetostring
1497
+ */
1430
1498
  XMLSerializer.prototype.serializeToString = function(node,isHtml,nodeFilter){
1431
1499
  return nodeSerializeToString.call(node,isHtml,nodeFilter);
1432
1500
  }
@@ -1645,7 +1713,7 @@ function serializeToString(node,buf,isHTML,nodeFilter,visibleNamespaces){
1645
1713
  .replace(/[<&>]/g,_xmlEncoder)
1646
1714
  );
1647
1715
  case CDATA_SECTION_NODE:
1648
- return buf.push( '<![CDATA[',node.data,']]>');
1716
+ return buf.push('<![CDATA[', node.data.replace(/]]>/g, ']]]]><![CDATA[>'), ']]>');
1649
1717
  case COMMENT_NODE:
1650
1718
  return buf.push( "<!--",node.data,"-->");
1651
1719
  case DOCUMENT_TYPE_NODE:
package/lib/sax.js CHANGED
@@ -597,7 +597,7 @@ function parseDCC(source,start,domBuilder,errorHandler){//sure start with '<!'
597
597
  function parseInstruction(source,start,domBuilder){
598
598
  var end = source.indexOf('?>',start);
599
599
  if(end){
600
- var match = source.substring(start,end).match(/^<\?(\S*)\s*([\s\S]*?)\s*$/);
600
+ var match = source.substring(start,end).match(/^<\?(\S*)\s*([\s\S]*?)$/);
601
601
  if(match){
602
602
  var len = match[0].length;
603
603
  domBuilder.processingInstruction(match[1], match[2]) ;
package/package.json CHANGED
@@ -1,71 +1,72 @@
1
1
  {
2
- "name": "@xmldom/xmldom",
3
- "version": "0.8.10",
4
- "description": "A pure JavaScript W3C standard-based (XML DOM Level 2 Core) DOMParser and XMLSerializer module.",
5
- "keywords": [
6
- "w3c",
7
- "dom",
8
- "xml",
9
- "parser",
10
- "javascript",
11
- "DOMParser",
12
- "XMLSerializer",
13
- "ponyfill"
14
- ],
15
- "homepage": "https://github.com/xmldom/xmldom",
16
- "repository": {
17
- "type": "git",
18
- "url": "git://github.com/xmldom/xmldom.git"
19
- },
20
- "main": "lib/index.js",
21
- "types": "index.d.ts",
22
- "files": [
23
- "CHANGELOG.md",
24
- "LICENSE",
25
- "readme.md",
26
- "SECURITY.md",
27
- "index.d.ts",
28
- "lib"
29
- ],
30
- "scripts": {
31
- "lint": "eslint lib test",
32
- "format": "prettier --write test",
33
- "changelog": "auto-changelog --unreleased-only",
34
- "start": "nodemon --watch package.json --watch lib --watch test --exec 'npm --silent run test && npm --silent run lint'",
35
- "stryker": "stryker run",
36
- "stryker:dry-run": "stryker run -m '' --reporters progress",
37
- "test": "jest",
38
- "testrelease": "npm test && eslint lib",
39
- "version": "./changelog-has-version.sh",
40
- "release": "np --no-yarn --test-script testrelease --branch release-0.8.x patch"
41
- },
42
- "engines": {
43
- "node": ">=10.0.0"
44
- },
45
- "dependencies": {},
46
- "devDependencies": {
47
- "@stryker-mutator/core": "5.6.1",
48
- "auto-changelog": "2.4.0",
49
- "eslint": "8.25.0",
50
- "eslint-config-prettier": "8.5.0",
51
- "eslint-plugin-es5": "1.5.0",
52
- "eslint-plugin-prettier": "4.2.1",
53
- "get-stream": "6.0.1",
54
- "jest": "27.5.1",
55
- "nodemon": "2.0.20",
56
- "np": "7.6.2",
57
- "prettier": "2.7.1",
58
- "xmltest": "1.5.0",
59
- "yauzl": "2.10.0"
60
- },
61
- "bugs": {
62
- "url": "https://github.com/xmldom/xmldom/issues"
63
- },
64
- "license": "MIT",
65
- "auto-changelog": {
66
- "prepend": true,
67
- "remote": "upstream",
68
- "tagPrefix": "",
69
- "template": "./auto-changelog.hbs"
70
- }
2
+ "name": "@xmldom/xmldom",
3
+ "version": "0.8.12",
4
+ "description": "A pure JavaScript W3C standard-based (XML DOM Level 2 Core) DOMParser and XMLSerializer module.",
5
+ "keywords": [
6
+ "w3c",
7
+ "dom",
8
+ "xml",
9
+ "parser",
10
+ "javascript",
11
+ "DOMParser",
12
+ "XMLSerializer",
13
+ "ponyfill"
14
+ ],
15
+ "homepage": "https://github.com/xmldom/xmldom",
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git://github.com/xmldom/xmldom.git"
19
+ },
20
+ "main": "lib/index.js",
21
+ "types": "index.d.ts",
22
+ "files": [
23
+ "CHANGELOG.md",
24
+ "LICENSE",
25
+ "readme.md",
26
+ "SECURITY.md",
27
+ "index.d.ts",
28
+ "lib"
29
+ ],
30
+ "scripts": {
31
+ "lint": "eslint lib test",
32
+ "format": "prettier --write test",
33
+ "format:check": "prettier --check test",
34
+ "changelog": "auto-changelog --unreleased-only",
35
+ "start": "nodemon --watch package.json --watch lib --watch test --exec 'npm --silent run test && npm --silent run lint'",
36
+ "stryker": "stryker run",
37
+ "stryker:dry-run": "stryker run -m '' --reporters progress",
38
+ "test": "jest",
39
+ "testrelease": "npm test && eslint lib",
40
+ "version": "./changelog-has-version.sh",
41
+ "release": "np --no-yarn --test-script testrelease --branch release-0.8.x patch"
42
+ },
43
+ "engines": {
44
+ "node": ">=10.0.0"
45
+ },
46
+ "dependencies": {},
47
+ "devDependencies": {
48
+ "@stryker-mutator/core": "5.6.1",
49
+ "auto-changelog": "2.4.0",
50
+ "eslint": "8.25.0",
51
+ "eslint-config-prettier": "8.5.0",
52
+ "eslint-plugin-es5": "1.5.0",
53
+ "eslint-plugin-prettier": "4.2.1",
54
+ "get-stream": "6.0.1",
55
+ "jest": "27.5.1",
56
+ "nodemon": "2.0.20",
57
+ "np": "9.2.0",
58
+ "prettier": "2.7.1",
59
+ "xmltest": "1.5.0",
60
+ "yauzl": "2.10.0"
61
+ },
62
+ "bugs": {
63
+ "url": "https://github.com/xmldom/xmldom/issues"
64
+ },
65
+ "license": "MIT",
66
+ "auto-changelog": {
67
+ "prepend": true,
68
+ "remote": "upstream",
69
+ "tagPrefix": "",
70
+ "template": "./auto-changelog.hbs"
71
+ }
71
72
  }