Skip to content

Commit

Permalink
Guard against bad default frontends (#61)
Browse files Browse the repository at this point in the history
* Guard against bad default frontends

* review update
  • Loading branch information
grod220 authored Jun 26, 2024
1 parent 631e94e commit 5d126c7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
31 changes: 31 additions & 0 deletions apps/extension/src/shared/utils/is-valid-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, it, expect } from 'vitest';
import { isValidUrl } from './is-valid-url';

describe('isValidUrl', () => {
it('should return true for valid http URLs', () => {
expect(isValidUrl('http://example.com')).toBe(true);
expect(isValidUrl('http://www.example.com')).toBe(true);
expect(isValidUrl('http://example.com/path')).toBe(true);
expect(isValidUrl('http://example.com?q=query')).toBe(true);
});

it('should return true for valid https URLs', () => {
expect(isValidUrl('https://example.com')).toBe(true);
expect(isValidUrl('https://www.example.com')).toBe(true);
expect(isValidUrl('https://example.com/path')).toBe(true);
expect(isValidUrl('https://example.com?q=query')).toBe(true);
});

it('should return false for URLs with unsupported protocols', () => {
expect(isValidUrl('ftp://example.com')).toBe(false);
expect(isValidUrl('mailto:[email protected]')).toBe(false);
expect(isValidUrl('chrome-extension://lkpmkhpnhknhmibgnmmhdhgdilepfghe/page.html')).toBe(false);
});

it('should return false for strings that are not URLs', () => {
expect(isValidUrl('justastring')).toBe(false);
expect(isValidUrl('www.example.com')).toBe(false);
expect(isValidUrl('12345')).toBe(false);
expect(isValidUrl('')).toBe(false);
});
});
7 changes: 5 additions & 2 deletions apps/extension/src/shared/utils/is-valid-url.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
export const isValidUrl = (url: string) => {
try {
new URL(url);
return true;
const proposedUrl = new URL(url);

// Security measure: allows us to guard against someone being deceived
// into adding a url with chrome-extension:// in it with intentions to inject code
return ['https:', 'http:'].includes(proposedUrl.protocol);
} catch {
return false;
}
Expand Down

0 comments on commit 5d126c7

Please sign in to comment.