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

fix: add region validation using DNS Host label regex #1402

Merged
merged 1 commit into from
Jul 28, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`);
};