Skip to content

Commit 50002cf

Browse files
authored
feat(core): validate XML during deserialization (#5991)
1 parent 2c7f9ac commit 50002cf

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import type { SerdeContext } from "@smithy/types";
2+
import { toUtf8 } from "@smithy/util-utf8";
3+
4+
import { parseXmlBody } from "./parseXmlBody";
5+
6+
describe(parseXmlBody.name, () => {
7+
const context = {
8+
utf8Encoder: toUtf8,
9+
};
10+
it("should parse xml", async () => {
11+
const xml = Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>
12+
<ListAllMyBucketsResult>
13+
<Buckets>
14+
<Bucket>
15+
<CreationDate>timestamp</CreationDate>
16+
<Name>string</Name>
17+
</Bucket>
18+
</Buckets>
19+
<Owner>
20+
<DisplayName>string</DisplayName>
21+
<ID>string</ID>
22+
</Owner>
23+
</ListAllMyBucketsResult>
24+
`);
25+
const parsed = await parseXmlBody(xml, context as any as SerdeContext);
26+
expect(parsed).toEqual({
27+
Buckets: { Bucket: { CreationDate: "timestamp", Name: "string" } },
28+
Owner: { DisplayName: "string", ID: "string" },
29+
});
30+
});
31+
32+
it("should throw on incomplete xml", async () => {
33+
const xml = Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>
34+
<ListAllMyBucketsResult>
35+
<Buckets>
36+
<Bucket>
37+
<CreationDate>timestamp</CreationDate>
38+
<Name>string</Name>
39+
</Bucket>
40+
</Buckets>
41+
<Owner>
42+
<DisplayName>string</DisplayName>
43+
<ID>string</ID>
44+
</Owner>
45+
`);
46+
const parsed = await parseXmlBody(xml, context as any as SerdeContext).catch((_) => _);
47+
expect(parsed.toString()).toEqual(`Error: Unclosed tag 'ListAllMyBucketsResult'.:2:1`);
48+
});
49+
50+
it("should throw on incomplete xml", async () => {
51+
const xml = Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>
52+
<ListAllMyBucketsResult>
53+
<Buckets>
54+
<Bucket>
55+
<CreationDate>timestamp</Creatio
56+
`);
57+
const parsed = await parseXmlBody(xml, context as any as SerdeContext).catch((_) => _);
58+
expect(parsed.toString()).toEqual(`Error: Closing tag 'Creatio' doesn't have proper closing.:6:1`);
59+
});
60+
});

packages/core/src/protocols/xml/parseXmlBody.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const parseXmlBody = (streamBody: any, context: SerdeContext): any =>
2121

2222
let parsedObj;
2323
try {
24-
parsedObj = parser.parse(encoded);
24+
parsedObj = parser.parse(encoded, true);
2525
} catch (e: any) {
2626
if (e && typeof e === "object") {
2727
Object.defineProperty(e, "$responseBodyText", {

0 commit comments

Comments
 (0)