Fix password-share caching, slug gate, video probe and mobile tile tap (#119, #294)

This commit is contained in:
Alan Grainger
2026-10-05 09:39:14 +13:00
parent bbc23b6250
commit 588174f62e
7 changed files with 118 additions and 16 deletions
+14 -1
View File
@@ -169,11 +169,24 @@ html, body {
justify-content: center;
}
#gallery a:hover .tile-check,
#gallery.select-mode .tile-check {
opacity: 1;
}
/* Hover reveal only where hover is real: iOS spends a first tap on it #294 */
@media (hover: hover) {
#gallery a:hover .tile-check {
opacity: 1;
}
}
/* Touch outside select mode: no invisible tap target. Long-press selects */
@media (hover: none) {
#gallery:not(.select-mode) .tile-check {
pointer-events: none;
}
}
.tile-check svg {
width: 16px;
height: 16px;
+6 -3
View File
@@ -160,9 +160,12 @@ export async function gallery (res: Response, share: SharedLink, openItem?: numb
metaBase
}
// HTML gallery page cache time
const cacheTime = Math.max(0, getNumericConfigOption('ipp.gallery.cacheTime', 300))
res.header('Cache-Control', 'public, max-age=' + cacheTime)
// HTML gallery page cache time. A password-protected gallery keeps the
// no-store that handleShareRequest set.
if (!share.password) {
const cacheTime = Math.max(0, getNumericConfigOption('ipp.gallery.cacheTime', 300))
res.header('Cache-Control', 'public, max-age=' + cacheTime)
}
res.send(renderPage(h(Gallery, props)))
}
+7
View File
@@ -15,6 +15,13 @@ export function addResponseHeaders (res: Response): void {
})
}
/** Stop browsers and shared caches storing a response from a password-protected share. */
export function addNoStoreHeaders (res: Response): void {
res.set('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate')
res.set('Pragma', 'no-cache')
res.set('Expires', '0')
}
/**
* Wrap an async route handler so a rejected promise is passed to Express's
* error chain (and on to `errorHandler`).
+9 -9
View File
@@ -13,7 +13,7 @@ import {
} from './types'
import dayjs from 'dayjs'
import { getConfigOption } from './config/access'
import { addResponseHeaders } from './http'
import { addNoStoreHeaders, addResponseHeaders } from './http'
import { canDownload } from './share'
import { log } from './utils/log'
import { assetBuffer } from './stream/asset'
@@ -162,11 +162,7 @@ export async function handleShareRequest (req: IncomingShareRequest, res: Respon
}
// Don't cache password-protected albums
if (sharedLinkRes.passwordRequired || req.password) {
res.header('Cache-Control', 'no-store, no-cache, must-revalidate, proxy-revalidate')
res.header('Pragma', 'no-cache')
res.header('Expires', '0')
}
if (sharedLinkRes.passwordRequired || req.password) addNoStoreHeaders(res)
// Password required - show the visitor the password page
if (sharedLinkRes.passwordRequired) {
@@ -495,14 +491,18 @@ export function fetchAssetDetail (asset: Asset): Promise<Asset | undefined> {
}
/**
* Get the content-type of a video, for the lightbox <video> element
* Get the content-type of a video, for the lightbox <video> element. Undefined
* when Immich can't serve it, so one missing video doesn't break the share #119
*/
export async function getVideoContentType (asset: Asset) {
export async function getVideoContentType (asset: Asset): Promise<string | undefined> {
const headers = await authHeadersForAsset(asset)
const data = await request(buildUrl('/assets/' + encodeURIComponent(asset.id) + '/video/playback', {
[asset.keyType]: asset.key
}), { headers })
return data.headers.get('Content-Type')
if (!(data instanceof globalThis.Response)) return undefined
// Only the header is needed; an unread body keeps the whole video download open
await data.body?.cancel()
return data.headers.get('Content-Type') || undefined
}
/**
+13 -3
View File
@@ -22,7 +22,7 @@ import { NextFunction, Request, Response } from 'express-serve-static-core'
import { Asset, AssetType, ImageSize, KeyType, SharedLink } from './types'
import { getConfigOption } from './config/access'
import { loadConfig } from './config/loader'
import { addResponseHeaders, asyncHandler, errorHandler } from './http'
import { addNoStoreHeaders, addResponseHeaders, asyncHandler, errorHandler } from './http'
import { canDownload, findMotionPhotoStill } from './share'
import { toString } from './utils/text'
import { decrypt, encrypt } from './encrypt'
@@ -104,7 +104,15 @@ type SharedAssetResolution =
| { ok: true, link: SharedLink, asset: Asset }
| { ok: false, status: number, reason: string, passwordRequired?: boolean }
/** Whether this is a slug link and the operator has turned slug links off. */
function slugLinksDisabled (keyType: KeyType): boolean {
return keyType === KeyType.slug && !getConfigOption('ipp.allowSlugLinks', true)
}
async function resolveShare (req: Request, keyType: KeyType): Promise<ShareResolution> {
if (slugLinksDisabled(keyType)) {
return { ok: false, status: 404, reason: 'Slug links are disabled in config.json' }
}
if (!isKey(req.params.key)) {
return { ok: false, status: 404, reason: 'Invalid key for ' + req.path }
}
@@ -165,8 +173,7 @@ app.get(/^(|\/share)\/healthcheck$/, asyncHandler(async (_req, res) => {
app.get('/:shareType(share|s)/:key/:mode(download)?', decodeCookie, asyncHandler(async (req, res) => {
const keyType = getKeyTypeFromShare(req.params.shareType)
if (keyType === KeyType.slug && !getConfigOption('ipp.allowSlugLinks', true)) {
// Slug type links are not allowed
if (slugLinksDisabled(keyType)) {
respondToInvalidRequest(res, 404, 'Slug links are disabled in config.json')
} else {
await handleShareRequest({
@@ -275,6 +282,7 @@ app.get('/share/:type(photo|video)/:key/:id/:size?', decodeCookie, asyncHandler(
respondToInvalidRequest(res, resolved.status, resolved.reason)
return
}
if (resolved.link.password) addNoStoreHeaders(res)
const asset: Asset = {
...resolved.asset,
type: req.params.type === 'video' ? AssetType.video : resolved.asset.type
@@ -306,6 +314,8 @@ app.get('/:shareType(share|s)/meta/:key/:id', decodeCookie, asyncHandler(async (
return
}
if (resolved.link.password) addNoStoreHeaders(res)
const detail = await fetchAssetDetail(resolved.asset)
if (!detail) {
respondToInvalidRequest(res, 404, 'Asset detail unavailable for ' + req.params.id)
+46
View File
@@ -0,0 +1,46 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { getVideoContentType } from '../src/immich'
import { Asset, AssetType, KeyType } from '../src/types'
const video: Asset = {
id: 'v1',
key: 'testkey',
keyType: KeyType.key,
type: AssetType.video,
isTrashed: false
}
describe('getVideoContentType', () => {
beforeEach(() => {
process.env.IMMICH_URL = 'http://immich.test'
vi.spyOn(console, 'log').mockImplementation(() => {})
})
afterEach(() => {
vi.unstubAllGlobals()
vi.restoreAllMocks()
})
it('returns the content-type and cancels the unread video body', async () => {
let cancelled = false
const body = new ReadableStream({
pull (controller) { controller.enqueue(new Uint8Array(1024)) },
cancel () { cancelled = true }
})
vi.stubGlobal('fetch', vi.fn(async () => new globalThis.Response(body, {
status: 200,
headers: { 'content-type': 'video/mp4' }
})))
expect(await getVideoContentType(video)).toBe('video/mp4')
expect(cancelled).toBe(true)
})
it('returns undefined when Immich cannot serve the video #119', async () => {
vi.stubGlobal('fetch', vi.fn(async () => new globalThis.Response('{"message":"Not found"}', { status: 404 })))
expect(await getVideoContentType(video)).toBeUndefined()
})
it('returns undefined when Immich is unreachable', async () => {
vi.stubGlobal('fetch', vi.fn(async () => { throw new TypeError('fetch failed') }))
expect(await getVideoContentType(video)).toBeUndefined()
})
})