@cdk8s/awscdk-resolver 0.0.544 → 0.0.545

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