-
Notifications
You must be signed in to change notification settings - Fork 2
Change DMARC reporting template #11
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
Merged
+251
−23
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { | ||
| WALL_OF_SHAME_DMARC_ISSUES_NEW, | ||
| buildWallOfShameDmarcIssueUrl, | ||
| formatWallOfShameDmarcRecordForUrl, | ||
| inferWallOfShameDmarcIssueType, | ||
| mxtoolboxDmarcLookupUrl, | ||
| WALL_OF_SHAME_DMARC_RECORD_URL_MAX, | ||
| } from '@/lib/wallOfShameDmarcIssue'; | ||
|
|
||
| describe('mxtoolboxDmarcLookupUrl', () => { | ||
| it('builds SuperTool action dmarc:<domain> with encoded action param', () => { | ||
| expect(mxtoolboxDmarcLookupUrl('example.com')).toBe( | ||
| 'https://mxtoolbox.com/SuperTool.aspx?action=dmarc%3Aexample.com', | ||
| ); | ||
| }); | ||
|
|
||
| it('encodes special characters in the domain', () => { | ||
| expect(mxtoolboxDmarcLookupUrl('bad host')).toContain( | ||
| encodeURIComponent('dmarc:bad host'), | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('inferWallOfShameDmarcIssueType', () => { | ||
| it('returns missing when there are no DMARC TXT records', () => { | ||
| expect(inferWallOfShameDmarcIssueType([])).toBe( | ||
| 'No DMARC record (missing)', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns missing when TXT is not DMARC-shaped', () => { | ||
| expect(inferWallOfShameDmarcIssueType(['v=spf1 ~all'])).toBe( | ||
| 'No DMARC record (missing)', | ||
| ); | ||
| }); | ||
|
|
||
| it('returns p=none option when policy is none', () => { | ||
| expect( | ||
| inferWallOfShameDmarcIssueType(['v=DMARC1; p=none; rua=mailto:a@b.co']), | ||
| ).toBe("DMARC policy set to 'none' (p=none)"); | ||
| }); | ||
|
|
||
| it('returns malformed when multiple DMARC records exist', () => { | ||
| expect( | ||
| inferWallOfShameDmarcIssueType([ | ||
| 'v=DMARC1; p=reject;', | ||
| 'v=DMARC1; p=none;', | ||
| ]), | ||
| ).toBe('Malformed / invalid DMARC record'); | ||
| }); | ||
|
|
||
| it('returns malformed when DMARC exists but policy tag is invalid', () => { | ||
| expect( | ||
| inferWallOfShameDmarcIssueType(['v=DMARC1; p=monitor;']), | ||
| ).toBe('Malformed / invalid DMARC record'); | ||
| }); | ||
|
|
||
| it('returns not a Wall of Shame issue for valid strict policies', () => { | ||
| expect( | ||
| inferWallOfShameDmarcIssueType(['v=DMARC1; p=reject;']), | ||
| ).toBe('Not a Wall of Shame issue (valid DMARC policy)'); | ||
| expect( | ||
| inferWallOfShameDmarcIssueType(['v=DMARC1; p=quarantine;']), | ||
| ).toBe('Not a Wall of Shame issue (valid DMARC policy)'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('formatWallOfShameDmarcRecordForUrl', () => { | ||
| it('returns empty string when there are no records', () => { | ||
| expect(formatWallOfShameDmarcRecordForUrl([])).toBe(''); | ||
| }); | ||
|
|
||
| it('returns empty when maxChars is non-positive (no unsafe slice)', () => { | ||
| const rec = 'v=DMARC1; p=none;'; | ||
| expect(formatWallOfShameDmarcRecordForUrl([rec], 0)).toBe(''); | ||
| expect(formatWallOfShameDmarcRecordForUrl([rec], -1)).toBe(''); | ||
| }); | ||
|
|
||
| it('truncates to a single ellipsis character when maxChars is 1', () => { | ||
| expect(formatWallOfShameDmarcRecordForUrl(['v=DMARC1;'], 1)).toBe('…'); | ||
| expect(formatWallOfShameDmarcRecordForUrl(['v=DMARC1;'], 1).length).toBe(1); | ||
| }); | ||
|
|
||
| it('returns the single record verbatim when within limit', () => { | ||
| const rec = 'v=DMARC1; p=none;'; | ||
| expect(formatWallOfShameDmarcRecordForUrl([rec])).toBe(rec); | ||
| }); | ||
|
|
||
| it('joins multiple records with a separator line', () => { | ||
| const a = 'v=DMARC1; p=reject;'; | ||
| const b = 'v=DMARC1; p=none;'; | ||
| expect(formatWallOfShameDmarcRecordForUrl([a, b])).toBe(`${a}\n---\n${b}`); | ||
| }); | ||
|
|
||
| it('truncates long records with an ellipsis suffix', () => { | ||
| const inner = 'x'.repeat(100); | ||
| const rec = `v=DMARC1; p=none; note=${inner}`; | ||
| const max = 40; | ||
| const out = formatWallOfShameDmarcRecordForUrl([rec], max); | ||
| expect(out.length).toBe(max); | ||
| expect(out.endsWith('…')).toBe(true); | ||
| expect(out.startsWith('v=DMARC1;')).toBe(true); | ||
| }); | ||
|
|
||
| it('respects default max length constant behaviour', () => { | ||
| const long = 'v=DMARC1; ' + 'z'.repeat(WALL_OF_SHAME_DMARC_RECORD_URL_MAX); | ||
| const out = formatWallOfShameDmarcRecordForUrl([long]); | ||
| expect(out.length).toBe(WALL_OF_SHAME_DMARC_RECORD_URL_MAX); | ||
| expect(out.endsWith('…')).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('buildWallOfShameDmarcIssueUrl', () => { | ||
| function parseIssueUrl(url: string): URLSearchParams { | ||
| const u = new URL(url); | ||
| expect(u.origin + u.pathname).toBe(WALL_OF_SHAME_DMARC_ISSUES_NEW); | ||
| return u.searchParams; | ||
| } | ||
|
|
||
| it('sets template, title, org, domain, issue type, lookup, and omits dmarc_record when absent', () => { | ||
| const url = buildWallOfShameDmarcIssueUrl( | ||
| 'ACME Corp', | ||
| 'example.com', | ||
| [], | ||
| ); | ||
| const q = parseIssueUrl(url); | ||
| expect(q.get('template')).toBe('dmarc_submission.yml'); | ||
| expect(q.get('title')).toBe('[DMARC] example.com'); | ||
| expect(q.get('org_name')).toBe('ACME Corp'); | ||
| expect(q.get('domain')).toBe('example.com'); | ||
| expect(q.get('issue_type')).toBe('No DMARC record (missing)'); | ||
| expect(q.has('dmarc_record')).toBe(false); | ||
| expect(q.get('lookup_url')).toBe(mxtoolboxDmarcLookupUrl('example.com')); | ||
| }); | ||
|
|
||
| it('includes dmarc_record when records exist', () => { | ||
| const rec = 'v=DMARC1; p=none;'; | ||
| const url = buildWallOfShameDmarcIssueUrl('Co', 'x.test', [rec]); | ||
| const q = parseIssueUrl(url); | ||
| expect(q.get('dmarc_record')).toBe(rec); | ||
| expect(q.get('issue_type')).toBe("DMARC policy set to 'none' (p=none)"); | ||
| }); | ||
|
|
||
| it('encodes organisation names with ampersands and preserves round-trip via URLSearchParams', () => { | ||
| const url = buildWallOfShameDmarcIssueUrl( | ||
| 'Foo & Bar Ltd', | ||
| 'brand.example', | ||
| [], | ||
| ); | ||
| const q = parseIssueUrl(url); | ||
| expect(q.get('org_name')).toBe('Foo & Bar Ltd'); | ||
| expect(url).toContain('org_name='); | ||
| }); | ||
|
|
||
| it('uses malformed issue type for multiple DMARC TXTs', () => { | ||
| const url = buildWallOfShameDmarcIssueUrl( | ||
| 'Co', | ||
| 'dup.example', | ||
| ['v=DMARC1; p=reject;', 'v=DMARC1; p=none;'], | ||
| ); | ||
| expect(parseIssueUrl(url).get('issue_type')).toBe( | ||
| 'Malformed / invalid DMARC record', | ||
| ); | ||
| expect(parseIssueUrl(url).get('dmarc_record')).toContain('\n---\n'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| import { analyzeDmarc } from '@/lib/parse/dmarc'; | ||
|
|
||
| export const WALL_OF_SHAME_DMARC_ISSUES_NEW = | ||
| 'https://github.com/jkerai1/DMARC-WallOfShame/issues/new'; | ||
|
|
||
| /** Max chars for DMARC TXT prefilled via URL (avoid GitHub URI limits). */ | ||
| export const WALL_OF_SHAME_DMARC_RECORD_URL_MAX = 3500; | ||
|
|
||
| function truncateForUrlSnippet(s: string, max: number): string { | ||
| if (max <= 0) return ''; | ||
| const t = s.trim(); | ||
| if (t.length <= max) return t; | ||
| return `${t.slice(0, max - 1)}…`; | ||
| } | ||
|
|
||
| /** MXToolbox SuperTool deep link (matches Wall of Shame issue template placeholder). */ | ||
| export function mxtoolboxDmarcLookupUrl(domain: string): string { | ||
| return `https://mxtoolbox.com/SuperTool.aspx?action=${encodeURIComponent(`dmarc:${domain}`)}`; | ||
| } | ||
|
|
||
| /** Maps analysed DMARC TXT to the Wall of Shame issue form dropdown value. */ | ||
| export function inferWallOfShameDmarcIssueType( | ||
| dmarcRecords: readonly string[], | ||
| ): string { | ||
| const a = analyzeDmarc([...dmarcRecords]); | ||
| if (a.multipleRecords) { | ||
| return 'Malformed / invalid DMARC record'; | ||
| } | ||
| if (!a.present) { | ||
| return 'No DMARC record (missing)'; | ||
| } | ||
| if (a.policy === 'none') { | ||
| return "DMARC policy set to 'none' (p=none)"; | ||
| } | ||
| if (a.policy === 'quarantine' || a.policy === 'reject') { | ||
| return 'Not a Wall of Shame issue (valid DMARC policy)'; | ||
| } | ||
| return 'Malformed / invalid DMARC record'; | ||
| } | ||
|
|
||
| /** DMARC TXT snippet for GitHub issue URL prefilling (may be empty). */ | ||
| export function formatWallOfShameDmarcRecordForUrl( | ||
| dmarcRecords: readonly string[], | ||
| maxChars: number = WALL_OF_SHAME_DMARC_RECORD_URL_MAX, | ||
| ): string { | ||
| if (maxChars <= 0) return ''; | ||
| if (!dmarcRecords.length) return ''; | ||
| const joined = | ||
| dmarcRecords.length === 1 | ||
| ? dmarcRecords[0] | ||
| : dmarcRecords.join('\n---\n'); | ||
| return truncateForUrlSnippet(joined, maxChars); | ||
| } | ||
|
|
||
| /** Builds the prefilled DMARC Wall of Shame GitHub issue form URL. */ | ||
| export function buildWallOfShameDmarcIssueUrl( | ||
| orgName: string, | ||
| domain: string, | ||
| dmarcRecords: readonly string[], | ||
| ): string { | ||
| const params = new URLSearchParams(); | ||
| params.set('template', 'dmarc_submission.yml'); | ||
| params.set('title', `[DMARC] ${domain}`); | ||
| params.set('org_name', orgName); | ||
| params.set('domain', domain); | ||
| params.set('issue_type', inferWallOfShameDmarcIssueType(dmarcRecords)); | ||
| const dmarcSnippet = formatWallOfShameDmarcRecordForUrl(dmarcRecords); | ||
| if (dmarcSnippet) { | ||
| params.set('dmarc_record', dmarcSnippet); | ||
| } | ||
| params.set('lookup_url', mxtoolboxDmarcLookupUrl(domain)); | ||
| return `${WALL_OF_SHAME_DMARC_ISSUES_NEW}?${params}`; | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.