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
+22 -16
View File
@@ -25,7 +25,8 @@ import { ImageSize } from './types'
import { canDownload, uploadHealthcheck } from './share'
import { ASSET_VERSION } from './version'
import { resolveShare, resolveSharedAsset } from './resolve'
import { deny, loadRecipient, requireRecipient, requireShareAccess } from './magic/session'
import { deny, loadRecipient, requireRecipient } from './magic/session'
import { allowShareView, canViewShare } from './magic/access'
import { registerRecipientRoutes, registerSignInRoutes } from './magic/routes'
import { registerAdminRoutes } from './magic/admin'
import { getStore } from './magic/store'
@@ -34,9 +35,12 @@ import { getStore } from './magic/store'
* Build the Express app. Call `loadConfig` first: routes read config as they
* are registered.
*
* Every route that shows anything from Immich sits behind two gates: a
* magic-link session (`requireRecipient`), and that recipient having been
* given the specific share (`requireShareAccess`).
* Every route that shows anything from Immich is gated. A share's gallery,
* photos and downloads open for a recipient given that share, or for anyone
* when the share is public (`allowShareView`); an Immich link not seen before
* is checked with Immich and recorded as a public share. Everything else,
* comments included, needs a magic-link session (`requireRecipient`) and,
* for comments, a grant of that share (`requireShareAccess`).
*/
export function createApp (): Express {
const app = express()
@@ -89,14 +93,11 @@ export function createApp (): Express {
registerSignInRoutes(app)
registerAdminRoutes(app)
// ----- everything below needs a magic-link session --------------------------
app.use(requireRecipient)
registerRecipientRoutes(app)
// ----- shares: public ones for anyone, the rest for their recipients --------
/*
[ROUTE] The main URL a visitor opens from a shared link
*/
app.get('/:shareType(share|s)/:key/:mode(download)?', requireShareAccess, decodeCookie, asyncHandler(async (req, res) => {
app.get('/:shareType(share|s)/:key/:mode(download)?', allowShareView, decodeCookie, asyncHandler(async (req, res) => {
const keyType = getKeyTypeFromShare(req.params.shareType)
if (slugLinksDisabled(keyType)) {
@@ -113,13 +114,14 @@ export function createApp (): Express {
}))
/*
[ROUTE] Password unlock from the password page. Only reachable for a
share without a stored password, and only for a recipient of that share.
[ROUTE] Password unlock from the password page, for a visitor who may
view the share: a recipient of it (when no password is stored), or
anyone for a public share.
*/
app.post('/share/unlock', (req, res, next) => {
const key = req.body?.key
if (typeof key !== 'string' || !isKey(key) || !getStore().grantedShare(req.recipient!.id, key)) {
deny(req, res, 'Unlock for a share the recipient was not given')
if (typeof key !== 'string' || !isKey(key) || !canViewShare(req, key)) {
deny(req, res, 'Unlock for a share this visitor may not view')
return
}
next()
@@ -131,7 +133,7 @@ export function createApp (): Express {
Validates each ID against share.assets so the request can't pull anything
outside the share.
*/
app.post('/:shareType(share|s)/:key/download', requireShareAccess, decodeCookie, asyncHandler(async (req, res) => {
app.post('/:shareType(share|s)/:key/download', allowShareView, decodeCookie, asyncHandler(async (req, res) => {
const keyType = getKeyTypeFromShare(req.params.shareType)
let requestedIds: unknown
try {
@@ -177,7 +179,7 @@ export function createApp (): Express {
/*
[ROUTE] Direct link to a photo or video asset
*/
app.get('/share/:type(photo|video)/:key/:id/:size?', requireShareAccess, decodeCookie, asyncHandler(async (req, res) => {
app.get('/share/:type(photo|video)/:key/:id/:size?', allowShareView, decodeCookie, asyncHandler(async (req, res) => {
addResponseHeaders(res)
if (req.params.size && !Object.values(ImageSize).includes(req.params.size as ImageSize)) {
@@ -220,7 +222,7 @@ export function createApp (): Express {
validated against the share's asset set (defence in depth - Immich also
enforces this via the share key) before we fetch `GET /assets/:id`.
*/
app.get('/:shareType(share|s)/meta/:key/:id', requireShareAccess, decodeCookie, asyncHandler(async (req, res) => {
app.get('/:shareType(share|s)/meta/:key/:id', allowShareView, decodeCookie, asyncHandler(async (req, res) => {
addResponseHeaders(res)
const resolved = await resolveSharedAsset(req, getKeyTypeFromShare(req.params.shareType))
@@ -240,6 +242,10 @@ export function createApp (): Express {
res.json(buildAssetMetadata(detail, resolved.link))
}))
// ----- everything below needs a magic-link session --------------------------
app.use(requireRecipient)
registerRecipientRoutes(app)
/*
[ROUTE] Send a 404 for all other routes and methods. `all`, so a POST or DELETE to an
unknown path gets the same policy instead of Express's own HTML error page.