Skip to content
Closed
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
19 changes: 19 additions & 0 deletions apps/server/src/lib/email-utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { describe, it, expect } from 'vitest';
import { getListUnsubscribeAction } from './email-utils';

describe('getListUnsubscribeAction', () => {
it('parses a valid http List-Unsubscribe URL', () => {
expect(
getListUnsubscribeAction({ listUnsubscribe: '<https://example.com/u?id=1>' }),
).toEqual({ type: 'get', url: 'https://example.com/u?id=1', host: 'example.com' });
});

it('returns null for an angle-bracketed value that is not a valid URL', () => {
// Regression: previously threw `TypeError: Invalid URL` because the primary
// `new URL(match[1])` path had no try/catch, unlike the fallback path.
expect(getListUnsubscribeAction({ listUnsubscribe: '<here>' })).toBeNull();
expect(
getListUnsubscribeAction({ listUnsubscribe: 'Click <here> to unsubscribe' }),
).toBeNull();
});
});
8 changes: 7 additions & 1 deletion apps/server/src/lib/email-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,13 @@ export const getListUnsubscribeAction = ({
}

// NOTE: List-Unsubscribe can contain multiple URLs, but the spec says to process the first one we can.
const url = new URL(match[1]);
let url: URL;
try {
url = new URL(match[1]);
} catch {
// The angle-bracket content is not a valid URL (malformed or placeholder header).
return null;
}

if (url.protocol.startsWith('http')) {
return processHttpUrl(url, listUnsubscribePost);
Expand Down