-
Notifications
You must be signed in to change notification settings - Fork 126
fix: improve validation and normalization for portfolio URLs #1950
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { describe, it, expect } from 'vitest'; | ||
| import { validateAndNormalizeUrl } from './urlValidation'; | ||
|
|
||
| describe('validateAndNormalizeUrl', () => { | ||
| it('returns empty string for null, undefined, or empty string', () => { | ||
| expect(validateAndNormalizeUrl(null)).toBe(''); | ||
| expect(validateAndNormalizeUrl(undefined)).toBe(''); | ||
| expect(validateAndNormalizeUrl('')).toBe(''); | ||
| expect(validateAndNormalizeUrl(' ')).toBe(''); | ||
| }); | ||
|
|
||
| it('prepends https:// if protocol is missing', () => { | ||
| expect(validateAndNormalizeUrl('github.com/username')).toBe('https://github.com/username'); | ||
| expect(validateAndNormalizeUrl('linkedin.com/in/username')).toBe('https://linkedin.com/in/username'); | ||
| expect(validateAndNormalizeUrl('example.com')).toBe('https://example.com/'); | ||
| }); | ||
|
|
||
| it('keeps http:// and https:// if present', () => { | ||
| expect(validateAndNormalizeUrl('https://github.com/username')).toBe('https://github.com/username'); | ||
| expect(validateAndNormalizeUrl('http://example.com')).toBe('http://example.com/'); | ||
| }); | ||
|
|
||
| it('returns empty string for invalid URLs', () => { | ||
| expect(validateAndNormalizeUrl('not a url')).toBe(''); | ||
| expect(validateAndNormalizeUrl('javascript:alert(1)')).toBe(''); | ||
| expect(validateAndNormalizeUrl('data:text/html,<html>')).toBe(''); | ||
| expect(validateAndNormalizeUrl('ftp://example.com')).toBe(''); | ||
|
Check failure on line 27 in src/utils/urlValidation.test.ts
|
||
| }); | ||
|
|
||
| it('checks for required domains if provided', () => { | ||
| expect(validateAndNormalizeUrl('https://github.com/user', 'github.com')).toBe('https://github.com/user'); | ||
| expect(validateAndNormalizeUrl('github.com/user', 'github.com')).toBe('https://github.com/user'); | ||
| expect(validateAndNormalizeUrl('https://example.com', 'github.com')).toBe(''); | ||
| expect(validateAndNormalizeUrl('linkedin.com/in/user', 'linkedin.com')).toBe('https://linkedin.com/in/user'); | ||
| expect(validateAndNormalizeUrl('https://notlinkedin.com/in/user', 'linkedin.com')).toBe(''); | ||
|
Check failure on line 35 in src/utils/urlValidation.test.ts
|
||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** | ||
| * Validates and normalizes URLs to ensure they are safe and correctly formatted. | ||
| * If a URL is missing a protocol, 'https://' is prepended. | ||
| * If the URL is invalid or uses an unsafe protocol (like javascript:), it returns an empty string. | ||
| * | ||
| * @param url - The input URL to validate and normalize | ||
| * @param requiredDomain - Optional domain string that the URL must include (e.g., 'github.com') | ||
| * @returns A normalized, safe URL string, or an empty string if invalid | ||
| */ | ||
| export const validateAndNormalizeUrl = (url: string | null | undefined, requiredDomain?: string): string => { | ||
| if (!url) return ''; | ||
|
|
||
| let trimmedUrl = url.trim(); | ||
|
|
||
| // If the user didn't provide a protocol, assume https | ||
| if (!/^https?:\/\//i.test(trimmedUrl)) { | ||
| trimmedUrl = `https://${trimmedUrl}`; | ||
| } | ||
|
kumudasrip marked this conversation as resolved.
|
||
|
|
||
| try { | ||
| const parsedUrl = new URL(trimmedUrl); | ||
|
|
||
| // Only allow http and https protocols | ||
| if (!['http:', 'https:'].includes(parsedUrl.protocol)) { | ||
| return ''; | ||
| } | ||
|
|
||
| // If a specific domain is required, verify it's part of the hostname | ||
| if (requiredDomain) { | ||
| if (!parsedUrl.hostname.toLowerCase().includes(requiredDomain.toLowerCase())) { | ||
| return ''; | ||
|
kumudasrip marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| return parsedUrl.toString(); | ||
| } catch (e) { | ||
| // URL parsing failed, meaning it's highly malformed | ||
| return ''; | ||
| } | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.