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:
@@ -0,0 +1,112 @@
|
||||
import { NextFunction, Request, Response } from 'express-serve-static-core'
|
||||
import { fetchSharedLink, getKeyTypeFromShare, isKey, KeyType, log, slugLinksDisabled, title, TtlLruCache } from '@ipp/core'
|
||||
import { getStore, ShareRecord } from './store'
|
||||
import { listShareAssets, scanShare } from './digest'
|
||||
import { deny, nowIso } from './session'
|
||||
|
||||
declare module 'express-serve-static-core' {
|
||||
interface Request {
|
||||
/** The share being viewed, whether by grant or because it is public. */
|
||||
viewedShare?: ShareRecord
|
||||
}
|
||||
}
|
||||
|
||||
// Keys and slugs Immich said don't exist, so a scanner trying random keys
|
||||
// can't make the proxy ask Immich about each one more than once in a while
|
||||
const unknownToImmich = new TtlLruCache<true>({ ttlMs: 10 * 60_000, max: 5000 })
|
||||
// One Immich lookup per new share, however many requests arrive at once
|
||||
const discovering = new Map<string, Promise<ShareRecord | undefined>>()
|
||||
|
||||
const SLUG = /^[A-Za-z0-9_-]{1,200}$/
|
||||
|
||||
/**
|
||||
* Look up a share this proxy hasn't seen in Immich, and record it if Immich
|
||||
* knows it. New shares start public: their link already works in Immich
|
||||
* itself, and the admin can switch any of them to sign-in only.
|
||||
*/
|
||||
async function discoverShare (keyOrSlug: string, keyType: KeyType): Promise<ShareRecord | undefined> {
|
||||
if (keyType === KeyType.key ? !isKey(keyOrSlug) : (slugLinksDisabled(keyType) || !SLUG.test(keyOrSlug))) return undefined
|
||||
const cacheKey = keyType + ':' + keyOrSlug
|
||||
if (unknownToImmich.get(cacheKey)) return undefined
|
||||
let pending = discovering.get(cacheKey)
|
||||
if (!pending) {
|
||||
pending = (async () => {
|
||||
const result = await fetchSharedLink(keyOrSlug, keyType)
|
||||
if (!result.valid) {
|
||||
unknownToImmich.set(cacheKey, true)
|
||||
return undefined
|
||||
}
|
||||
const store = getStore()
|
||||
if (result.link) {
|
||||
const share = store.addDiscoveredShare({ key: result.link.key, slug: result.link.slug || null, title: title(result.link) }, nowIso())
|
||||
log('Added share "' + share.title + '", opened by its Immich link')
|
||||
// Note what's there now, so recipients added later only hear about newer photos
|
||||
scanShare(store, share, listShareAssets, nowIso()).catch(() => false)
|
||||
return share
|
||||
}
|
||||
// Password-protected: Immich won't say more without the password. Record
|
||||
// it by key so the visitor gets the password page; a slug can't be
|
||||
// recorded, as only the key identifies the share.
|
||||
if (keyType !== KeyType.key) return undefined
|
||||
const share = store.addDiscoveredShare({ key: keyOrSlug, slug: null, title: 'Password-protected share' }, nowIso())
|
||||
log('Added a password-protected share, opened by its Immich link')
|
||||
return share
|
||||
})().finally(() => discovering.delete(cacheKey))
|
||||
discovering.set(cacheKey, pending)
|
||||
}
|
||||
return pending
|
||||
}
|
||||
|
||||
/**
|
||||
* Which share, if any, this visitor may view by `keyOrSlug`: one granted to
|
||||
* the signed-in recipient, or any public one. Never asks Immich.
|
||||
*/
|
||||
function viewableShare (req: Request, keyOrSlug: string): { share?: ShareRecord, granted: boolean, known: boolean } {
|
||||
const store = getStore()
|
||||
const granted = req.recipient ? store.grantedShare(req.recipient.id, keyOrSlug) : undefined
|
||||
if (granted) return { share: granted, granted: true, known: true }
|
||||
const share = store.findShare(keyOrSlug)
|
||||
return { share: share?.public ? share : undefined, granted: false, known: !!share }
|
||||
}
|
||||
|
||||
function admit (req: Request, res: Response, share: ShareRecord, granted: boolean) {
|
||||
req.viewedShare = share
|
||||
if (granted) {
|
||||
req.grantedShare = share
|
||||
// Recipients are never asked for a password the admin stored. Anonymous
|
||||
// visitors to a public share go through Immich's own password page.
|
||||
if (!req.password && share.password) req.password = share.password
|
||||
}
|
||||
// What a visitor sees depends on who they are, so never share a cached copy
|
||||
res.set('Vary', 'Cookie')
|
||||
}
|
||||
|
||||
/**
|
||||
* Gate for viewing a share (its gallery, photos, videos, downloads). Passes a
|
||||
* recipient given the share, or anyone for a public one. A share key or slug
|
||||
* this proxy hasn't seen is checked with Immich and, if real, recorded as a
|
||||
* public share. Comments are behind `requireShareAccess` instead, so they
|
||||
* stay recipient-only.
|
||||
*/
|
||||
export async function allowShareView (req: Request, res: Response, next: NextFunction) {
|
||||
try {
|
||||
const keyOrSlug = req.params.key
|
||||
if (typeof keyOrSlug !== 'string') return deny(req, res, 'No share in ' + req.path)
|
||||
let { share, granted, known } = viewableShare(req, keyOrSlug)
|
||||
if (!share && !known) {
|
||||
// Asset routes (/share/photo|video/...) only ever take keys
|
||||
const keyType = req.params.shareType ? getKeyTypeFromShare(req.params.shareType) : KeyType.key
|
||||
share = await discoverShare(keyOrSlug, keyType)
|
||||
}
|
||||
if (!share) return deny(req, res, 'No access to share ' + keyOrSlug)
|
||||
admit(req, res, share, granted)
|
||||
next()
|
||||
} catch (e) {
|
||||
next(e)
|
||||
}
|
||||
}
|
||||
|
||||
/** True if this visitor may view the share with this key; for the password unlock form. */
|
||||
export function canViewShare (req: Request, key: string): boolean {
|
||||
return !!viewableShare(req, key).share
|
||||
}
|
||||
Reference in New Issue
Block a user