Skip to content

Commit f42e713

Browse files
committed
fix(redaction): bound XML attribute scanning
1 parent d4480b7 commit f42e713

2 files changed

Lines changed: 45 additions & 8 deletions

File tree

‎src/lib/redact.ts‎

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,6 @@ const OTHER_FRAMED_CREDENTIALS: Array<[RegExp, string]> = [
100100
),
101101
"element",
102102
],
103-
[
104-
new RegExp(
105-
`(<[^\\S\\r\\n]*(?:[A-Za-z_][\\w.-]*:)?[A-Za-z_][\\w:.-]*)(?=[^>]*?(?<![\\w:.-])(?:name|key|id)[^\\S]*=[^\\S]*["']?(?:${CREDENTIAL_HEADER_LABEL})["']?(?=[\\s/>]))[\\s\\S]*`,
106-
"gi",
107-
),
108-
"element",
109-
],
110103
// Multipart part: everything from a credential-named part header to the end
111104
// of the input. Part-based, not line-based — a body can span lines and the
112105
// blank line is often missing in a malformed echo.
@@ -136,7 +129,44 @@ const OTHER_FRAMED_CREDENTIALS: Array<[RegExp, string]> = [
136129
function maskOtherFramings(value: string): string {
137130
// Same union rule as the header pass: decoding may add coverage, never
138131
// remove it.
139-
return maskOtherFramingsOnce(maskOtherFramingsOnce(value, true), false);
132+
const decoded = maskOtherFramingsOnce(value, true);
133+
const decodedXml = maskXmlIdentifyingAttribute(decoded, true);
134+
const plain = maskOtherFramingsOnce(decodedXml, false);
135+
return maskXmlIdentifyingAttribute(plain, false);
136+
}
137+
138+
const XML_TAG_NAME = /<[^\S\r\n]*(?:[A-Za-z_][\w.-]*:)?[A-Za-z_][\w:.-]*/y;
139+
const XML_CREDENTIAL_ATTRIBUTE = new RegExp(
140+
`(?<![\\w:.-])(?:name|key|id)[^\\S]*=[^\\S]*["']?(?:${CREDENTIAL_HEADER_LABEL})["']?(?=[\\s/>])`,
141+
"i",
142+
);
143+
144+
/** Scan each tag once; a suffix-searching lookahead here makes unterminated tags quadratic. */
145+
function maskXmlIdentifyingAttribute(value: string, decodeEscapes: boolean): string {
146+
const { folded, map } = foldForMatching(value, decodeEscapes);
147+
let searchFrom = 0;
148+
while (searchFrom < folded.length) {
149+
const relativeStart = folded.indexOf("<", searchFrom);
150+
if (relativeStart === -1) return value;
151+
const tagStart = relativeStart;
152+
XML_TAG_NAME.lastIndex = tagStart;
153+
const tagName = XML_TAG_NAME.exec(folded);
154+
if (!tagName) {
155+
searchFrom = tagStart + 1;
156+
continue;
157+
}
158+
const close = folded.indexOf(">", tagStart + tagName[0].length);
159+
const tagEnd = close === -1 ? folded.length : close + 1;
160+
const attributes = folded.slice(tagStart + tagName[0].length, tagEnd);
161+
if (XML_CREDENTIAL_ATTRIBUTE.test(attributes)) {
162+
const start = map[tagStart] ?? value.length;
163+
const headEnd = map[tagStart + tagName[0].length] ?? value.length;
164+
return value.slice(0, start) + value.slice(start, headEnd) + REDACTED_SECRET;
165+
}
166+
if (close === -1) return value;
167+
searchFrom = tagEnd;
168+
}
169+
return value;
140170
}
141171

142172
function maskOtherFramingsOnce(value: string, decodeEscapes: boolean): string {

‎tests/lib/redact.test.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,13 @@ describe("redactSecretString", () => {
335335
.toBe('<field data-name="authorization">public-status</field>');
336336
});
337337

338+
test("many unterminated non-credential tags are scanned in bounded time", () => {
339+
const input = "<a ".repeat(8_000);
340+
const started = performance.now();
341+
expect(redactSecretString(input)).toBe(input);
342+
expect(performance.now() - started).toBeLessThan(1_000);
343+
});
344+
338345
test("a multipart credential part is masked through the rest of the body", () => {
339346
// Line-based masking left a multi-line body and the no-blank-line shape
340347
// partly intact, and stopping at the first `--` trusted an attacker-chosen

0 commit comments

Comments
 (0)