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

BAU: Prevent evil domain substrings #974

Merged
merged 1 commit into from
Dec 18, 2024
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
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
Loading