feat: 为本地 HTTP API 增加 Token 鉴权与安全加固

- 使用 Electron safeStorage 加密存储并自动初始化 API Token
- 为 health 以外的接口增加 Bearer Token 鉴权
- 限制 CORS 仅允许可信本地 Origin
- 增加鉴权、Token rotation、safeStorage 和手动验收测试
This commit is contained in:
Wxw-Gu
2026-08-07 17:48:05 +08:00
parent 0c21008ec3
commit a73af3b5ad
33 changed files with 1328 additions and 130 deletions
@@ -105,6 +105,22 @@ describe('preload IPC contract', () => {
expect(api).not.toHaveProperty('send')
})
it('exposes only the intentional API token IPC operations', async () => {
const api = await loadApi()
invoke.mockResolvedValue({ available: true, hasToken: true, maskedToken: '••••' })
await api.apiTokenStatus()
expect(invoke).toHaveBeenLastCalledWith('api:tokenStatus')
await api.revealApiToken()
expect(invoke).toHaveBeenLastCalledWith('api:revealToken')
await api.copyApiToken()
expect(invoke).toHaveBeenLastCalledWith('api:copyToken')
await api.rotateApiToken()
expect(invoke).toHaveBeenLastCalledWith('api:rotateToken')
await api.copyLocalApiCurl({ endpointId: 'contact' })
expect(invoke).toHaveBeenLastCalledWith('api:copyCurl', { endpointId: 'contact' })
})
it('unsubscribes the same listener registered for native database changes', async () => {
const api = await loadApi()
const callback = vi.fn()