You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.9 KiB
72 lines
2.9 KiB
import { describe, expect, it } from 'vitest'
|
|
import { resolve } from 'node:path'
|
|
import { allowedExternalUrl, isTrustedPage, isWithin, RateLimiter } from '@template/core'
|
|
import { preferencesSchema, fileTokenSchema } from '@template/contracts'
|
|
import { requestSchema } from 'electron-trpc/shared'
|
|
|
|
describe('security policy', () => {
|
|
it('matches exact origin and entry path; hashes may route internally', () => {
|
|
expect(
|
|
isTrustedPage(
|
|
'http://localhost:5173/index.html#/settings',
|
|
'http://localhost:5173/index.html'
|
|
)
|
|
).toBe(true)
|
|
for (const url of [
|
|
'http://localhost:5173.evil.test/index.html',
|
|
'http://localhost:5174/index.html',
|
|
'http://localhost:5173/other',
|
|
'https://localhost:5173/index.html',
|
|
'http://user:password@localhost:5173/index.html'
|
|
]) {
|
|
expect(isTrustedPage(url, 'http://localhost:5173/index.html')).toBe(false)
|
|
}
|
|
expect(isTrustedPage('app://desktop/index.html#/about', 'app://desktop/index.html')).toBe(true)
|
|
expect(isTrustedPage('app://attacker/index.html', 'app://desktop/index.html')).toBe(false)
|
|
})
|
|
|
|
it('contains paths without sibling-prefix or parent traversal escapes', () => {
|
|
const root = resolve('public')
|
|
expect(isWithin(root, resolve(root, 'assets', 'app.js'))).toBe(true)
|
|
expect(isWithin(root, `${root}-evil/file`)).toBe(false)
|
|
expect(isWithin(root, resolve(root, '..', 'secret'))).toBe(false)
|
|
})
|
|
|
|
it('allows only explicitly configured HTTPS hosts without credentials', () => {
|
|
const hosts = ['vuejs.org']
|
|
expect(allowedExternalUrl('https://vuejs.org/guide/', hosts)).toBe(true)
|
|
for (const url of [
|
|
'file:///etc/passwd',
|
|
'javascript:alert(1)',
|
|
'http://vuejs.org',
|
|
'https://vuejs.org.evil.test',
|
|
'https://user@vuejs.org'
|
|
])
|
|
expect(allowedExternalUrl(url, hosts)).toBe(false)
|
|
})
|
|
|
|
it('rate-limits independently and resets on expiry and revocation', () => {
|
|
let time = 0
|
|
const limiter = new RateLimiter(2, 100, () => time)
|
|
expect(limiter.allow('a')).toBe(true)
|
|
expect(limiter.allow('a')).toBe(true)
|
|
expect(limiter.allow('a')).toBe(false)
|
|
expect(limiter.allow('b')).toBe(true)
|
|
time = 100
|
|
expect(limiter.allow('a')).toBe(true)
|
|
limiter.remove('a')
|
|
expect(limiter.allow('a')).toBe(true)
|
|
})
|
|
|
|
it('rejects unknown methods, unsafe settings keys and arbitrary filesystem paths', () => {
|
|
expect(requestSchema.safeParse({ method: 'execute', input: 'rm' }).success).toBe(false)
|
|
expect(preferencesSchema.safeParse({ storagePath: '/tmp' }).success).toBe(false)
|
|
expect(preferencesSchema.safeParse({ theme: 'injected' }).success).toBe(false)
|
|
expect(fileTokenSchema.safeParse('/etc/passwd').success).toBe(false)
|
|
})
|
|
|
|
it('does not fill defaults into a partial preferences update', () => {
|
|
expect(preferencesSchema.parse({ theme: 'light' })).toEqual({ theme: 'light' })
|
|
expect(preferencesSchema.parse({ language: 'en' })).toEqual({ language: 'en' })
|
|
})
|
|
})
|
|
|