Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

feat(albums): refactor password hashing for album creation and update

Ahmet Ömer076a35b813f3
diff --git a/server/api/albums/[albumId]/index.put.ts b/server/api/albums/[albumId]/index.put.ts
index e9ed723d..c4655093 100644
--- a/server/api/albums/[albumId]/index.put.ts
+++ b/server/api/albums/[albumId]/index.put.ts
@@ -54,6 +54,13 @@ export default eventHandler(async (event) => {
})
}
+ const passwordHash =
+ body.password !== undefined
+ ? body.password
+ ? await hashAlbumPassword(body.password)
+ : null
+ : undefined
+
// 使用事务更新相簿
const updatedAlbum = db.transaction((tx) => {
// 更新基本信息
@@ -75,10 +82,8 @@ export default eventHandler(async (event) => {
if (body.isHidden !== undefined) {
updateData.isHidden = body.isHidden
}
- if (body.password !== undefined) {
- updateData.passwordHash = body.password
- ? hashAlbumPassword(body.password)
- : null
+ if (passwordHash !== undefined) {
+ updateData.passwordHash = passwordHash
}
tx.update(tables.albums)
diff --git a/server/api/albums/index.post.ts b/server/api/albums/index.post.ts
index 79ecf416..1da5b206 100644
--- a/server/api/albums/index.post.ts
+++ b/server/api/albums/index.post.ts
@@ -27,6 +27,10 @@ export default eventHandler(async (event) => {
const db = useDB()
+ const passwordHash = body.password
+ ? await hashAlbumPassword(body.password)
+ : null
+
const album = db.transaction((tx) => {
// Place new album first (min position minus one gap), preserving the
// default "newest first" order
@@ -43,7 +47,7 @@ export default eventHandler(async (event) => {
description: body.description || null,
coverPhotoId: body.coverPhotoId || null,
isHidden: body.isHidden || false,
- passwordHash: body.password ? hashAlbumPassword(body.password) : null,
+ passwordHash,
position,
})
.returning()
diff --git a/server/utils/album-access.ts b/server/utils/album-access.ts
index a11fe885..ce2ad68d 100644
--- a/server/utils/album-access.ts
+++ b/server/utils/album-access.ts
@@ -2,7 +2,6 @@ import {
createHmac,
randomBytes,
scrypt,
- scryptSync,
timingSafeEqual,
} from 'node:crypto'
import { promisify } from 'node:util'
@@ -30,9 +29,10 @@ const passwordVersion = (hash: string) =>
.digest('hex')
.slice(0, 32)
-export function hashAlbumPassword(password: string): string {
+export async function hashAlbumPassword(password: string): Promise<string> {
const salt = randomBytes(16).toString('hex')
- return `${salt}:${scryptSync(password, salt, 64).toString('hex')}`
+ const derived = (await scryptAsync(password, salt, 64)) as Buffer
+ return `${salt}:${derived.toString('hex')}`
}
export async function verifyAlbumPassword(