import { describe, it, expect } from 'vitest' import { assertHttpUrl } from '../src/main/url' import { isAllowedLoginUrl } from '../src/main/cookies' import { isYtdlpUpdateChannel } from '@shared/ipc' // --- F5: assertHttpUrl — argument-injection guard + normalisation ----------- describe('assertHttpUrl (audit F5)', () => { it('accepts http(s) URLs and returns the normalised href', () => { expect(assertHttpUrl('https://www.youtube.com/watch?v=abc')).toBe( 'https://www.youtube.com/watch?v=abc' ) // http with a bare host normalises to a trailing slash expect(assertHttpUrl('http://example.com')).toBe('http://example.com/') }) it('trims surrounding whitespace', () => { expect(assertHttpUrl(' https://example.com/v ')).toBe('https://example.com/v') }) it('strips interior tabs/newlines the URL parser tolerates (normalised output)', () => { // Returning the raw input would leak these through to argv / loadURL. expect(assertHttpUrl('https://exa\nmple.com/v')).toBe('https://example.com/v') const out = assertHttpUrl('https://example.com/a\tb') expect(out).not.toContain('\t') expect(out).not.toContain('\n') }) it('rejects non-http(s) protocols', () => { expect(() => assertHttpUrl('file:///C:/Windows/system32')).toThrow() expect(() => assertHttpUrl('javascript:alert(1)')).toThrow() expect(() => assertHttpUrl('ftp://example.com/x')).toThrow() expect(() => assertHttpUrl('aerofetch://download?url=x')).toThrow() }) it('rejects unparseable input', () => { expect(() => assertHttpUrl('not a url')).toThrow() expect(() => assertHttpUrl('')).toThrow() }) it('can never return a value that begins with "-" (would read as a yt-dlp flag)', () => { // A leading '-' cannot start a valid URL scheme, so it always throws — // the returned value therefore never opens with a dash. expect(() => assertHttpUrl('-https://example.com')).toThrow() }) }) // --- T4: isAllowedLoginUrl — sign-in window navigation confinement ---------- describe('isAllowedLoginUrl (audit T4)', () => { it('allows http(s) and about:blank', () => { expect(isAllowedLoginUrl('https://accounts.google.com/signin')).toBe(true) expect(isAllowedLoginUrl('http://example.com/login')).toBe(true) expect(isAllowedLoginUrl('about:blank')).toBe(true) }) it('blocks file://, the app protocol, and other external URI schemes', () => { expect(isAllowedLoginUrl('file:///C:/Windows/System32/calc.exe')).toBe(false) expect(isAllowedLoginUrl('aerofetch://download?url=https://evil.test')).toBe(false) expect(isAllowedLoginUrl('ms-settings:')).toBe(false) expect(isAllowedLoginUrl('mailto:x@y.z')).toBe(false) expect(isAllowedLoginUrl('javascript:alert(1)')).toBe(false) }) it('blocks unparseable input', () => { expect(isAllowedLoginUrl('not a url')).toBe(false) expect(isAllowedLoginUrl('')).toBe(false) }) }) // --- F1: isYtdlpUpdateChannel — --update-to allowlist ----------------------- describe('isYtdlpUpdateChannel (audit F1)', () => { it('accepts the two supported channels', () => { expect(isYtdlpUpdateChannel('stable')).toBe(true) expect(isYtdlpUpdateChannel('nightly')).toBe(true) }) it('rejects an arbitrary repository spec (the --update-to RCE vector)', () => { expect(isYtdlpUpdateChannel('evil/yt-dlp@latest')).toBe(false) expect(isYtdlpUpdateChannel('owner/repo')).toBe(false) }) it('rejects other yt-dlp channels not on AeroFetch’s allowlist', () => { expect(isYtdlpUpdateChannel('master')).toBe(false) }) it('rejects non-string / empty values', () => { expect(isYtdlpUpdateChannel('')).toBe(false) expect(isYtdlpUpdateChannel(undefined)).toBe(false) expect(isYtdlpUpdateChannel(null)).toBe(false) expect(isYtdlpUpdateChannel(42)).toBe(false) }) })