Move password payload to cookie Improve invalid password message
This commit is contained in:
+7
-22
@@ -3,7 +3,6 @@ import dayjs from 'dayjs'
|
||||
import { addResponseHeaders, getConfigOption, log } from './functions'
|
||||
import render from './render'
|
||||
import { Response } from 'express-serve-static-core'
|
||||
import { encrypt } from './encrypt'
|
||||
|
||||
class Immich {
|
||||
/**
|
||||
@@ -65,8 +64,9 @@ class Immich {
|
||||
// A password is required, but the visitor-provided one doesn't match
|
||||
if (sharedLinkRes.passwordRequired && request.password) {
|
||||
log('Invalid password for key ' + request.key)
|
||||
res.status(401).send()
|
||||
return
|
||||
res.status(401)
|
||||
// Delete the cookie-session data, so that it doesn't keep saying "Invalid password"
|
||||
if (request.req?.session) delete request.req.session[request.key]
|
||||
}
|
||||
|
||||
// Password required - show the visitor the password page
|
||||
@@ -75,7 +75,8 @@ class Immich {
|
||||
const key = request.key.replace(/[^\w-]/g, '')
|
||||
res.render('password', {
|
||||
key,
|
||||
lgConfig: render.lgConfig
|
||||
lgConfig: render.lgConfig,
|
||||
notifyInvalidPassword: !!request.password
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -222,16 +223,14 @@ class Immich {
|
||||
photoUrl (key: string, id: string, size?: ImageSize, password?: string) {
|
||||
const path = ['photo', key, id]
|
||||
if (size) path.push(size)
|
||||
const params = password ? this.encryptPassword(password) : {}
|
||||
return this.buildUrl('/share/' + path.join('/'), params)
|
||||
return this.buildUrl('/share/' + path.join('/'))
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the video data URL for a video
|
||||
*/
|
||||
videoUrl (key: string, id: string, password?: string) {
|
||||
const params = password ? this.encryptPassword(password) : {}
|
||||
return this.buildUrl(`/share/video/${key}/${id}`, params)
|
||||
return this.buildUrl(`/share/video/${key}/${id}`)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -250,20 +249,6 @@ class Immich {
|
||||
return !!key.match(/^[\w-]+$/)
|
||||
}
|
||||
|
||||
/**
|
||||
* When loading assets from a password-protected link, make the decryption key valid for a
|
||||
* short time. If the visitor loads the share link again, it will renew that expiry time.
|
||||
* Even though the recipient already knows the password, this is just in case - for example
|
||||
* to protect against the password-protected link being revoked, but the asset links still
|
||||
* being valid.
|
||||
*/
|
||||
encryptPassword (password: string) {
|
||||
return encrypt(JSON.stringify({
|
||||
password,
|
||||
expires: dayjs().add(1, 'hour').format()
|
||||
}))
|
||||
}
|
||||
|
||||
async accessible () {
|
||||
return !!(await immich.request('/server/ping'))
|
||||
}
|
||||
|
||||
+29
-10
@@ -1,13 +1,15 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import express from 'express'
|
||||
import cookieSession from 'cookie-session'
|
||||
import immich from './immich'
|
||||
import crypto from 'crypto'
|
||||
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, getConfigOption } from './functions'
|
||||
import { decrypt, encrypt } from './encrypt'
|
||||
|
||||
// Extend the Request type with a `password` property
|
||||
declare module 'express-serve-static-core' {
|
||||
@@ -19,6 +21,12 @@ declare module 'express-serve-static-core' {
|
||||
require('dotenv').config()
|
||||
|
||||
const app = express()
|
||||
app.use(cookieSession({
|
||||
name: 'session',
|
||||
httpOnly: true,
|
||||
sameSite: 'strict',
|
||||
secret: crypto.randomBytes(32).toString('base64url')
|
||||
}))
|
||||
// Add the EJS view engine, to render the gallery page
|
||||
app.set('view engine', 'ejs')
|
||||
// For parsing the password unlock form
|
||||
@@ -29,14 +37,16 @@ app.use('/share/static', express.static('public', { setHeaders: addResponseHeade
|
||||
app.use(express.static('public', { setHeaders: addResponseHeaders }))
|
||||
|
||||
/**
|
||||
* Middleware to decode an encrypted password sent from the frontend (if provided)
|
||||
* Middleware to decode the encrypted data stored in the session cookie
|
||||
*/
|
||||
const checkPassword = (req: Request, res: Response, next: NextFunction) => {
|
||||
if (req.query?.cr && req.query?.iv) {
|
||||
const decodeCookie = (req: Request, _res: Response, next: NextFunction) => {
|
||||
const shareKey = req.params.key
|
||||
const session = req.session?.[shareKey]
|
||||
if (shareKey && session?.iv && session?.cr) {
|
||||
try {
|
||||
const payload = JSON.parse(decrypt({
|
||||
iv: toString(req.query.iv),
|
||||
cr: toString(req.query.cr)
|
||||
iv: toString(session.iv),
|
||||
cr: toString(session.cr)
|
||||
}))
|
||||
if (payload?.expires && dayjs(payload.expires) > dayjs()) {
|
||||
req.password = payload.password
|
||||
@@ -61,8 +71,9 @@ 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)?', checkPassword, async (req, res) => {
|
||||
app.get('/share/:key/:mode(download)?', decodeCookie, async (req, res) => {
|
||||
await immich.handleShareRequest({
|
||||
req,
|
||||
key: req.params.key,
|
||||
mode: req.params.mode,
|
||||
password: req.password
|
||||
@@ -71,16 +82,23 @@ app.get('/share/:key/:mode(download)?', checkPassword, async (req, res) => {
|
||||
|
||||
/*
|
||||
* [ROUTE] Receive an unlock request from the password page
|
||||
* Returns an encrypted unlock key which lasts for 1 hour
|
||||
* Stores a cookie with an encrypted payload which expires in 1 hour.
|
||||
* After that time, the visitor will need to provide the password again.
|
||||
*/
|
||||
app.post('/share/unlock', async (req, res) => {
|
||||
res.send(immich.encryptPassword(req.body.password))
|
||||
if (req.session && req.body.key) {
|
||||
req.session[req.body.key] = encrypt(JSON.stringify({
|
||||
password: req.body.password,
|
||||
expires: dayjs().add(1, 'hour').format()
|
||||
}))
|
||||
}
|
||||
res.send()
|
||||
})
|
||||
|
||||
/*
|
||||
* [ROUTE] This is the direct link to a photo or video asset
|
||||
*/
|
||||
app.get('/share/:type(photo|video)/:key/:id/:size?', checkPassword, async (req, res) => {
|
||||
app.get('/share/:type(photo|video)/:key/:id/:size?', decodeCookie, async (req, res) => {
|
||||
// Add the headers configured in config.json (most likely `cache-control`)
|
||||
addResponseHeaders(res)
|
||||
|
||||
@@ -102,6 +120,7 @@ app.get('/share/:type(photo|video)/:key/:id/:size?', checkPassword, async (req,
|
||||
// is allowed by this shared link.
|
||||
const sharedLink = (await immich.getShareByKey(req.params.key, req.password))?.link
|
||||
const request = {
|
||||
req,
|
||||
key: req.params.key,
|
||||
range: req.headers.range || ''
|
||||
}
|
||||
|
||||
@@ -121,7 +121,6 @@ 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', {})
|
||||
})
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { Request } from 'express-serve-static-core'
|
||||
|
||||
export enum AssetType {
|
||||
image = 'IMAGE',
|
||||
video = 'VIDEO'
|
||||
@@ -41,6 +43,7 @@ export enum ImageSize {
|
||||
}
|
||||
|
||||
export interface IncomingShareRequest {
|
||||
req: Request;
|
||||
key: string;
|
||||
password?: string;
|
||||
mode?: string;
|
||||
|
||||
Reference in New Issue
Block a user