Skip to content

Commit

Permalink
fix(util-endpoints): return null in parseArn for some empty elements (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Sep 13, 2022
1 parent bbb09f3 commit f8bc8b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
15 changes: 9 additions & 6 deletions packages/util-endpoints/src/lib/aws/parseArn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ describe(parseArn.name, () => {
expect(parseArn(input)).toEqual(outout);
});

it.each(["some:random:string:separated:by:colons", "arn:aws:too:short"])(
"returns null for invalid arn %s",
(input: string) => {
expect(parseArn(input)).toBeNull();
}
);
it.each([
"arn::s3:us-west-2:123456789012:accesspoint:myendpoint", // partition not present
"arn:aws::us-west-2:123456789012:accesspoint:myendpoint", // service not present
"arn:aws:s3:us-west-2:123456789012:", // resource ID not present
"some:random:string:separated:by:colons",
"arn:aws:too:short",
])("returns null for invalid arn %s", (input: string) => {
expect(parseArn(input)).toBeNull();
});
});
14 changes: 4 additions & 10 deletions packages/util-endpoints/src/lib/aws/parseArn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,11 @@ import { EndpointARN } from "@aws-sdk/types";
export const parseArn = (value: string): EndpointARN | null => {
const segments = value.split(":");

if (segments.length < 6 || segments[0] !== "arn") return null;
if (segments.length < 6) return null;

const [
,
//Skip "arn" literal
partition,
service,
region,
accountId,
...resourceId
] = segments;
const [arn, partition, service, region, accountId, ...resourceId] = segments;

if (arn !== "arn" || partition === "" || service === "" || resourceId[0] === "") return null;

return {
partition,
Expand Down

0 comments on commit f8bc8b3

Please sign in to comment.