Skip to content

Commit

Permalink
fix: add region validation using DNS Host label regex (#1402)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Jul 28, 2020
1 parent 7839fc8 commit 88e96e6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
26 changes: 23 additions & 3 deletions packages/config-resolver/src/EndpointsConfig.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,29 @@ describe("EndpointsConfig", () => {
describe("throws error", () => {
const error = new Error("error");

it("if region throws error", () => {
region.mockRejectedValueOnce(error);
return expect(resolveEndpointsConfig(input).endpoint()).rejects.toStrictEqual(error);
describe("if region", () => {
it("throws error", () => {
region.mockRejectedValueOnce(error);
return expect(resolveEndpointsConfig(input).endpoint()).rejects.toStrictEqual(error);
});

it("is invalid", () => {
[
"",
"has_underscore",
"-starts-with-dash",
"ends-with-dash-",
"-starts-and-ends-with-dash-",
"-",
"c0nt@in$-$ymb01$",
"0123456789012345678901234567890123456789012345678901234567890123", // 64 characters
].forEach((invalidRegion) => {
region.mockResolvedValueOnce(invalidRegion);
return expect(resolveEndpointsConfig(input).endpoint()).rejects.toStrictEqual(
new Error("Invalid region in client config")
);
});
});
});

describe("if regionInfoProvider", () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/config-resolver/src/EndpointsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ const normalizeEndpoint = (input: EndpointsInputConfig & PreviouslyResolved): Pr
const getEndPointFromRegion = async (input: EndpointsInputConfig & PreviouslyResolved) => {
const { tls = true } = input;
const region = await input.region();

const dnsHostRegex = new RegExp(/^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]{0,61}[a-zA-Z0-9])$/);
if (!dnsHostRegex.test(region)) {
throw new Error("Invalid region in client config");
}

const { hostname } = (await input.regionInfoProvider(region)) ?? {};
if (!hostname) {
throw new Error("Cannot resolve hostname from client config");
}

return input.urlParser(`${tls ? "https:" : "http:"}//${hostname}`);
};

0 comments on commit 88e96e6

Please sign in to comment.