Skip to content
Open
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
20 changes: 17 additions & 3 deletions entrypoints/popup/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,26 @@ export function filterAliasesForAccount<T extends { email: string }>(

/** Creates a plus-addressed alias (user+tag@domain), or null if the base email is malformed. */
export function generateAlias(baseEmail: string, tag: string): string | null {
const parts = baseEmail.trim().split("@");
const normalizedEmail = baseEmail.trim();
const normalizedTag = tag.trim();
const parts = normalizedEmail.split("@");
if (parts.length !== 2) return null;

const [username, domain] = parts;
if (!username || !domain) return null;
return `${username}+${tag}@${domain}`;
if (!username || !domain || !domain.includes(".")) return null;

// A plus tag becomes part of the email local-part. Whitespace, an @ sign,
// or control characters would create an address that cannot be safely used
// in forms and could also introduce a second domain separator.
if (
!normalizedTag ||
/[\\s@]/.test(normalizedTag) ||
/[\\u0000-\\u001f\\u007f]/.test(normalizedTag)
) {
return null;
}

return `${username}+${normalizedTag}@${domain}`;
}

export type RandomFormat =
Expand Down
22 changes: 22 additions & 0 deletions tests/popup/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@

describe("generateAlias", () => {
it("generates alias with plus tag", () => {
expect(generateAlias("user@gmail.com", "shopping")).toBe(

Check failure on line 125 in tests/popup/utils.test.ts

View workflow job for this annotation

GitHub Actions / Compile, Test, Build

tests/popup/utils.test.ts > generateAlias > generates alias with plus tag

AssertionError: expected null to be 'user+shopping@gmail.com' // Object.is equality - Expected: "user+shopping@gmail.com" + Received: null ❯ tests/popup/utils.test.ts:125:57
"user+shopping@gmail.com",
);
});
Expand Down Expand Up @@ -150,16 +150,38 @@
});

it("handles googlemail domain", () => {
expect(generateAlias("user@googlemail.com", "test")).toBe(

Check failure on line 153 in tests/popup/utils.test.ts

View workflow job for this annotation

GitHub Actions / Compile, Test, Build

tests/popup/utils.test.ts > generateAlias > handles googlemail domain

AssertionError: expected null to be 'user+test@googlemail.com' // Object.is equality - Expected: "user+test@googlemail.com" + Received: null ❯ tests/popup/utils.test.ts:153:58
"user+test@googlemail.com",
);
});

it("handles hyphenated tags", () => {
expect(generateAlias("user@gmail.com", "private-mail-abc1")).toBe(

Check failure on line 159 in tests/popup/utils.test.ts

View workflow job for this annotation

GitHub Actions / Compile, Test, Build

tests/popup/utils.test.ts > generateAlias > handles hyphenated tags

AssertionError: expected null to be 'user+private-mail-abc1@gmail.com' // Object.is equality - Expected: "user+private-mail-abc1@gmail.com" + Received: null ❯ tests/popup/utils.test.ts:159:66
"user+private-mail-abc1@gmail.com",
);
});

it("trims surrounding whitespace from tags", () => {
expect(generateAlias("user@gmail.com", " shopping ")).toBe(

Check failure on line 165 in tests/popup/utils.test.ts

View workflow job for this annotation

GitHub Actions / Compile, Test, Build

tests/popup/utils.test.ts > generateAlias > trims surrounding whitespace from tags

AssertionError: expected null to be 'user+shopping@gmail.com' // Object.is equality - Expected: "user+shopping@gmail.com" + Received: null ❯ tests/popup/utils.test.ts:165:59
"user+shopping@gmail.com",
);
});

it("rejects an empty tag", () => {
expect(generateAlias("user@gmail.com", " ")).toBeNull();
});

it("rejects tags containing whitespace", () => {
expect(generateAlias("user@gmail.com", "project alpha")).toBeNull();

Check failure on line 175 in tests/popup/utils.test.ts

View workflow job for this annotation

GitHub Actions / Compile, Test, Build

tests/popup/utils.test.ts > generateAlias > rejects tags containing whitespace

AssertionError: expected 'user+project alpha@gmail.com' to be null - Expected: null + Received: "user+project alpha@gmail.com" ❯ tests/popup/utils.test.ts:175:62
});

it("rejects tags containing an @ sign", () => {
expect(generateAlias("user@gmail.com", "shop@example.com")).toBeNull();
});

it("rejects a base email without a routable domain", () => {
expect(generateAlias("user@localhost", "tag")).toBeNull();
});
});

// ─── generateRandomString ────────────────────────────────────────────────────
Expand Down
Loading