fix(electron-utils): trim and normalize URLs in safeExternalUrl (#274)

Co-authored-by: Aniruddha Adak <aniruddhaadak80@users.noreply.github.com>
This commit is contained in:
ANIRUDDHA ADAK
2026-09-10 11:50:40 +08:00
committed by GitHub
co-authored by Aniruddha Adak
parent 12026fd21a
commit a5ed6f8cf0
2 changed files with 17 additions and 4 deletions
@@ -35,4 +35,13 @@ describe('safeExternalUrl', () => {
expect(safeExternalUrl('mailto:a@b.com', opts)).toBe('mailto:a@b.com')
expect(safeExternalUrl('http://example.com', opts)).toBeNull()
})
it('trims surrounding whitespace instead of returning it verbatim', () => {
// The WHATWG parser strips surrounding whitespace while parsing, so the
// untrimmed input validates but would fail in shell.openExternal.
expect(safeExternalUrl(' https://example.com/a?b=c ')).toBe('https://example.com/a?b=c')
expect(safeExternalUrl('\nhttp://example.com\n')).toBe('http://example.com')
expect(safeExternalUrl(' ')).toBeNull()
expect(safeExternalUrl(' file:///etc/passwd ')).toBeNull()
})
})