diff --git a/app/package.json b/app/package.json index 501c4b1..63b7219 100644 --- a/app/package.json +++ b/app/package.json @@ -1,6 +1,6 @@ { "name": "immich-public-proxy", - "version": "1.5.2", + "version": "1.5.3", "scripts": { "dev": "ts-node src/index.ts", "build": "npx tsc", diff --git a/app/src/immich.ts b/app/src/immich.ts index f6818b6..fe4b997 100644 --- a/app/src/immich.ts +++ b/app/src/immich.ts @@ -136,6 +136,7 @@ class Immich { if (res.status === 200) { // Normal response - get the shared assets link = jsonBody as SharedLink + link.password = password if (link.expiresAt && dayjs(link.expiresAt) < dayjs()) { // This link has expired log('Expired link ' + key) diff --git a/app/src/index.ts b/app/src/index.ts index c59af30..3875d52 100644 --- a/app/src/index.ts +++ b/app/src/index.ts @@ -2,10 +2,17 @@ import express from 'express' import immich from './immich' import render from './render' import dayjs from 'dayjs' +import { Request, Response, NextFunction } from 'express-serve-static-core' import { AssetType, ImageSize } from './types' import { decrypt } from './encrypt' import { log, toString, addResponseHeaders } from './functions' +declare module 'express-serve-static-core' { + interface Request { + password?: string; + } +} + require('dotenv').config() const app = express() @@ -18,6 +25,26 @@ app.use('/share/static', express.static('public', { setHeaders: addResponseHeade // Serve the same assets on /, to allow for /robots.txt and /favicon.ico app.use(express.static('public', { setHeaders: addResponseHeaders })) +const checkPassword = (req: Request, res: Response, next: NextFunction) => { + if (req.query?.cr && req.query?.iv) { + try { + const payload = JSON.parse(decrypt({ + iv: toString(req.query.iv), + cr: toString(req.query.cr) + })) + if (payload?.expires && dayjs(payload.expires) > dayjs()) { + req.password = payload.password + } else { + log(`Attempted to load assets from ${req.params.key} with an expired decryption token`) + // Send 404 rather than 401 so as not to provide information to an attacker that there is "something" at this path + res.status(404).send() + return + } + } catch (e) { } + } + next() +} + /* * [ROUTE] Healthcheck * The path matches for /share/healthcheck, and also the legacy /healthcheck @@ -33,10 +60,11 @@ app.get(/^(|\/share)\/healthcheck$/, async (_req, res) => { /* * [ROUTE] This is the main URL that someone would visit if they are opening a shared link */ -app.get('/share/:key/:mode(download)?', async (req, res) => { +app.get('/share/:key/:mode(download)?', checkPassword, async (req, res) => { await immich.handleShareRequest({ key: req.params.key, - mode: req.params.mode + mode: req.params.mode, + password: req.password }, res) }) @@ -53,7 +81,7 @@ app.post('/share/unlock', async (req, res) => { /* * [ROUTE] This is the direct link to a photo or video asset */ -app.get('/share/:type(photo|video)/:key/:id/:size?', async (req, res) => { +app.get('/share/:type(photo|video)/:key/:id/:size?', checkPassword, async (req, res) => { // Add the headers configured in config.json (most likely `cache-control`) addResponseHeaders(res) @@ -71,28 +99,9 @@ app.get('/share/:type(photo|video)/:key/:id/:size?', async (req, res) => { return } - // Validate the password payload, if one was provided - let password - if (req.query?.cr && req.query?.iv) { - try { - const payload = JSON.parse(decrypt({ - iv: toString(req.query.iv), - cr: toString(req.query.cr) - })) - if (payload?.expires && dayjs(payload.expires) > dayjs()) { - password = payload.password - } else { - log(`Attempted to load assets from ${req.params.key} with an expired decryption token`) - // Send 404 rather than 401 so as not to provide information to an attacker that there is "something" at this path - res.status(404).send() - return - } - } catch (e) { } - } - // Fetch the shared link information from Immich, so we can check to make sure that the requested asset // is allowed by this shared link. - const sharedLink = (await immich.getShareByKey(req.params.key, password))?.link + const sharedLink = (await immich.getShareByKey(req.params.key, req.password))?.link const request = { key: req.params.key, range: req.headers.range || '' diff --git a/app/src/render.ts b/app/src/render.ts index 677fdea..0570993 100644 --- a/app/src/render.ts +++ b/app/src/render.ts @@ -3,6 +3,7 @@ import { Response } from 'express-serve-static-core' import { Asset, AssetType, ImageSize, IncomingShareRequest, SharedLink } from './types' import { getConfigOption } from './functions' import archiver from 'archiver' +import dayjs from 'dayjs' class Render { lgConfig @@ -121,6 +122,7 @@ class Render { title: this.title(share), path: '/share/' + share.key, showDownload: getConfigOption('ipp.allowDownloadAll', false), + password: share.password ? immich.encryptPassword(share.password) : {}, showTitle: getConfigOption('ipp.showGalleryTitle', false), lgConfig: getConfigOption('lightGallery', {}) }) @@ -130,7 +132,7 @@ class Render { * Attempt to get a title from the link description or the album title */ title (share: SharedLink) { - return share.description || share?.album?.albumName || '' + return share.description || share?.album?.albumName || 'Gallery' } /** diff --git a/app/src/types.ts b/app/src/types.ts index 604da4c..b7a56d6 100644 --- a/app/src/types.ts +++ b/app/src/types.ts @@ -17,6 +17,7 @@ export interface SharedLink { type: string; description?: string; assets: Asset[]; + password?: string; album?: { id: string; albumName?: string; diff --git a/app/views/gallery.ejs b/app/views/gallery.ejs index 3f8287c..10378d7 100644 --- a/app/views/gallery.ejs +++ b/app/views/gallery.ejs @@ -2,7 +2,7 @@
-