@cdk8s/awscdk-resolver 0.0.609 → 0.0.610

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.
Files changed (145) hide show
  1. package/.jsii +3 -3
  2. package/lib/resolve.js +1 -1
  3. package/node_modules/@aws-sdk/client-cloudformation/package.json +3 -3
  4. package/node_modules/@aws-sdk/core/package.json +2 -2
  5. package/node_modules/@aws-sdk/credential-provider-env/package.json +2 -2
  6. package/node_modules/@aws-sdk/credential-provider-http/package.json +2 -2
  7. package/node_modules/@aws-sdk/credential-provider-ini/package.json +9 -9
  8. package/node_modules/@aws-sdk/credential-provider-login/package.json +3 -3
  9. package/node_modules/@aws-sdk/credential-provider-node/package.json +7 -7
  10. package/node_modules/@aws-sdk/credential-provider-process/package.json +2 -2
  11. package/node_modules/@aws-sdk/credential-provider-sso/package.json +4 -4
  12. package/node_modules/@aws-sdk/credential-provider-web-identity/package.json +3 -3
  13. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/cognito-identity/index.js +1 -1
  14. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/signin/index.js +1 -1
  15. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sso/index.js +1 -1
  16. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sso-oidc/index.js +1 -1
  17. package/node_modules/@aws-sdk/nested-clients/dist-cjs/submodules/sts/index.js +1 -1
  18. package/node_modules/@aws-sdk/nested-clients/package.json +2 -2
  19. package/node_modules/@aws-sdk/token-providers/package.json +3 -3
  20. package/node_modules/@aws-sdk/xml-builder/dist-cjs/xml-parser.browser.js +1 -1
  21. package/node_modules/@aws-sdk/xml-builder/dist-cjs/xml-parser.js +233 -43
  22. package/node_modules/@aws-sdk/xml-builder/dist-es/xml-parser.browser.js +1 -1
  23. package/node_modules/@aws-sdk/xml-builder/dist-es/xml-parser.js +233 -43
  24. package/node_modules/@aws-sdk/xml-builder/dist-types/ts3.4/xml-parser.d.ts +1 -1
  25. package/node_modules/@aws-sdk/xml-builder/dist-types/xml-parser.d.ts +2 -1
  26. package/node_modules/@aws-sdk/xml-builder/package.json +4 -4
  27. package/node_modules/@smithy/core/dist-cjs/submodules/cbor/index.js +518 -371
  28. package/node_modules/@smithy/core/dist-cjs/submodules/client/index.js +1 -1
  29. package/node_modules/@smithy/core/dist-es/submodules/cbor/cbor-decode.js +326 -269
  30. package/node_modules/@smithy/core/dist-es/submodules/cbor/cbor-encode.js +192 -103
  31. package/node_modules/@smithy/core/dist-es/submodules/cbor/cbor.js +4 -2
  32. package/node_modules/@smithy/core/dist-es/submodules/client/smithy-client/get-value-from-text-node.js +1 -1
  33. package/node_modules/@smithy/core/dist-types/submodules/cbor/cbor-decode.d.ts +4 -0
  34. package/node_modules/@smithy/core/dist-types/submodules/cbor/cbor-encode.d.ts +8 -4
  35. package/node_modules/@smithy/core/package.json +1 -1
  36. package/node_modules/@smithy/credential-provider-imds/package.json +2 -2
  37. package/node_modules/@smithy/fetch-http-handler/package.json +2 -2
  38. package/node_modules/@smithy/node-http-handler/package.json +2 -2
  39. package/node_modules/@smithy/signature-v4/package.json +2 -2
  40. package/package.json +6 -6
  41. package/node_modules/@aws-sdk/xml-builder/dist-cjs/xml-external/nodable_entities.js +0 -333
  42. package/node_modules/@aws-sdk/xml-builder/dist-es/xml-external/nodable_entities.js +0 -332
  43. package/node_modules/@aws-sdk/xml-builder/dist-types/ts3.4/xml-external/nodable_entities.d.ts +0 -66
  44. package/node_modules/@aws-sdk/xml-builder/dist-types/xml-external/nodable_entities.d.ts +0 -65
  45. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/CHANGELOG.md +0 -827
  46. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/LICENSE +0 -21
  47. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/README.md +0 -202
  48. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/lib/fxbuilder.min.js +0 -2
  49. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/lib/fxbuilder.min.js.map +0 -1
  50. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/lib/fxp.cjs +0 -1
  51. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/lib/fxp.d.cts +0 -770
  52. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/lib/fxp.min.js +0 -2
  53. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/lib/fxp.min.js.map +0 -1
  54. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/lib/fxparser.min.js +0 -2
  55. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/lib/fxparser.min.js.map +0 -1
  56. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/lib/fxvalidator.min.js +0 -2
  57. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/lib/fxvalidator.min.js.map +0 -1
  58. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/package.json +0 -93
  59. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/cli/cli.js +0 -97
  60. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/cli/man.js +0 -17
  61. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/cli/read.js +0 -43
  62. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/fxp.d.ts +0 -739
  63. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/fxp.js +0 -14
  64. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/ignoreAttributes.js +0 -18
  65. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/util.js +0 -61
  66. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/CharsSymbol.js +0 -16
  67. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/EntitiesParser.js +0 -106
  68. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/OptionsBuilder.js +0 -61
  69. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/OutputBuilders/BaseOutputBuilder.js +0 -69
  70. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/OutputBuilders/JsArrBuilder.js +0 -103
  71. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/OutputBuilders/JsMinArrBuilder.js +0 -100
  72. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/OutputBuilders/JsObjBuilder.js +0 -154
  73. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/OutputBuilders/ParserOptionsBuilder.js +0 -94
  74. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/Report.js +0 -0
  75. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/TagPath.js +0 -81
  76. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/TagPathMatcher.js +0 -13
  77. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/XMLParser.js +0 -83
  78. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/Xml2JsParser.js +0 -235
  79. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/XmlPartReader.js +0 -210
  80. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/XmlSpecialTagsReader.js +0 -111
  81. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/inputSource/BufferSource.js +0 -116
  82. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/inputSource/StringSource.js +0 -121
  83. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/valueParsers/EntitiesParser.js +0 -105
  84. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/valueParsers/booleanParser.js +0 -22
  85. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/valueParsers/booleanParserExt.js +0 -19
  86. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/valueParsers/currency.js +0 -38
  87. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/valueParsers/join.js +0 -13
  88. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/valueParsers/number.js +0 -14
  89. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/v6/valueParsers/trim.js +0 -6
  90. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/validator.js +0 -425
  91. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/xmlbuilder/json2xml.js +0 -6
  92. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/xmlparser/DocTypeReader.js +0 -407
  93. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/xmlparser/OptionsBuilder.js +0 -163
  94. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/xmlparser/OrderedObjParser.js +0 -840
  95. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/xmlparser/XMLParser.js +0 -71
  96. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/xmlparser/node2json.js +0 -177
  97. package/node_modules/@aws-sdk/xml-builder/node_modules/fast-xml-parser/src/xmlparser/xmlNode.js +0 -40
  98. package/node_modules/@nodable/entities/README.md +0 -41
  99. package/node_modules/@nodable/entities/package.json +0 -57
  100. package/node_modules/@nodable/entities/src/EntityDecoder.js +0 -644
  101. package/node_modules/@nodable/entities/src/EntityEncoder.js +0 -194
  102. package/node_modules/@nodable/entities/src/entities.js +0 -1159
  103. package/node_modules/@nodable/entities/src/entityTries.js +0 -49
  104. package/node_modules/@nodable/entities/src/index.d.ts +0 -341
  105. package/node_modules/@nodable/entities/src/index.js +0 -29
  106. package/node_modules/anynum/LICENSE +0 -21
  107. package/node_modules/anynum/README.md +0 -142
  108. package/node_modules/anynum/anynum.js +0 -135
  109. package/node_modules/anynum/digitTable.js +0 -116
  110. package/node_modules/anynum/package.json +0 -42
  111. package/node_modules/fast-xml-builder/CHANGELOG.md +0 -40
  112. package/node_modules/fast-xml-builder/LICENSE +0 -21
  113. package/node_modules/fast-xml-builder/README.md +0 -74
  114. package/node_modules/fast-xml-builder/lib/fxb.cjs +0 -1
  115. package/node_modules/fast-xml-builder/lib/fxb.d.cts +0 -270
  116. package/node_modules/fast-xml-builder/lib/fxb.min.js +0 -2
  117. package/node_modules/fast-xml-builder/lib/fxb.min.js.map +0 -1
  118. package/node_modules/fast-xml-builder/package.json +0 -81
  119. package/node_modules/fast-xml-builder/src/fxb.d.ts +0 -270
  120. package/node_modules/fast-xml-builder/src/fxb.js +0 -599
  121. package/node_modules/fast-xml-builder/src/ignoreAttributes.js +0 -18
  122. package/node_modules/fast-xml-builder/src/orderedJs2Xml.js +0 -359
  123. package/node_modules/fast-xml-builder/src/prettifyJs2Xml.js +0 -0
  124. package/node_modules/fast-xml-builder/src/util.js +0 -16
  125. package/node_modules/path-expression-matcher/LICENSE +0 -21
  126. package/node_modules/path-expression-matcher/README.md +0 -947
  127. package/node_modules/path-expression-matcher/lib/pem.cjs +0 -1
  128. package/node_modules/path-expression-matcher/lib/pem.d.cts +0 -715
  129. package/node_modules/path-expression-matcher/lib/pem.min.js +0 -2
  130. package/node_modules/path-expression-matcher/lib/pem.min.js.map +0 -1
  131. package/node_modules/path-expression-matcher/package.json +0 -79
  132. package/node_modules/path-expression-matcher/src/Expression.js +0 -232
  133. package/node_modules/path-expression-matcher/src/ExpressionSet.js +0 -225
  134. package/node_modules/path-expression-matcher/src/Matcher.js +0 -660
  135. package/node_modules/path-expression-matcher/src/index.d.ts +0 -589
  136. package/node_modules/path-expression-matcher/src/index.js +0 -29
  137. package/node_modules/strnum/CHANGELOG.md +0 -51
  138. package/node_modules/strnum/LICENSE +0 -21
  139. package/node_modules/strnum/README.md +0 -113
  140. package/node_modules/strnum/package.json +0 -36
  141. package/node_modules/strnum/strnum.js +0 -171
  142. package/node_modules/xml-naming/README.md +0 -189
  143. package/node_modules/xml-naming/package.json +0 -54
  144. package/node_modules/xml-naming/src/index.d.ts +0 -74
  145. package/node_modules/xml-naming/src/index.js +0 -270
@@ -1,644 +0,0 @@
1
- // ---------------------------------------------------------------------------
2
- // Built-in named entity map (name → replacement string)
3
- // No regex, no {regex,val} objects — just flat key/value pairs.
4
- // ---------------------------------------------------------------------------
5
-
6
- import { XML as DEFAULT_XML_ENTITIES } from "./entities.js"
7
-
8
- // ---------------------------------------------------------------------------
9
- // Entity hook action constants
10
- // ---------------------------------------------------------------------------
11
-
12
- /**
13
- * Action constants for `onExternalEntity` and `onInputEntity` hooks.
14
- *
15
- * Use these instead of raw strings to avoid typos:
16
- *
17
- * @example
18
- * import EntityDecoder, { ENTITY_ACTION } from './EntityDecoder.js';
19
- * const dec = new EntityDecoder({
20
- * onInputEntity: (name, value) => ENTITY_ACTION.BLOCK,
21
- * });
22
- */
23
- export const ENTITY_ACTION = Object.freeze({
24
- /** Resolve and expand the entity normally. */
25
- ALLOW: 'allow',
26
- /** Silently skip this entity — it will not be registered. */
27
- BLOCK: 'block',
28
- /** Throw an error, aborting entity registration entirely. */
29
- THROW: 'throw',
30
- });
31
-
32
- // ---------------------------------------------------------------------------
33
- // Helpers
34
- // ---------------------------------------------------------------------------
35
-
36
- const SPECIAL_CHARS = new Set('!?\\\\/[]$%{}^&*()<>|+');
37
-
38
- /**
39
- * Validate that an entity name contains no dangerous characters.
40
- * @param {string} name
41
- * @returns {string} the name, unchanged
42
- * @throws {Error} on invalid characters
43
- */
44
- function validateEntityName(name) {
45
- if (name[0] === '#') {
46
- throw new Error(`[EntityReplacer] Invalid character '#' in entity name: "${name}"`);
47
- }
48
- for (const ch of name) {
49
- if (SPECIAL_CHARS.has(ch)) {
50
- throw new Error(`[EntityReplacer] Invalid character '${ch}' in entity name: "${name}"`);
51
- }
52
- }
53
- return name;
54
- }
55
-
56
- /**
57
- * Merge one or more entity maps into a flat name→string map.
58
- * Accepts either:
59
- * - plain string values: { amp: '&' }
60
- * - legacy {regex,val} / {regx,val}: { lt: { regex: /.../, val: '<' } }
61
- *
62
- * Values containing '&' are skipped (recursive expansion risk).
63
- *
64
- * @param {...object} maps
65
- * @returns {Record<string, string>}
66
- */
67
- function mergeEntityMaps(...maps) {
68
- const out = Object.create(null);
69
- for (const map of maps) {
70
- if (!map) continue;
71
- for (const key of Object.keys(map)) {
72
- const raw = map[key];
73
- if (typeof raw === 'string') {
74
- out[key] = raw;
75
- } else if (raw && typeof raw === 'object' && raw.val !== undefined) {
76
- // Legacy {regex,val} or {regx,val} — extract the string val only
77
- const val = raw.val;
78
- if (typeof val === 'string') {
79
- out[key] = val;
80
- }
81
- // function vals are not supported in the scanner — skip
82
- }
83
- }
84
- }
85
- return out;
86
- }
87
-
88
- // ---------------------------------------------------------------------------
89
- // applyLimitsTo helpers
90
- // ---------------------------------------------------------------------------
91
-
92
- const LIMIT_TIER_EXTERNAL = 'external'; // input/runtime + persistent external maps
93
- const LIMIT_TIER_BASE = 'base'; // DEFAULT_XML_ENTITIES + namedEntities (system) maps
94
- const LIMIT_TIER_ALL = 'all'; // every entity regardless of tier
95
-
96
- /**
97
- * Resolve `applyLimitsTo` option into a normalised Set of tier strings.
98
- * Accepted values: 'external' | 'base' | 'all' | string[]
99
- * Default: 'external' (only untrusted injected entities are counted).
100
- * @param {string|string[]|undefined} raw
101
- * @returns {Set<string>}
102
- */
103
- function parseLimitTiers(raw) {
104
- if (!raw || raw === LIMIT_TIER_EXTERNAL) return new Set([LIMIT_TIER_EXTERNAL]);
105
- if (raw === LIMIT_TIER_ALL) return new Set([LIMIT_TIER_ALL]);
106
- if (raw === LIMIT_TIER_BASE) return new Set([LIMIT_TIER_BASE]);
107
- if (Array.isArray(raw)) return new Set(raw);
108
- return new Set([LIMIT_TIER_EXTERNAL]); // safe default for unrecognised values
109
- }
110
-
111
- // ---------------------------------------------------------------------------
112
- // NCR (Numeric Character Reference) classification
113
- // ---------------------------------------------------------------------------
114
-
115
- // Severity order — higher number = stricter action.
116
- // Used to enforce minimum action levels for specific codepoint ranges.
117
- const NCR_LEVEL = Object.freeze({ allow: 0, leave: 1, remove: 2, throw: 3 });
118
-
119
- // XML 1.0 §2.2: allowed chars are #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
120
- // Restricted C0: U+0001–U+001F excluding U+0009, U+000A, U+000D
121
- const XML10_ALLOWED_C0 = new Set([0x09, 0x0A, 0x0D]);
122
-
123
- /**
124
- * Parse the `ncr` constructor option into flat, hot-path-friendly fields.
125
- * @param {object|undefined} ncr
126
- * @returns {{ xmlVersion: number, onLevel: number, nullLevel: number }}
127
- */
128
- function parseNCRConfig(ncr) {
129
- if (!ncr) {
130
- return { xmlVersion: 1.0, onLevel: NCR_LEVEL.allow, nullLevel: NCR_LEVEL.remove };
131
- }
132
- const xmlVersion = ncr.xmlVersion === 1.1 ? 1.1 : 1.0;
133
- const onLevel = NCR_LEVEL[ncr.onNCR] ?? NCR_LEVEL.allow;
134
- const nullLevel = NCR_LEVEL[ncr.nullNCR] ?? NCR_LEVEL.remove;
135
- // 'allow' is not meaningful for null — clamp to at least 'remove'
136
- const clampedNull = Math.max(nullLevel, NCR_LEVEL.remove);
137
- return { xmlVersion, onLevel, nullLevel: clampedNull };
138
- }
139
-
140
- // ---------------------------------------------------------------------------
141
- // EntityReplacer
142
- // ---------------------------------------------------------------------------
143
-
144
- /**
145
- * Single-pass, zero-regex entity replacer for XML/HTML content.
146
- *
147
- * Algorithm: scan the string once for '&', read to ';', resolve via map
148
- * or direct codepoint conversion, build output chunks, join once at the end.
149
- *
150
- * Entity lookup priority (highest → lowest):
151
- * 1. input / runtime (DOCTYPE entities for current document)
152
- * 2. persistent external (survive across documents)
153
- * 3. base named map (DEFAULT_XML_ENTITIES + user-supplied namedEntities)
154
- *
155
- * Both input and external resolve as the 'external' tier for limit purposes.
156
- * Base map entities resolve as the 'base' tier.
157
- *
158
- * Numeric / hex references (&#NNN; / &#xHH;) are resolved directly via
159
- * String.fromCodePoint() — no map needed. They count as 'base' tier.
160
- *
161
- * @example
162
- * const replacer = new EntityReplacer({ namedEntities: COMMON_HTML });
163
- * replacer.setExternalEntities({ brand: 'Acme' });
164
- *
165
- * const instance = replacer.reset();
166
- * instance.addInputEntities({ version: '1.0' });
167
- * instance.encode('&brand; v&version; &lt;'); // 'Acme v1.0 <'
168
- */
169
- export default class EntityDecoder {
170
- /**
171
- * @param {object} [options]
172
- * @param {object|null} [options.namedEntities] — extra named entities merged into base map
173
- * @param {object} [options.limit] — security limits
174
- * @param {number} [options.limit.maxTotalExpansions=0] — 0 = unlimited
175
- * @param {number} [options.limit.maxExpandedLength=0] — 0 = unlimited
176
- * @param {'external'|'base'|'all'|string[]} [options.limit.applyLimitsTo='external']
177
- * Which entity tiers count against the security limits:
178
- * - 'external' (default) — only input/runtime + persistent external entities
179
- * - 'base' — only DEFAULT_XML_ENTITIES + namedEntities
180
- * - 'all' — every entity regardless of tier
181
- * - string[] — explicit combination, e.g. ['external', 'base']
182
- * @param {((resolved: string, original: string) => string)|null} [options.postCheck=null]
183
- * @param {string[]} [options.remove=[]] — entity names (e.g. ['nbsp', '#13']) to delete (replace with empty string)
184
- * @param {string[]} [options.leave=[]] — entity names to keep as literal (unchanged in output)
185
- * @param {object} [options.ncr] — Numeric Character Reference controls
186
- * @param {1.0|1.1} [options.ncr.xmlVersion=1.0]
187
- * XML version governing which codepoint ranges are restricted:
188
- * - 1.0 — C0 controls U+0001–U+001F (except U+0009/000A/000D) are prohibited
189
- * - 1.1 — C0 controls are allowed when written as NCRs; C1 (U+007F–U+009F) decoded as-is
190
- * @param {'allow'|'leave'|'remove'|'throw'} [options.ncr.onNCR='allow']
191
- * Base action for numeric references. Severity order: allow < leave < remove < throw.
192
- * For codepoint ranges that carry a minimum level (surrogates → remove, XML 1.0 C0 → remove),
193
- * the effective action is max(onNCR, rangeMinimum).
194
- * @param {'remove'|'throw'} [options.ncr.nullNCR='remove']
195
- * Action for U+0000 (null). 'allow' and 'leave' are clamped to 'remove' since null is never safe.
196
- * @param {((name: string, value: string) => 'allow'|'block'|'throw')|null} [options.onExternalEntity=null]
197
- * Hook called when an external entity is registered via `setExternalEntities()` or
198
- * `addExternalEntity()`. Return `ENTITY_ACTION.ALLOW` to accept the entity,
199
- * `ENTITY_ACTION.BLOCK` to silently skip it, or `ENTITY_ACTION.THROW` to abort with an error.
200
- * @param {((name: string, value: string) => 'allow'|'block'|'throw')|null} [options.onInputEntity=null]
201
- * Hook called when an input entity is registered via `addInputEntities()`. Return
202
- * `ENTITY_ACTION.ALLOW` to accept, `ENTITY_ACTION.BLOCK` to silently skip, or
203
- * `ENTITY_ACTION.THROW` to abort with an error.
204
- */
205
- constructor(options = {}) {
206
- this._limit = options.limit || {};
207
- this._maxTotalExpansions = this._limit.maxTotalExpansions || 0;
208
- this._maxExpandedLength = this._limit.maxExpandedLength || 0;
209
- this._postCheck = typeof options.postCheck === 'function' ? options.postCheck : r => r;
210
- this._limitTiers = parseLimitTiers(this._limit.applyLimitsTo ?? LIMIT_TIER_EXTERNAL);
211
- this._numericAllowed = options.numericAllowed ?? true;
212
- // Base map: DEFAULT_XML_ENTITIES + user-supplied extras. Immutable after construction.
213
- this._baseMap = mergeEntityMaps(DEFAULT_XML_ENTITIES, options.namedEntities || null);
214
-
215
- // Persistent external entities — survive across documents.
216
- // Stored as a separate map so reset() never touches them.
217
- /** @type {Record<string, string>} */
218
- this._externalMap = Object.create(null);
219
-
220
- // Input / runtime entities — current document only, wiped on reset().
221
- /** @type {Record<string, string>} */
222
- this._inputMap = Object.create(null);
223
-
224
- // Per-document counters
225
- this._totalExpansions = 0;
226
- this._expandedLength = 0;
227
-
228
- // --- New: remove / leave sets ---
229
- /** @type {Set<string>} */
230
- this._removeSet = new Set(options.remove && Array.isArray(options.remove) ? options.remove : []);
231
- /** @type {Set<string>} */
232
- this._leaveSet = new Set(options.leave && Array.isArray(options.leave) ? options.leave : []);
233
-
234
- // --- NCR config (parsed into flat fields for hot-path speed) ---
235
- const ncrCfg = parseNCRConfig(options.ncr);
236
- this._ncrXmlVersion = ncrCfg.xmlVersion;
237
- this._ncrOnLevel = ncrCfg.onLevel;
238
- this._ncrNullLevel = ncrCfg.nullLevel;
239
-
240
- // --- Registration hooks ---
241
- /** @type {((name: string, value: string) => 'allow'|'block'|'throw')|null} */
242
- this._onExternalEntity = typeof options.onExternalEntity === 'function'
243
- ? options.onExternalEntity
244
- : null;
245
- /** @type {((name: string, value: string) => 'allow'|'block'|'throw')|null} */
246
- this._onInputEntity = typeof options.onInputEntity === 'function'
247
- ? options.onInputEntity
248
- : null;
249
- }
250
-
251
- // -------------------------------------------------------------------------
252
- // Private: registration hook dispatch
253
- // -------------------------------------------------------------------------
254
-
255
- /**
256
- * Invoke a registration hook for a single entity name/value pair.
257
- * Returns true when the entity should be accepted, false when it should be
258
- * silently skipped (BLOCK), and throws when the hook returns THROW.
259
- *
260
- * @param {((name: string, value: string) => 'allow'|'block'|'throw')|null} hook
261
- * @param {string} name
262
- * @param {string} value
263
- * @param {string} context — used in error messages ('external' | 'input')
264
- * @returns {boolean} true = accept, false = skip
265
- */
266
- _applyRegistrationHook(hook, name, value, context) {
267
- if (!hook) return true; // no hook → always accept
268
- const action = hook(name, value);
269
- if (action === ENTITY_ACTION.BLOCK) return false;
270
- if (action === ENTITY_ACTION.THROW) {
271
- throw new Error(
272
- `[EntityDecoder] Registration of ${context} entity "&${name};" was rejected by hook`
273
- );
274
- }
275
- return true; // ALLOW or any unknown return value → accept
276
- }
277
-
278
- // -------------------------------------------------------------------------
279
- // Persistent external entity registration
280
- // -------------------------------------------------------------------------
281
-
282
- /**
283
- * Replace the full set of persistent external entities.
284
- * All keys are validated — throws on invalid characters.
285
- * If `onExternalEntity` is set, it is called once per entry; entries that
286
- * return `ENTITY_ACTION.BLOCK` are silently omitted, `ENTITY_ACTION.THROW`
287
- * aborts the whole call.
288
- * @param {Record<string, string | { regex?: RegExp, val: string }>} map
289
- */
290
- setExternalEntities(map) {
291
- if (map) {
292
- for (const key of Object.keys(map)) {
293
- validateEntityName(key);
294
- }
295
- }
296
- if (!this._onExternalEntity) {
297
- this._externalMap = mergeEntityMaps(map);
298
- return;
299
- }
300
- // Hook present — resolve values first, then filter
301
- const flat = mergeEntityMaps(map);
302
- const filtered = Object.create(null);
303
- for (const [name, value] of Object.entries(flat)) {
304
- if (this._applyRegistrationHook(this._onExternalEntity, name, value, 'external')) {
305
- filtered[name] = value;
306
- }
307
- }
308
- this._externalMap = filtered;
309
- }
310
-
311
- /**
312
- * Add a single persistent external entity.
313
- * If `onExternalEntity` is set it is called before the entity is stored;
314
- * `ENTITY_ACTION.BLOCK` silently skips storage, `ENTITY_ACTION.THROW` raises.
315
- * @param {string} key
316
- * @param {string} value
317
- */
318
- addExternalEntity(key, value) {
319
- validateEntityName(key);
320
- if (typeof value === 'string' && value.indexOf('&') === -1) {
321
- if (this._applyRegistrationHook(this._onExternalEntity, key, value, 'external')) {
322
- this._externalMap[key] = value;
323
- }
324
- }
325
- }
326
-
327
- // -------------------------------------------------------------------------
328
- // Input / runtime entity registration (per document)
329
- // -------------------------------------------------------------------------
330
-
331
- /**
332
- * Inject DOCTYPE entities for the current document.
333
- * Also resets per-document expansion counters.
334
- * If `onInputEntity` is set it is called once per entry; entries returning
335
- * `ENTITY_ACTION.BLOCK` are silently omitted, `ENTITY_ACTION.THROW` aborts.
336
- * @param {Record<string, string | { regx?: RegExp, regex?: RegExp, val: string }>} map
337
- */
338
- addInputEntities(map) {
339
- this._totalExpansions = 0;
340
- this._expandedLength = 0;
341
- if (!this._onInputEntity) {
342
- this._inputMap = mergeEntityMaps(map);
343
- return;
344
- }
345
- const flat = mergeEntityMaps(map);
346
- const filtered = Object.create(null);
347
- for (const [name, value] of Object.entries(flat)) {
348
- if (this._applyRegistrationHook(this._onInputEntity, name, value, 'input')) {
349
- filtered[name] = value;
350
- }
351
- }
352
- this._inputMap = filtered;
353
- }
354
-
355
- // -------------------------------------------------------------------------
356
- // Per-document reset
357
- // -------------------------------------------------------------------------
358
-
359
- /**
360
- * Wipe input/runtime entities and reset counters.
361
- * Call this before processing each new document.
362
- * @returns {this}
363
- */
364
- reset() {
365
- this._inputMap = Object.create(null);
366
- this._totalExpansions = 0;
367
- this._expandedLength = 0;
368
- return this;
369
- }
370
-
371
- // -------------------------------------------------------------------------
372
- // XML version (can be set after construction, e.g. once parser reads <?xml?>)
373
- // -------------------------------------------------------------------------
374
-
375
- /**
376
- * Update the XML version used for NCR classification.
377
- * Call this as soon as the document's `<?xml version="...">` declaration is parsed.
378
- * @param {1.0|1.1|number} version
379
- */
380
- setXmlVersion(version) {
381
- this._ncrXmlVersion = version === 1.1 ? 1.1 : 1.0;
382
- }
383
-
384
- // -------------------------------------------------------------------------
385
- // Primary API
386
- // -------------------------------------------------------------------------
387
-
388
- /**
389
- * Replace all entity references in `str` in a single pass.
390
- *
391
- * @param {string} str
392
- * @returns {string}
393
- */
394
- decode(str) {
395
- if (typeof str !== 'string' || str.length === 0) return str;
396
- //TODO: check if needed
397
- if (str.indexOf('&') === -1) return str; // fast path — no entities at all
398
-
399
- const original = str;
400
- const chunks = [];
401
- const len = str.length;
402
- let last = 0; // start of next unprocessed literal chunk
403
- let i = 0;
404
-
405
- const limitExpansions = this._maxTotalExpansions > 0;
406
- const limitLength = this._maxExpandedLength > 0;
407
- const checkLimits = limitExpansions || limitLength;
408
-
409
- while (i < len) {
410
- // Scan forward to next '&'
411
- if (str.charCodeAt(i) !== 38 /* '&' */) { i++; continue; }
412
-
413
- // --- Found '&' at position i ---
414
-
415
- // Scan forward to ';'
416
- let j = i + 1;
417
- while (j < len && str.charCodeAt(j) !== 59 /* ';' */ && (j - i) <= 32) j++;
418
-
419
- if (j >= len || str.charCodeAt(j) !== 59) {
420
- // No closing ';' within window — treat '&' as literal
421
- i++;
422
- continue;
423
- }
424
-
425
- // Raw token between '&' and ';' (exclusive)
426
- const token = str.slice(i + 1, j);
427
- if (token.length === 0) { i++; continue; }
428
-
429
- let replacement;
430
- let tier; // which limit tier this entity belongs to
431
-
432
- if (this._removeSet.has(token)) {
433
- // Remove entity: replace with empty string
434
- replacement = '';
435
- // If entity was unknown (replacement undefined), we still need a tier for limits.
436
- // Treat as external tier because it's user-directed removal of an unknown reference.
437
- if (tier === undefined) {
438
- tier = LIMIT_TIER_EXTERNAL;
439
- }
440
- } else if (this._leaveSet.has(token)) {
441
- // Do not replace — keep original &token; as literal
442
- i++;
443
- continue;
444
- } else if (token.charCodeAt(0) === 35 /* '#' */) {
445
- // ---- Numeric / NCR reference ----
446
- // NCR classification always runs first — prohibited codepoints must be
447
- // caught regardless of numericAllowed.
448
- const ncrResult = this._resolveNCR(token);
449
- if (ncrResult === undefined) {
450
- // 'leave' action — keep original &token; as-is
451
- i++;
452
- continue;
453
- }
454
- replacement = ncrResult; // '' for remove, char string for allow
455
- tier = LIMIT_TIER_BASE;
456
- } else {
457
- // ---- Named reference ----
458
- const resolved = this._resolveName(token);
459
- replacement = resolved?.value;
460
- tier = resolved?.tier;
461
- }
462
-
463
- if (replacement === undefined) {
464
- // Unknown entity — leave as-is, advance past '&' only
465
- i++;
466
- continue;
467
- }
468
-
469
- // Flush literal chunk before this entity
470
- if (i > last) chunks.push(str.slice(last, i));
471
- chunks.push(replacement);
472
- last = j + 1; // skip past ';'
473
- i = last;
474
-
475
- // Apply expansion limits only if this tier is being tracked
476
- if (checkLimits && this._tierCounts(tier)) {
477
- if (limitExpansions) {
478
- this._totalExpansions++;
479
- if (this._totalExpansions > this._maxTotalExpansions) {
480
- throw new Error(
481
- `[EntityReplacer] Entity expansion count limit exceeded: ` +
482
- `${this._totalExpansions} > ${this._maxTotalExpansions}`
483
- );
484
- }
485
- }
486
- if (limitLength) {
487
- // delta: replacement.length minus the raw &token; length (token.length + 2 for '&' and ';')
488
- const delta = replacement.length - (token.length + 2);
489
- if (delta > 0) {
490
- this._expandedLength += delta;
491
- if (this._expandedLength > this._maxExpandedLength) {
492
- throw new Error(
493
- `[EntityReplacer] Expanded content length limit exceeded: ` +
494
- `${this._expandedLength} > ${this._maxExpandedLength}`
495
- );
496
- }
497
- }
498
- }
499
- }
500
- }
501
-
502
- // Flush trailing literal
503
- if (last < len) chunks.push(str.slice(last));
504
-
505
- // If nothing was replaced, chunks is empty — return original
506
- const result = chunks.length === 0 ? str : chunks.join('');
507
-
508
- return this._postCheck(result, original);
509
- }
510
-
511
- // -------------------------------------------------------------------------
512
- // Private: limit tier check
513
- // -------------------------------------------------------------------------
514
-
515
- /**
516
- * Returns true if a resolved entity of the given tier should count
517
- * against the expansion/length limits.
518
- * @param {string} tier — LIMIT_TIER_EXTERNAL | LIMIT_TIER_BASE
519
- * @returns {boolean}
520
- */
521
- _tierCounts(tier) {
522
- if (this._limitTiers.has(LIMIT_TIER_ALL)) return true;
523
- return this._limitTiers.has(tier);
524
- }
525
-
526
- // -------------------------------------------------------------------------
527
- // Private: entity resolution
528
- // -------------------------------------------------------------------------
529
-
530
- /**
531
- * Resolve a named entity token (without & and ;).
532
- * Priority: inputMap > externalMap > baseMap
533
- * Returns the resolved value tagged with its limit tier.
534
- *
535
- * @param {string} name
536
- * @returns {{ value: string, tier: string }|undefined}
537
- */
538
- _resolveName(name) {
539
- // input and external both count as 'external' tier for limit purposes —
540
- // they are injected at runtime and are the untrusted surface.
541
- if (name in this._inputMap) return { value: this._inputMap[name], tier: LIMIT_TIER_EXTERNAL };
542
- if (name in this._externalMap) return { value: this._externalMap[name], tier: LIMIT_TIER_EXTERNAL };
543
- if (name in this._baseMap) return { value: this._baseMap[name], tier: LIMIT_TIER_BASE };
544
- return undefined;
545
- }
546
-
547
- /**
548
- * Classify a codepoint and return the minimum action level that must be applied.
549
- * Returns -1 when no minimum is imposed (normal allow path).
550
- *
551
- * Ranges checked (in priority order):
552
- * 1. U+0000 — null, governed by nullNCR (always ≥ remove)
553
- * 2. U+D800–U+DFFF — surrogates, always prohibited (min: remove)
554
- * 3. U+0001–U+001F \ {0x09,0x0A,0x0D} — XML 1.0 restricted C0 (min: remove)
555
- * (skipped in XML 1.1 — C0 controls are allowed when written as NCRs)
556
- *
557
- * @param {number} cp — codepoint
558
- * @returns {number} — minimum NCR_LEVEL value, or -1 for no restriction
559
- */
560
- _classifyNCR(cp) {
561
- // 1. Null
562
- if (cp === 0) return this._ncrNullLevel;
563
-
564
- // 2. Surrogates — always prohibited, minimum 'remove'
565
- if (cp >= 0xD800 && cp <= 0xDFFF) return NCR_LEVEL.remove;
566
-
567
- // 3. XML 1.0 restricted C0 controls
568
- if (this._ncrXmlVersion === 1.0) {
569
- if (cp >= 0x01 && cp <= 0x1F && !XML10_ALLOWED_C0.has(cp)) return NCR_LEVEL.remove;
570
- }
571
-
572
- return -1; // no restriction
573
- }
574
-
575
- /**
576
- * Execute a resolved NCR action.
577
- *
578
- * @param {number} action — NCR_LEVEL value
579
- * @param {string} token — raw token (e.g. '#38') for error messages
580
- * @param {number} cp — codepoint, used only for error messages
581
- * @returns {string|undefined}
582
- * - decoded character string → 'allow'
583
- * - '' → 'remove'
584
- * - undefined → 'leave' (caller must skip past '&' only)
585
- * - throws Error → 'throw'
586
- */
587
- _applyNCRAction(action, token, cp) {
588
- switch (action) {
589
- case NCR_LEVEL.allow: return String.fromCodePoint(cp);
590
- case NCR_LEVEL.remove: return '';
591
- case NCR_LEVEL.leave: return undefined; // signal: keep literal
592
- case NCR_LEVEL.throw:
593
- throw new Error(
594
- `[EntityDecoder] Prohibited numeric character reference ` +
595
- `&${token}; (U+${cp.toString(16).toUpperCase().padStart(4, '0')})`
596
- );
597
- default: return String.fromCodePoint(cp);
598
- }
599
- }
600
-
601
- /**
602
- * Full NCR resolution pipeline for a numeric token.
603
- *
604
- * Steps:
605
- * 1. Parse the codepoint (decimal or hex).
606
- * 2. Validate the raw codepoint range (NaN, <0, >0x10FFFF).
607
- * 3. If numericAllowed is false and no minimum restriction applies → leave as-is.
608
- * 4. Classify the codepoint to find the minimum required action level.
609
- * 5. Resolve effective action = max(onNCR, minimum).
610
- * 6. Apply and return.
611
- *
612
- * @param {string} token — e.g. '#38', '#x26', '#X26'
613
- * @returns {string|undefined}
614
- * - string (incl. '') — replacement ('' = remove)
615
- * - undefined — leave original &token; as-is
616
- */
617
- _resolveNCR(token) {
618
- // Step 1: parse codepoint
619
- const second = token.charCodeAt(1);
620
- let cp;
621
- if (second === 120 /* x */ || second === 88 /* X */) {
622
- cp = parseInt(token.slice(2), 16);
623
- } else {
624
- cp = parseInt(token.slice(1), 10);
625
- }
626
-
627
- // Step 2: out-of-range → leave as-is unconditionally
628
- if (Number.isNaN(cp) || cp < 0 || cp > 0x10FFFF) return undefined;
629
-
630
- // Step 3: classify to get minimum action level
631
- const minimum = this._classifyNCR(cp);
632
-
633
- // Step 4: if numericAllowed is false and no hard minimum → leave
634
- if (!this._numericAllowed && minimum < NCR_LEVEL.remove) return undefined;
635
-
636
- // Step 5: effective action = max(configured onNCR, range minimum)
637
- const effective = minimum === -1
638
- ? this._ncrOnLevel
639
- : Math.max(this._ncrOnLevel, minimum);
640
-
641
- // Step 6: apply
642
- return this._applyNCRAction(effective, token, cp);
643
- }
644
- }