137 lines
5.0 KiB
TypeScript
137 lines
5.0 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import type { InternalAxiosRequestConfig } from 'axios'
|
|
import { createHash, createHmac } from 'node:crypto'
|
|
import { attachRequestSignature } from '@/utils/request-signature'
|
|
|
|
const buildConfig = (): InternalAxiosRequestConfig => ({
|
|
headers: {},
|
|
method: 'post',
|
|
baseURL: '/admin/api',
|
|
url: '/manager/planning/appointments',
|
|
params: { b: 2, a: 1 },
|
|
data: { name: 'test' }
|
|
} as InternalAxiosRequestConfig)
|
|
|
|
describe('request signature', () => {
|
|
beforeEach(() => {
|
|
window.localStorage.clear()
|
|
vi.restoreAllMocks()
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
it('adds device and signature headers for protected unsafe requests', async () => {
|
|
vi.spyOn(Date, 'now').mockReturnValue(1_789_999_999_000)
|
|
vi.spyOn(crypto, 'randomUUID').mockReturnValue('00000000-0000-4000-8000-000000000001')
|
|
vi.spyOn(crypto, 'getRandomValues').mockImplementation(array => {
|
|
const values = array as Uint32Array
|
|
values[0] = 10
|
|
values[1] = 20
|
|
return array
|
|
})
|
|
|
|
const config = await attachRequestSignature(buildConfig(), 'token-secret', { authFree: false })
|
|
|
|
expect(config.headers['X-Device-Id']).toBe('00000000-0000-4000-8000-000000000001')
|
|
expect(config.headers['X-Timestamp']).toBe('1789999999000')
|
|
expect(config.headers['X-Nonce']).toBe('1789999999000-00000000-0000-4000-8000-000000000001-ak')
|
|
expect(config.headers['X-Sign-Version']).toBe('v1')
|
|
expect(String(config.headers['X-Signature'])).toHaveLength(64)
|
|
})
|
|
|
|
it('signs unsafe requests when Web Crypto subtle is unavailable', async () => {
|
|
vi.spyOn(Date, 'now').mockReturnValue(1_789_999_999_000)
|
|
vi.stubGlobal('crypto', {
|
|
randomUUID: () => '00000000-0000-4000-8000-000000000001',
|
|
getRandomValues: (array: Uint32Array) => {
|
|
array[0] = 10
|
|
array[1] = 20
|
|
return array
|
|
}
|
|
})
|
|
|
|
const config = await attachRequestSignature(buildConfig(), 'token-secret', { authFree: false })
|
|
const payloadHash = createHash('sha256').update(JSON.stringify({ name: 'test' })).digest('hex')
|
|
const canonical = [
|
|
'POST',
|
|
'/admin/manager/planning/appointments',
|
|
'1789999999000',
|
|
'1789999999000-00000000-0000-4000-8000-000000000001-ak',
|
|
payloadHash
|
|
].join('\n')
|
|
const expectedSignature = createHmac('sha256', 'token-secret').update(canonical).digest('hex')
|
|
|
|
expect(config.headers['X-Signature']).toBe(expectedSignature)
|
|
})
|
|
|
|
it('hashes canonical parameters as payload when unsafe request has no json body', async () => {
|
|
vi.spyOn(Date, 'now').mockReturnValue(1_789_999_999_000)
|
|
vi.stubGlobal('crypto', {
|
|
randomUUID: () => '00000000-0000-4000-8000-000000000001',
|
|
getRandomValues: (array: Uint32Array) => {
|
|
array[0] = 10
|
|
array[1] = 20
|
|
return array
|
|
}
|
|
})
|
|
const config = await attachRequestSignature({
|
|
headers: {},
|
|
method: 'post',
|
|
baseURL: '/admin/api',
|
|
url: '/manager/example',
|
|
params: { b: 2, a: 1 },
|
|
data: undefined
|
|
} as InternalAxiosRequestConfig, 'token-secret', { authFree: false })
|
|
const payloadHash = createHash('sha256').update('a=1&b=2').digest('hex')
|
|
const canonical = [
|
|
'POST',
|
|
'/admin/manager/example',
|
|
'1789999999000',
|
|
'1789999999000-00000000-0000-4000-8000-000000000001-ak',
|
|
payloadHash
|
|
].join('\n')
|
|
const expectedSignature = createHmac('sha256', 'token-secret').update(canonical).digest('hex')
|
|
|
|
expect(config.headers['X-Signature']).toBe(expectedSignature)
|
|
})
|
|
|
|
it('signs user api requests with backend path instead of dev proxy prefix', async () => {
|
|
vi.spyOn(Date, 'now').mockReturnValue(1_789_999_999_000)
|
|
vi.stubGlobal('crypto', {
|
|
randomUUID: () => '00000000-0000-4000-8000-000000000001',
|
|
getRandomValues: (array: Uint32Array) => {
|
|
array[0] = 10
|
|
array[1] = 20
|
|
return array
|
|
}
|
|
})
|
|
const data = { nickname: '店务', signature: '正常保存' }
|
|
const config = await attachRequestSignature({
|
|
headers: {},
|
|
method: 'put',
|
|
baseURL: '/api',
|
|
url: '/user/info',
|
|
data
|
|
} as InternalAxiosRequestConfig, 'token-secret', { authFree: false })
|
|
const payloadHash = createHash('sha256').update(JSON.stringify(data)).digest('hex')
|
|
const canonical = [
|
|
'PUT',
|
|
'/user/info',
|
|
'1789999999000',
|
|
'1789999999000-00000000-0000-4000-8000-000000000001-ak',
|
|
payloadHash
|
|
].join('\n')
|
|
const expectedSignature = createHmac('sha256', 'token-secret').update(canonical).digest('hex')
|
|
|
|
expect(config.headers['X-Signature']).toBe(expectedSignature)
|
|
})
|
|
|
|
it('skips signature for auth-free requests but still attaches device id', async () => {
|
|
vi.spyOn(crypto, 'randomUUID').mockReturnValue('00000000-0000-4000-8000-000000000001')
|
|
|
|
const config = await attachRequestSignature(buildConfig(), 'token-secret', { authFree: true })
|
|
|
|
expect(config.headers['X-Device-Id']).toBe('00000000-0000-4000-8000-000000000001')
|
|
expect(config.headers['X-Signature']).toBeUndefined()
|
|
})
|
|
})
|