Skip to content

Commit 4f064f7

Browse files
trivikrsrchase
authored andcommitted
fix: add region validation using DNS Host label regex (#1402)
1 parent 17c5203 commit 4f064f7

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

packages/config-resolver/src/EndpointsConfig.spec.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,29 @@ describe("EndpointsConfig", () => {
8585
describe("throws error", () => {
8686
const error = new Error("error");
8787

88-
it("if region throws error", () => {
89-
region.mockRejectedValueOnce(error);
90-
return expect(resolveEndpointsConfig(input).endpoint()).rejects.toStrictEqual(error);
88+
describe("if region", () => {
89+
it("throws error", () => {
90+
region.mockRejectedValueOnce(error);
91+
return expect(resolveEndpointsConfig(input).endpoint()).rejects.toStrictEqual(error);
92+
});
93+
94+
it("is invalid", () => {
95+
[
96+
"",
97+
"has_underscore",
98+
"-starts-with-dash",
99+
"ends-with-dash-",
100+
"-starts-and-ends-with-dash-",
101+
"-",
102+
"c0nt@in$-$ymb01$",
103+
"0123456789012345678901234567890123456789012345678901234567890123", // 64 characters
104+
].forEach((invalidRegion) => {
105+
region.mockResolvedValueOnce(invalidRegion);
106+
return expect(resolveEndpointsConfig(input).endpoint()).rejects.toStrictEqual(
107+
new Error("Invalid region in client config")
108+
);
109+
});
110+
});
91111
});
92112

93113
describe("if regionInfoProvider", () => {

packages/config-resolver/src/EndpointsConfig.ts

+7
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,16 @@ const normalizeEndpoint = (input: EndpointsInputConfig & PreviouslyResolved): Pr
4545
const getEndPointFromRegion = async (input: EndpointsInputConfig & PreviouslyResolved) => {
4646
const { tls = true } = input;
4747
const region = await input.region();
48+
49+
const dnsHostRegex = new RegExp(/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9])$/);
50+
if (!dnsHostRegex.test(region)) {
51+
throw new Error("Invalid region in client config");
52+
}
53+
4854
const { hostname } = (await input.regionInfoProvider(region)) ?? {};
4955
if (!hostname) {
5056
throw new Error("Cannot resolve hostname from client config");
5157
}
58+
5259
return input.urlParser(`${tls ? "https:" : "http:"}//${hostname}`);
5360
};

0 commit comments

Comments
 (0)