Public shares: any valid Immich link opens through the proxy and is recorded in /admin, with a per-share sign-in-only switch; comments stay recipient-only

This commit is contained in:
zyphlar
2026-11-07 22:34:44 -08:00
parent d4ce4444b8
commit 17bf59b218
9 changed files with 368 additions and 42 deletions
+71 -1
View File
@@ -18,6 +18,9 @@ const KEY_A = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
const KEY_B = 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb'
const KEY_C = 'ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc'
const KEY_P = 'ppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp'
// In Immich but never added to the proxy: opened by their Immich links
const KEY_D = 'ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd'
const KEY_E = 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'
const ASSET_1 = '11111111-1111-4111-8111-111111111111'
const ASSET_2 = '22222222-2222-4222-8222-222222222222'
const ASSET_B = '33333333-3333-4333-8333-333333333333'
@@ -53,7 +56,9 @@ const links: Record<string, ReturnType<typeof link>> = {
[KEY_A]: link(KEY_A, 'Holiday', 'holiday', [ASSET_1, ASSET_2]),
[KEY_B]: link(KEY_B, 'Wedding', null, [ASSET_B]),
[KEY_C]: link(KEY_C, 'Garden', null, [ASSET_1]),
[KEY_P]: link(KEY_P, 'Private party', null, [ASSET_P])
[KEY_P]: link(KEY_P, 'Private party', null, [ASSET_P]),
[KEY_D]: link(KEY_D, 'Picnic', null, [ASSET_2]),
[KEY_E]: link(KEY_E, 'Beach', 'beach', [ASSET_1])
}
const PASSWORD = 'party-pw'
const IMMICH_TOKEN = 'immich-token-for-p'
@@ -459,3 +464,68 @@ describe('admin console', () => {
expect((await request(linkPath, { page: true })).res.status).toBe(401)
})
})
describe('public shares', () => {
it('an Immich link not seen before opens for anyone, without comments, and is recorded as public', async () => {
const page = await request('/share/' + KEY_D, { page: true })
expect(page.res.status).toBe(200)
expect(page.text).toContain('Picnic')
expect(page.text).not.toContain('/share/comments/')
expect(page.res.headers.get('vary')).toContain('Cookie')
expect(store.getShare(KEY_D)).toMatchObject({ title: 'Picnic', public: true, password: null })
const photo = await request('/share/photo/' + KEY_D + '/' + ASSET_2 + '/thumbnail')
expect(photo.res.status).toBe(200)
expect(photo.text).toBe('JPEG:' + ASSET_2)
// Another share's photo can't be pulled through this one
expect((await request('/share/photo/' + KEY_D + '/' + ASSET_1 + '/thumbnail')).res.status).toBe(404)
// Comments stay recipient-only
expect((await request('/share/comments/' + KEY_D + '/' + ASSET_2)).res.status).toBe(404)
expect((await request('/share/comments/' + KEY_D + '/' + ASSET_2, { json: { body: 'hi' } })).res.status).toBe(404)
})
it('opens by slug too, recording the share under its key', async () => {
expect((await request('/s/beach', { page: true })).res.status).toBe(200)
expect(store.getShare(KEY_E)).toMatchObject({ slug: 'beach', public: true })
})
it('refuses a key Immich does not know, and asks Immich about it only once', async () => {
const unknown = 'z'.repeat(67)
const before = immichCalls
expect((await request('/share/' + unknown, { page: true })).res.status).toBe(401)
expect((await request('/share/' + unknown, { page: true })).res.status).toBe(401)
expect(immichCalls - before).toBe(1)
expect(store.getShare(unknown)).toBeUndefined()
})
it('a recipient not given the share can view it, but not comment', async () => {
const cookie = await signIn()
const page = await request('/share/' + KEY_D, { page: true, cookie })
expect(page.res.status).toBe(200)
expect(page.text).not.toContain('/share/comments/')
expect((await request('/share/comments/' + KEY_D + '/' + ASSET_2, { cookie, json: { body: 'hi' } })).res.status).toBe(404)
})
it('the admin can switch a share to sign-in only, and back', async () => {
const login = await request('/admin/login', { form: { password: 'admin-secret' } })
const admin = cookieFrom(login.res, 'imp_admin')!
const dashboard = await request('/admin', { page: true, cookie: admin })
expect(dashboard.text).toContain('Picnic')
expect(dashboard.text).toContain('Public link works')
await request('/admin/shares/' + KEY_D + '/access', { cookie: admin, form: { public: '0' } })
expect(store.getShare(KEY_D)?.public).toBe(false)
expect((await request('/share/' + KEY_D, { page: true })).res.status).toBe(401)
expect((await request('/share/photo/' + KEY_D + '/' + ASSET_2 + '/thumbnail')).res.status).toBe(404)
expect((await request('/share/' + KEY_D, { page: true, cookie: await signIn() })).res.status).toBe(404)
await request('/admin/shares/' + KEY_D + '/access', { cookie: admin, form: { public: ['0', '1'] } })
expect(store.getShare(KEY_D)?.public).toBe(true)
expect((await request('/share/' + KEY_D, { page: true })).res.status).toBe(200)
})
it('needs the admin session to change access', async () => {
await request('/admin/shares/' + KEY_D + '/access', { form: { public: '0' } })
expect(store.getShare(KEY_D)?.public).toBe(true)
})
})
+52
View File
@@ -1,3 +1,7 @@
import { mkdtempSync, rmSync } from 'fs'
import { tmpdir } from 'os'
import { join } from 'path'
import { DatabaseSync } from 'node:sqlite'
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
import { Store } from '../src/magic/store'
import { hashToken, safeNextPath } from '../src/magic/tokens'
@@ -155,3 +159,51 @@ describe('safeNextPath', () => {
expect(safeNextPath(['/a'])).toBeUndefined()
})
})
describe('public shares', () => {
it('a share added by the admin requires sign-in unless marked public', () => {
expect(store.upsertShare({ key: 'k1', slug: null, title: 'A', password: null }, T0).public).toBe(false)
expect(store.upsertShare({ key: 'k2', slug: null, title: 'B', password: null, public: true }, T0).public).toBe(true)
})
it('saving a share again keeps its setting unless one is given', () => {
store.upsertShare({ key: 'k1', slug: null, title: 'A', password: null, public: true }, T0)
expect(store.upsertShare({ key: 'k1', slug: null, title: 'A2', password: null }, T1).public).toBe(true)
expect(store.upsertShare({ key: 'k1', slug: null, title: 'A2', password: null, public: false }, T1).public).toBe(false)
store.setSharePublic('k1', true)
expect(store.getShare('k1')?.public).toBe(true)
})
it('a discovered share starts public, and never overwrites one on file', () => {
expect(store.addDiscoveredShare({ key: 'k1', slug: 'one', title: 'Found' }, T0)).toMatchObject({ public: true, password: null })
store.upsertShare({ key: 'k2', slug: null, title: 'Mine', password: 'pw' }, T0)
expect(store.addDiscoveredShare({ key: 'k2', slug: null, title: 'Other' }, T1)).toMatchObject({ title: 'Mine', password: 'pw', public: false })
})
it('finds a share by key or slug, and lists the setting', () => {
store.addDiscoveredShare({ key: 'k1', slug: 'one', title: 'Found' }, T0)
expect(store.findShare('k1')?.key).toBe('k1')
expect(store.findShare('one')?.key).toBe('k1')
expect(store.findShare('nope')).toBeUndefined()
expect(store.listShares()[0].public).toBe(true)
})
it('upgrades a database from before the setting, keeping its shares sign-in only', () => {
const dir = mkdtempSync(join(tmpdir(), 'imp-store-'))
try {
const path = join(dir, 'magic.db')
const old = new DatabaseSync(path)
old.exec(`CREATE TABLE shares (key TEXT PRIMARY KEY, slug TEXT, title TEXT NOT NULL DEFAULT '', password TEXT,
last_scanned_at TEXT, created_at TEXT NOT NULL)`)
old.prepare('INSERT INTO shares (key, slug, title, password, created_at) VALUES (?, ?, ?, ?, ?)').run('old', null, 'Old', null, T0)
old.close()
const upgraded = new Store(path)
expect(upgraded.getShare('old')?.public).toBe(false)
upgraded.close()
// Opening it again doesn't try to add the column twice
new Store(path).close()
} finally {
rmSync(dir, { recursive: true, force: true })
}
})
})