Skip to content

Commit

Permalink
Merge branch 'valid-local'
Browse files Browse the repository at this point in the history
  • Loading branch information
Uninen committed Jan 22, 2025
2 parents 1ae587c + 46e708d commit fd0a674
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.8.2 (2025-01-22)

- Fix: consider localhost as secure in `isValidSecureUrl`.

## 0.8.1 (2025-01-22)

- Fix: `getNext` now supports port numbers as well.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@slipmatio/toolbelt",
"type": "module",
"version": "0.8.1",
"version": "0.8.2",
"main": "dist/toolbelt.js",
"module": "dist/toolbelt.js",
"exports": {
Expand Down
7 changes: 5 additions & 2 deletions src/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,20 @@ export function isAllowedDomain(url: string, allowedDomains: string[]): boolean

/**
* Basic URL validation that checks if string can be parsed as URL
* Input must start with https://
*/
export function isValidSecureUrl(url: string) {
try {
const parsed = new URL(url)

if (parsed.hostname === 'localhost') {
return true
}

return parsed.protocol === 'https:'
} catch {
return false
}
}

/**
* Simple helper to prefetch images
* @param urls URL or array of URLs to prefetch
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/secure-urls.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { expect, test } from 'vitest'

import { isValidSecureUrl } from '@/browser'

test('isValidSecureUrl', () => {
expect(isValidSecureUrl('https://www.fi')).toBe(true)
expect(isValidSecureUrl('https://example.com/')).toBe(true)
expect(isValidSecureUrl('not-an-url.io')).toBe(false)

expect(isValidSecureUrl('https://next.slipmat.io/djuninen/')).toBe(true)

expect(isValidSecureUrl('http://localhost')).toBe(true)
expect(isValidSecureUrl('http://localhost:8000')).toBe(true)
expect(isValidSecureUrl('http://localhost:8000/login/')).toBe(true)
})
2 changes: 1 addition & 1 deletion tests/unit/type-helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect, test } from 'vitest'

import { isString } from '@/type-helpers'

test('entries', async () => {
test('entries', () => {
expect(isString('')).toBe(true)
expect(isString('foo')).toBe(true)
expect(isString([null])).toBe(false)
Expand Down

0 comments on commit fd0a674

Please sign in to comment.