Update docs

This commit is contained in:
Alan Grainger
2026-10-07 18:04:57 +13:00
parent 7a9ff2e6ee
commit 172f247418
14 changed files with 243 additions and 37 deletions
+5 -1
View File
@@ -3,6 +3,7 @@ import cookieSession from 'cookie-session'
import dayjs from 'dayjs'
import { NextFunction, Request, Response } from 'express-serve-static-core'
import { decrypt, encrypt } from './encrypt'
import { isKey } from './immich/client'
import { log } from './utils/log'
import { toString } from './utils/text'
@@ -78,9 +79,12 @@ export function storeUnlock (req: Request, key: string, password: string) {
/**
* Receive an unlock request from the password page (`POST <basePath>/share/unlock`).
* Only a share-shaped key and a string password are stored; anything else is
* ignored, so a stray object never reaches the cookie or the Immich login.
*/
export function unlockHandler (req: Request, res: Response) {
if (req.body.key) storeUnlock(req, req.body.key, req.body.password)
const { key, password } = req.body ?? {}
if (typeof key === 'string' && isKey(key) && typeof password === 'string') storeUnlock(req, key, password)
res.send()
}
+59
View File
@@ -0,0 +1,59 @@
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import express from 'express'
import type { Server } from 'http'
import type { AddressInfo } from 'net'
import { sessionMiddleware, unlockHandler } from '../src/session'
/*
The unlock body is visitor input. Only a share-shaped key with a string
password is stored; anything else is dropped before it reaches the cookie,
so a stray object can never become `req.password` or a session key.
*/
describe('unlockHandler', () => {
let server: Server
let base: string
beforeAll(async () => {
const app = express()
app.use(sessionMiddleware({ name: 'test-session' }))
app.use(express.json())
app.post('/share/unlock', unlockHandler)
await new Promise<void>(resolve => {
server = app.listen(0, resolve)
})
base = 'http://127.0.0.1:' + (server.address() as AddressInfo).port
})
afterAll(() => server?.close())
async function unlock (body: unknown): Promise<Response> {
return fetch(base + '/share/unlock', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body)
})
}
it('stores a share-shaped key with a string password', async () => {
const res = await unlock({ key: 'abc-DEF_123', password: 'hunter2' })
expect(res.status).toBe(200)
expect(res.headers.get('set-cookie')).toContain('test-session=')
})
it('ignores a key that is not share-shaped', async () => {
for (const key of ['../../etc', '__proto__', 'a b', '', 42, { k: 1 }]) {
const res = await unlock({ key, password: 'hunter2' })
expect(res.status).toBe(200)
expect(res.headers.get('set-cookie')).toBeNull()
}
})
it('ignores a password that is not a string', async () => {
for (const password of [{ nested: true }, ['a'], 42, null, undefined]) {
const res = await unlock({ key: 'abc', password })
expect(res.status).toBe(200)
expect(res.headers.get('set-cookie')).toBeNull()
}
})
})