Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): validate XML during deserialization #5991

Merged
merged 1 commit into from
Apr 10, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 60 additions & 0 deletions packages/core/src/protocols/xml/parseXmlBody.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { SerdeContext } from "@smithy/types";
import { toUtf8 } from "@smithy/util-utf8";

import { parseXmlBody } from "./parseXmlBody";

describe(parseXmlBody.name, () => {
const context = {
utf8Encoder: toUtf8,
};
it("should parse xml", async () => {
const xml = Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>
<ListAllMyBucketsResult>
<Buckets>
<Bucket>
<CreationDate>timestamp</CreationDate>
<Name>string</Name>
</Bucket>
</Buckets>
<Owner>
<DisplayName>string</DisplayName>
<ID>string</ID>
</Owner>
</ListAllMyBucketsResult>
`);
const parsed = await parseXmlBody(xml, context as any as SerdeContext);
expect(parsed).toEqual({
Buckets: { Bucket: { CreationDate: "timestamp", Name: "string" } },
Owner: { DisplayName: "string", ID: "string" },
});
});

it("should throw on incomplete xml", async () => {
const xml = Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>
<ListAllMyBucketsResult>
<Buckets>
<Bucket>
<CreationDate>timestamp</CreationDate>
<Name>string</Name>
</Bucket>
</Buckets>
<Owner>
<DisplayName>string</DisplayName>
<ID>string</ID>
</Owner>
`);
const parsed = await parseXmlBody(xml, context as any as SerdeContext).catch((_) => _);
expect(parsed.toString()).toEqual(`Error: Unclosed tag 'ListAllMyBucketsResult'.:2:1`);
});

it("should throw on incomplete xml", async () => {
const xml = Buffer.from(`<?xml version="1.0" encoding="UTF-8"?>
<ListAllMyBucketsResult>
<Buckets>
<Bucket>
<CreationDate>timestamp</Creatio
`);
const parsed = await parseXmlBody(xml, context as any as SerdeContext).catch((_) => _);
expect(parsed.toString()).toEqual(`Error: Closing tag 'Creatio' doesn't have proper closing.:6:1`);
});
});
2 changes: 1 addition & 1 deletion packages/core/src/protocols/xml/parseXmlBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const parseXmlBody = (streamBody: any, context: SerdeContext): any =>

let parsedObj;
try {
parsedObj = parser.parse(encoded);
parsedObj = parser.parse(encoded, true);
} catch (e: any) {
if (e && typeof e === "object") {
Object.defineProperty(e, "$responseBodyText", {
Expand Down