Skip to content

Commit

Permalink
fix(middleware-sdk-s3-control): ignore IP address in host prefix dedu…
Browse files Browse the repository at this point in the history
…pe (#4944)
  • Loading branch information
kuhe committed Jul 10, 2023
1 parent 86691eb commit 6dd73dd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions packages/middleware-sdk-s3-control/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@aws-sdk/middleware-bucket-endpoint": "*",
"@aws-sdk/types": "*",
"@aws-sdk/util-arn-parser": "*",
"@aws-sdk/util-endpoints": "*",
"@smithy/protocol-http": "^1.1.0",
"@smithy/types": "^1.1.0",
"tslib": "^2.5.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ describe(deduplicateHostPrefix.name, () => {

expect(deduplicateHostPrefix("12345.abcdefgh.12345.12345.host.com")).toEqual("12345.abcdefgh.12345.12345.host.com");
});

it("should not act on IP hostnames", () => {
expect(deduplicateHostPrefix("1.2.3.4")).toEqual("1.2.3.4");
expect(deduplicateHostPrefix("1.2.3.4:80")).toEqual("1.2.3.4:80");
expect(deduplicateHostPrefix("10.10.10.10")).toEqual("10.10.10.10");
expect(deduplicateHostPrefix("10.10.10.10:80")).toEqual("10.10.10.10:80");
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { isIpAddress } from "@aws-sdk/util-endpoints";

/**
* @example
* 12345.12345.____.com should become 12345.____.com.
*/
export const deduplicateHostPrefix = (hostname: string): string => {
const [prefix1, prefix2, ...rest] = hostname.split(".");
if (prefix1 === prefix2) {
return [prefix1, ...rest].join(".");
const [p1, p2, p3, p4, ...rest] = hostname.split(".");
if (isIpAddress(`${p1}.${p2}.${p3}.${parseInt(p4, 10)}`)) {
return hostname;
}
if (p1 === p2) {
return [p2, p3, p4, ...rest].join(".");
}
return hostname;
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ describe("middleware-sdk-s3-control", () => {
expect.hasAssertions();
});

it("doesn't dedupe host prefixes for IP addresses", async () => {
const client = new S3Control({
region: "snow",
endpoint: "https://10.10.10.10:8000/path?query=query",
useFipsEndpoint: false,
useDualstackEndpoint: false,
disableHostPrefix: true,
});

requireRequestsFrom(client).toMatch({
hostname: /^10.10.10.10$/,
port: /^8000$/,
});

await client.listAccessPoints({
AccountId: "123456789012",
Bucket: "my-bucket",
});

expect.hasAssertions();
});

it("parses outpost ARNs", async () => {
const client = new S3Control({
region: "us-west-2",
Expand Down
1 change: 1 addition & 0 deletions packages/util-endpoints/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./lib/aws/partition";
export * from "./lib/isIpAddress";
export * from "./resolveEndpoint";
export * from "./types";

0 comments on commit 6dd73dd

Please sign in to comment.