Fix #29 Download all password albums

This commit is contained in:
Alan Grainger
2024-12-01 18:32:26 +13:00
parent a4e4762cfb
commit e633f00afa
6 changed files with 47 additions and 28 deletions
+1 -1
View File
@@ -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",
+1
View File
@@ -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)
+32 -23
View File
@@ -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 || ''
+3 -1
View File
@@ -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'
}
/**
+1
View File
@@ -17,6 +17,7 @@ export interface SharedLink {
type: string;
description?: string;
assets: Asset[];
password?: string;
album?: {
id: string;
albumName?: string;
+9 -3
View File
@@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title><%- title || 'Gallery' %></title>
<title><%- title %></title>
<link rel="icon" href="/share/static/favicon.ico" type="image/x-icon">
<link type="text/css" rel="stylesheet" href="/share/static/style.css"/>
<link type="text/css" rel="stylesheet" href="/share/static/lg/lightgallery-bundle.min.css"/>
@@ -12,9 +12,15 @@
<% if (showTitle) { %>
<h1><%- title || 'Gallery' %></h1>
<% } %>
<% if (showDownload) { %>
<%
if (showDownload) {
// If there is a password required for the download, format it as a query string
const passwordQuery = Object.entries(password).length ? '?' + (new URLSearchParams(password)).toString() : ''
%>
<div id="download-all">
<a href="<%- path %>/download" title="Download all"><img src="/images/download-all.svg" height="24" width="24" alt="Download all"></a>
<a href="<%- path %>/download<%- passwordQuery %>" title="Download all"><img src="/images/download-all.svg"
height="24"
width="24" alt="Download all"></a>
</div>
<% } %>
</div>