Skip to content

Commit c1f7f08

Browse files
committed
regex DoS improvements and testing
1 parent 6f28cbd commit c1f7f08

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

lib/tdf3/src/utils/unwrap.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { InvalidFileError } from '../../../src/errors.js';
33

44
export function unwrapHtml(htmlPayload: Uint8Array): Uint8Array {
55
const html = new TextDecoder().decode(htmlPayload);
6-
const payloadRe = /<input id=['"]?data-input['"]?[^>]*?value=['"]?([a-zA-Z0-9+/=\-_]+?)['"]?/;
6+
const payloadRe =
7+
/<input\s+[^>]*id=(?:['"]?)data-input(?:['"]?)[^>]*value=(?:['"]?)([a-zA-Z0-9+/=\-_]+)(?:['"]?)/;
78
const reResult = payloadRe.exec(html);
89
if (!reResult) {
910
throw new InvalidFileError('Payload is missing');

lib/tests/mocha/unit/unwrap.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,46 @@ describe('unwrapHtml', () => {
2525
'There was a problem extracting the TDF3 payload'
2626
);
2727
});
28+
29+
describe('regex pattern variations', () => {
30+
it('should handle double quotes', () => {
31+
const htmlPayload = new TextEncoder().encode(
32+
'<input id="data-input" type="hidden" value="SGVsbG8gV29ybGQ=">'
33+
);
34+
const result = unwrapHtml(htmlPayload);
35+
expect(new TextDecoder().decode(result)).to.equal('Hello World');
36+
});
37+
38+
it('should handle single quotes', () => {
39+
const htmlPayload = new TextEncoder().encode(
40+
"<input id='data-input' type='hidden' value='SGVsbG8gV29ybGQ='>"
41+
);
42+
const result = unwrapHtml(htmlPayload);
43+
expect(new TextDecoder().decode(result)).to.equal('Hello World');
44+
});
45+
46+
it('should handle no quotes', () => {
47+
const htmlPayload = new TextEncoder().encode(
48+
'<input id=data-input type=hidden value=SGVsbG8gV29ybGQ=>'
49+
);
50+
const result = unwrapHtml(htmlPayload);
51+
expect(new TextDecoder().decode(result)).to.equal('Hello World');
52+
});
53+
54+
it('should handle URL-safe base64 characters', () => {
55+
const htmlPayload = new TextEncoder().encode(
56+
'<input id="data-input" type="hidden" value="SGVsbG8tV29ybGQ_">'
57+
);
58+
const result = unwrapHtml(htmlPayload);
59+
expect(new TextDecoder().decode(result)).to.equal('Hello-World?');
60+
});
61+
62+
it('should handle additional attributes', () => {
63+
const htmlPayload = new TextEncoder().encode(
64+
'<input class="hidden" id="data-input" data-test="value" type="hidden" value="SGVsbG8gV29ybGQ=">'
65+
);
66+
const result = unwrapHtml(htmlPayload);
67+
expect(new TextDecoder().decode(result)).to.equal('Hello World');
68+
});
69+
});
2870
});

0 commit comments

Comments
 (0)