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): enhance password validation for album passwords

Ahmet Ömerb05408643926
diff --git a/app/pages/dashboard/albums.vue b/app/pages/dashboard/albums.vue
index 099c11df..73a5a929 100644
--- a/app/pages/dashboard/albums.vue
+++ b/app/pages/dashboard/albums.vue
@@ -1,6 +1,11 @@
<script lang="ts" setup>
import type { Album, Photo } from '~~/server/utils/db'
import type { FormSubmitEvent, FormError } from '@nuxt/ui'
+import {
+ ALBUM_PASSWORD_MAX_LENGTH,
+ ALBUM_PASSWORD_MIN_LENGTH,
+ isValidAlbumPasswordLength,
+} from '~~/shared/utils/album-password'
definePageMeta({
layout: 'dashboard',
@@ -76,8 +81,31 @@ const validateForm = (state: any): FormError[] => {
message: $t('dashboard.albums.form.titleRequired'),
})
}
- if (state.passwordEnabled && !state.password && !currentAlbum.value?.hasPassword) {
- errors.push({ name: 'password', message: $t('dashboard.albums.form.passwordRequired') })
+ if (state.passwordEnabled) {
+ if (!state.password) {
+ if (!currentAlbum.value?.hasPassword) {
+ errors.push({
+ name: 'password',
+ message: $t('dashboard.albums.form.passwordRequired'),
+ })
+ }
+ } else if (!isValidAlbumPasswordLength(state.password)) {
+ if (state.password.length < ALBUM_PASSWORD_MIN_LENGTH) {
+ errors.push({
+ name: 'password',
+ message: $t('dashboard.albums.form.passwordTooShort', {
+ min: ALBUM_PASSWORD_MIN_LENGTH,
+ }),
+ })
+ } else {
+ errors.push({
+ name: 'password',
+ message: $t('dashboard.albums.form.passwordTooLong', {
+ max: ALBUM_PASSWORD_MAX_LENGTH,
+ }),
+ })
+ }
+ }
}
return errors
}
@@ -177,7 +205,8 @@ const onFormSubmit = async (event: FormSubmitEvent<AlbumFormState>) => {
photoIds: selectedPhotoIds.value,
isHidden: event.data.isHidden,
password: event.data.passwordEnabled
- ? (event.data.password || undefined) : '',
+ ? event.data.password || undefined
+ : '',
},
})
@@ -196,7 +225,9 @@ const onFormSubmit = async (event: FormSubmitEvent<AlbumFormState>) => {
coverPhotoId: coverPhotoId.value || undefined,
photoIds: selectedPhotoIds.value,
isHidden: event.data.isHidden,
- password: event.data.passwordEnabled ? event.data.password : undefined,
+ password: event.data.passwordEnabled
+ ? event.data.password
+ : undefined,
},
})
@@ -579,7 +610,12 @@ const columns = computed<any[]>(() => [
variant="soft"
color="neutral"
>
- {{ $t('dashboard.albums.photoCount', { count: (row.original as unknown as AlbumItem).photoCount || 0 }) }}
+ {{
+ $t('dashboard.albums.photoCount', {
+ count:
+ (row.original as unknown as AlbumItem).photoCount || 0,
+ })
+ }}
</UBadge>
</template>
@@ -609,7 +645,9 @@ const columns = computed<any[]>(() => [
color="neutral"
size="xs"
icon="tabler:chevron-down"
- :disabled="row.index === albums.length - 1 || isReorderingAlbums"
+ :disabled="
+ row.index === albums.length - 1 || isReorderingAlbums
+ "
:aria-label="$t('dashboard.albums.actions.moveDown')"
@click="moveAlbum(row.index, 1)"
/>
@@ -758,14 +796,35 @@ const columns = computed<any[]>(() => [
<UCheckbox
v-model="formData.passwordEnabled"
:label="$t('dashboard.albums.form.passwordProtection')"
- :description="$t('dashboard.albums.form.passwordProtectionHint')"
+ :description="
+ $t('dashboard.albums.form.passwordProtectionHint')
+ "
/>
</UFormField>
- <UFormField v-if="formData.passwordEnabled"
- :label="$t('dashboard.albums.form.password')" name="password"
- :hint="currentAlbum?.hasPassword ? $t('dashboard.albums.form.passwordKeepHint') : undefined">
- <UInput v-model="formData.password" type="password" autocomplete="new-password"
- class="w-full" :placeholder="$t('dashboard.albums.form.passwordPlaceholder')" />
+ <UFormField
+ v-if="formData.passwordEnabled"
+ :label="$t('dashboard.albums.form.password')"
+ name="password"
+ :hint="
+ currentAlbum?.hasPassword
+ ? $t('dashboard.albums.form.passwordKeepHint')
+ : $t('dashboard.albums.form.passwordLengthHint', {
+ min: ALBUM_PASSWORD_MIN_LENGTH,
+ max: ALBUM_PASSWORD_MAX_LENGTH,
+ })
+ "
+ >
+ <UInput
+ v-model="formData.password"
+ type="password"
+ autocomplete="new-password"
+ class="w-full"
+ :minlength="ALBUM_PASSWORD_MIN_LENGTH"
+ :maxlength="ALBUM_PASSWORD_MAX_LENGTH"
+ :placeholder="
+ $t('dashboard.albums.form.passwordPlaceholder')
+ "
+ />
</UFormField>
</UForm>
@@ -1179,7 +1238,11 @@ const columns = computed<any[]>(() => [
<p
class="truncate text-[10px] font-medium text-white/92"
>
- {{ photo.title || photo.storageKey || $t('ui.photo.untitled') }}
+ {{
+ photo.title ||
+ photo.storageKey ||
+ $t('ui.photo.untitled')
+ }}
</p>
<p class="truncate text-[9px] text-white/72">
{{
diff --git a/i18n/locales/en.json b/i18n/locales/en.json
index 688a42e3..89b6a88d 100644
--- a/i18n/locales/en.json
+++ b/i18n/locales/en.json
@@ -876,7 +876,10 @@
"password": "Password",
"passwordPlaceholder": "Enter a password",
"passwordKeepHint": "Leave blank to keep the current password",
+ "passwordLengthHint": "Must be {min}–{max} characters",
"passwordRequired": "Please enter a password",
+ "passwordTooShort": "Password must be at least {min} characters",
+ "passwordTooLong": "Password must be at most {max} characters",
"movePhotoUp": "Move photo up",
"movePhotoDown": "Move photo down",
"removePhoto": "Remove photo"
diff --git a/i18n/locales/ja.json b/i18n/locales/ja.json
index a6db4f05..1c5d36d0 100644
--- a/i18n/locales/ja.json
+++ b/i18n/locales/ja.json
@@ -802,7 +802,10 @@
"password": "パスワード",
"passwordPlaceholder": "パスワードを入力",
"passwordKeepHint": "現在のパスワードを変更しない場合は空欄にしてください",
+ "passwordLengthHint": "{min}〜{max} 文字で入力してください",
"passwordRequired": "パスワードを入力してください",
+ "passwordTooShort": "パスワードは {min} 文字以上にしてください",
+ "passwordTooLong": "パスワードは {max} 文字以下にしてください",
"movePhotoUp": "写真を上へ移動",
"movePhotoDown": "写真を下へ移動",
"removePhoto": "写真を選択から外す"
diff --git a/i18n/locales/ru.json b/i18n/locales/ru.json
index 6935ccf6..070b4533 100644
--- a/i18n/locales/ru.json
+++ b/i18n/locales/ru.json
@@ -875,7 +875,10 @@
"password": "Пароль",
"passwordPlaceholder": "Введите пароль",
"passwordKeepHint": "Оставьте поле пустым, чтобы сохранить текущий пароль",
+ "passwordLengthHint": "Длина должна быть от {min} до {max} символов",
"passwordRequired": "Введите пароль",
+ "passwordTooShort": "Пароль должен содержать не менее {min} символов",
+ "passwordTooLong": "Пароль должен содержать не более {max} символов",
"movePhotoUp": "Переместить фотографию вверх",
"movePhotoDown": "Переместить фотографию вниз",
"removePhoto": "Убрать фотографию"
diff --git a/i18n/locales/zh-Hans.json b/i18n/locales/zh-Hans.json
index fa33d794..d124dd8b 100644
--- a/i18n/locales/zh-Hans.json
+++ b/i18n/locales/zh-Hans.json
@@ -876,7 +876,10 @@
"password": "密码",
"passwordPlaceholder": "输入密码",
"passwordKeepHint": "留空以保留当前密码",
+ "passwordLengthHint": "长度须为 {min}–{max} 个字符",
"passwordRequired": "请输入密码",
+ "passwordTooShort": "密码至少需要 {min} 个字符",
+ "passwordTooLong": "密码最多 {max} 个字符",
"movePhotoUp": "上移照片",
"movePhotoDown": "下移照片",
"removePhoto": "移除照片"
diff --git a/i18n/locales/zh-Hant-HK.json b/i18n/locales/zh-Hant-HK.json
index 3153e357..190da9fe 100644
--- a/i18n/locales/zh-Hant-HK.json
+++ b/i18n/locales/zh-Hant-HK.json
@@ -802,7 +802,10 @@
"password": "密碼",
"passwordPlaceholder": "輸入密碼",
"passwordKeepHint": "留空以保留目前密碼",
+ "passwordLengthHint": "長度須為 {min}–{max} 個字元",
"passwordRequired": "請輸入密碼",
+ "passwordTooShort": "密碼至少需要 {min} 個字元",
+ "passwordTooLong": "密碼最多 {max} 個字元",
"movePhotoUp": "上移相片",
"movePhotoDown": "下移相片",
"removePhoto": "移除相片"
diff --git a/i18n/locales/zh-Hant-TW.json b/i18n/locales/zh-Hant-TW.json
index 03d01ffd..4bb8ad7d 100644
--- a/i18n/locales/zh-Hant-TW.json
+++ b/i18n/locales/zh-Hant-TW.json
@@ -802,7 +802,10 @@
"password": "密碼",
"passwordPlaceholder": "輸入密碼",
"passwordKeepHint": "留空以保留目前密碼",
+ "passwordLengthHint": "長度須為 {min}–{max} 個字元",
"passwordRequired": "請輸入密碼",
+ "passwordTooShort": "密碼至少需要 {min} 個字元",
+ "passwordTooLong": "密碼最多 {max} 個字元",
"movePhotoUp": "上移照片",
"movePhotoDown": "下移照片",
"removePhoto": "移除照片"
diff --git a/server/api/albums/[albumId]/index.put.ts b/server/api/albums/[albumId]/index.put.ts
index d0a40509..e9ed723d 100644
--- a/server/api/albums/[albumId]/index.put.ts
+++ b/server/api/albums/[albumId]/index.put.ts
@@ -1,4 +1,8 @@
import { z } from 'zod'
+import {
+ ALBUM_PASSWORD_MAX_LENGTH,
+ ALBUM_PASSWORD_MIN_LENGTH,
+} from '~~/shared/utils/album-password'
import { hashAlbumPassword } from '../../../utils/album-access'
export default eventHandler(async (event) => {
@@ -22,7 +26,15 @@ export default eventHandler(async (event) => {
coverPhotoId: z.string().optional(),
photoIds: z.array(z.string()).optional(),
isHidden: z.boolean().optional(),
- password: z.string().max(128).optional(),
+ password: z
+ .union([
+ z.literal(''),
+ z
+ .string()
+ .min(ALBUM_PASSWORD_MIN_LENGTH)
+ .max(ALBUM_PASSWORD_MAX_LENGTH),
+ ])
+ .optional(),
}).parse,
)
@@ -64,7 +76,9 @@ export default eventHandler(async (event) => {
updateData.isHidden = body.isHidden
}
if (body.password !== undefined) {
- updateData.passwordHash = body.password ? hashAlbumPassword(body.password) : null
+ updateData.passwordHash = body.password
+ ? hashAlbumPassword(body.password)
+ : null
}
tx.update(tables.albums)
@@ -109,5 +123,9 @@ export default eventHandler(async (event) => {
.get()
})
- return { ...updatedAlbum, passwordHash: undefined, hasPassword: Boolean(updatedAlbum?.passwordHash) }
+ return {
+ ...updatedAlbum,
+ passwordHash: undefined,
+ hasPassword: Boolean(updatedAlbum?.passwordHash),
+ }
})
diff --git a/server/api/albums/[albumId]/unlock.post.ts b/server/api/albums/[albumId]/unlock.post.ts
index 97d83bdd..bb22d27a 100644
--- a/server/api/albums/[albumId]/unlock.post.ts
+++ b/server/api/albums/[albumId]/unlock.post.ts
@@ -1,6 +1,7 @@
import { eq } from 'drizzle-orm'
import type { H3Event } from 'h3'
import { z } from 'zod'
+import { ALBUM_PASSWORD_MAX_LENGTH } from '~~/shared/utils/album-password'
import {
grantAlbumAccess,
verifyAlbumPassword,
@@ -45,9 +46,11 @@ export default eventHandler(async (event) => {
event,
z.object({ albumId: z.coerce.number().int().positive() }).parse,
)
+ // Keep min(1) so albums set before the stronger length policy can still unlock.
const { password } = await readValidatedBody(
event,
- z.object({ password: z.string().min(1).max(128) }).parse,
+ z.object({ password: z.string().min(1).max(ALBUM_PASSWORD_MAX_LENGTH) })
+ .parse,
)
const clientIp =
getRequestIP(event, { xForwardedFor: trustProxy }) || 'unknown'
diff --git a/server/api/albums/index.post.ts b/server/api/albums/index.post.ts
index 8e1743df..79ecf416 100644
--- a/server/api/albums/index.post.ts
+++ b/server/api/albums/index.post.ts
@@ -1,5 +1,9 @@
import { min } from 'drizzle-orm'
import z from 'zod'
+import {
+ ALBUM_PASSWORD_MAX_LENGTH,
+ ALBUM_PASSWORD_MIN_LENGTH,
+} from '~~/shared/utils/album-password'
import { hashAlbumPassword } from '../../utils/album-access'
export default eventHandler(async (event) => {
@@ -13,7 +17,11 @@ export default eventHandler(async (event) => {
coverPhotoId: z.string().optional(),
photoIds: z.array(z.string()).optional(),
isHidden: z.boolean().optional(),
- password: z.string().min(1).max(128).optional(),
+ password: z
+ .string()
+ .min(ALBUM_PASSWORD_MIN_LENGTH)
+ .max(ALBUM_PASSWORD_MAX_LENGTH)
+ .optional(),
}).parse,
)
@@ -62,7 +70,11 @@ export default eventHandler(async (event) => {
}
}
- return { ...newAlbum, passwordHash: undefined, hasPassword: Boolean(newAlbum.passwordHash) }
+ return {
+ ...newAlbum,
+ passwordHash: undefined,
+ hasPassword: Boolean(newAlbum.passwordHash),
+ }
})
return album
diff --git a/shared/utils/album-password.ts b/shared/utils/album-password.ts
new file mode 100644
index 00000000..d8245dae
--- /dev/null
+++ b/shared/utils/album-password.ts
@@ -0,0 +1,9 @@
+export const ALBUM_PASSWORD_MIN_LENGTH = 6
+export const ALBUM_PASSWORD_MAX_LENGTH = 128
+
+export function isValidAlbumPasswordLength(password: string): boolean {
+ return (
+ password.length >= ALBUM_PASSWORD_MIN_LENGTH &&
+ password.length <= ALBUM_PASSWORD_MAX_LENGTH
+ )
+}