Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/tdf3/src/utils/unwrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

export function unwrapHtml(htmlPayload: Uint8Array): Uint8Array {
const html = new TextDecoder().decode(htmlPayload);
const payloadRe = /<input id=['"]?data-input['"]?[^>]*?value=['"]?([a-zA-Z0-9+/=]+)['"]?/;
const payloadRe =
/<input\s+[^>]*id=(?:['"]?)data-input(?:['"]?)[^>]*value=(?:['"]?)([a-zA-Z0-9+/=\-_]+)(?:['"]?)/;
const reResult = payloadRe.exec(html);
if (!reResult) {
throw new InvalidFileError('Payload is missing');
Expand Down
42 changes: 42 additions & 0 deletions lib/tests/mocha/unit/unwrap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,46 @@ describe('unwrapHtml', () => {
'There was a problem extracting the TDF3 payload'
);
});

describe('regex pattern variations', () => {
it('should handle double quotes', () => {
const htmlPayload = new TextEncoder().encode(
'<input id="data-input" type="hidden" value="SGVsbG8gV29ybGQ=">'
);
const result = unwrapHtml(htmlPayload);
expect(new TextDecoder().decode(result)).to.equal('Hello World');
});

it('should handle single quotes', () => {
const htmlPayload = new TextEncoder().encode(
"<input id='data-input' type='hidden' value='SGVsbG8gV29ybGQ='>"
);
const result = unwrapHtml(htmlPayload);
expect(new TextDecoder().decode(result)).to.equal('Hello World');
});

it('should handle no quotes', () => {
const htmlPayload = new TextEncoder().encode(
'<input id=data-input type=hidden value=SGVsbG8gV29ybGQ=>'
);
const result = unwrapHtml(htmlPayload);
expect(new TextDecoder().decode(result)).to.equal('Hello World');
});

it('should handle URL-safe base64 characters', () => {
const htmlPayload = new TextEncoder().encode(
'<input id="data-input" type="hidden" value="SGVsbG8tV29ybGQ_">'
);
const result = unwrapHtml(htmlPayload);
expect(new TextDecoder().decode(result)).to.equal('Hello-World?');
});

it('should handle additional attributes', () => {
const htmlPayload = new TextEncoder().encode(
'<input class="hidden" id="data-input" data-test="value" type="hidden" value="SGVsbG8gV29ybGQ=">'
);
const result = unwrapHtml(htmlPayload);
expect(new TextDecoder().decode(result)).to.equal('Hello World');
});
});
});
Loading