Move password payload to cookie Improve invalid password message
This commit is contained in:
+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 || ''
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user