Skip to content

Commit

Permalink
BAU: Prevent evil domain substrings
Browse files Browse the repository at this point in the history
Previously, if slc.co.uk were on the allowlist notslc.co.uk would also be permitted
  • Loading branch information
ethanmills committed Dec 18, 2024
1 parent ed830c5 commit 73ccc6b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion express/src/lib/validators/allowed-email-domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const allowedEmailDomains = loadAllowedEmailDomains();
export default async function hasAllowedDomain(emailAddress: string): Promise<boolean> {
const domains = await allowedEmailDomains;
const emailDomain = getEmailDomain(emailAddress);
return domains.some(domain => emailDomain.endsWith(domain));
return domains.some(domain => emailDomain === domain || emailDomain.endsWith("." + domain));
}

function getEmailDomain(email: string) {
Expand Down
8 changes: 4 additions & 4 deletions express/tests/lib/validators/allowed-email-domains.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ describe("Verify email domains", () => {
it("Allow an email address ending with a domain on the allowed list", async () => {
expect(await hasAllowedDomain(`[email protected].${allowedDomain}`)).toBe(true);
});

it("Allow an email address ending with a subdomain on the allowed list", async () => {
expect(await hasAllowedDomain(`[email protected]${allowedSubDomain}`)).toBe(true);
});
});

describe("Reject invalid domains", () => {
Expand All @@ -36,5 +32,9 @@ describe("Verify email domains", () => {
it("Reject an email address not ending with a subdomain on the allowed list", async () => {
expect(await hasAllowedDomain(`[email protected].${allowedSubDomain}.subdomain`)).toBe(false);
});

it("Rejects an email address where the allowed domain is a substring but not a subdomain", async () => {
expect(await hasAllowedDomain(`[email protected]${allowedSubDomain}`)).toBe(false);
});
});
});
2 changes: 1 addition & 1 deletion express/tests/lib/validators/email-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import validateEmail from "../../../src/lib/validators/email-validator";

jest.mock("fs/promises", () => {
return {
readFile: jest.fn(() => `allowed.domain\n.gov.uk`)
readFile: jest.fn(() => `allowed.domain\ngov.uk`)
};
});

Expand Down

0 comments on commit 73ccc6b

Please sign in to comment.