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.

fix(albums): hide photos shared with a hidden album from anonymous viewers

simonyang086875f4561241
diff --git a/server/api/albums/[albumId]/index.get.ts b/server/api/albums/[albumId]/index.get.ts
index 9bfe3ed2..ade769ea 100644
--- a/server/api/albums/[albumId]/index.get.ts
+++ b/server/api/albums/[albumId]/index.get.ts
@@ -1,6 +1,8 @@
-import { asc, getTableColumns } from 'drizzle-orm'
+import { eq } from 'drizzle-orm'
import z from 'zod'
+import { fetchAlbumPhotos } from '../../../utils/album-visibility'
+
export default eventHandler(async (event) => {
const { albumId } = await getValidatedRouterParams(
event,
@@ -28,40 +30,21 @@ export default eventHandler(async (event) => {
}
// 检查相册是否隐藏,如果隐藏则需要用户登录才能访问
- if (album.isHidden) {
- const session = await getUserSession(event)
- if (!session.user) {
- throw createError({
- statusCode: 404,
- statusMessage: 'Album not found',
- })
- }
- }
+ const session = await getUserSession(event)
+ const isLoggedIn = Boolean(session.user)
- // 获取相册中的照片
- const photos = await db
- // all fields from tables.photos
- .select({
- ...getTableColumns(tables.photos),
+ if (album.isHidden && !isLoggedIn) {
+ throw createError({
+ statusCode: 404,
+ statusMessage: 'Album not found',
})
- .from(tables.photos)
- .innerJoin(
- tables.albumPhotos,
- eq(tables.photos.id, tables.albumPhotos.photoId),
- )
- .where(eq(tables.albumPhotos.albumId, albumId))
- .orderBy(asc(tables.albumPhotos.position))
- .all()
-
- // 验证相册数据完整性
- if (!photos || !Array.isArray(photos)) {
- // 空相册也是合法的,只需要返回空数组
- return {
- ...album,
- photos: [],
- }
}
+ // Anonymous viewers must not see photos that are also members of any
+ // hidden album — otherwise a photo shared between a public and a hidden
+ // album would leak through the public album (see issue #299).
+ const photos = await fetchAlbumPhotos(db, tables, albumId, isLoggedIn)
+
return {
...album,
photos,
diff --git a/server/utils/album-visibility.ts b/server/utils/album-visibility.ts
new file mode 100644
index 00000000..208dff3a
--- /dev/null
+++ b/server/utils/album-visibility.ts
@@ -0,0 +1,56 @@
+import { asc, eq, getTableColumns } from 'drizzle-orm'
+import type { useDB } from './db'
+import type * as schema from '../database/schema'
+
+/**
+ * Returns the photos of a given album, optionally stripping those that also
+ * appear in any hidden album (so anonymous viewers cannot reach them via a
+ * public album).
+ *
+ * Kept as a small pure helper so the visibility contract can be exercised
+ * from tests without spinning up the Nitro event-handler runtime. The
+ * production handler in `server/api/albums/[albumId]/index.get.ts` delegates
+ * to this helper.
+ */
+export async function fetchAlbumPhotos(
+ db: ReturnType<typeof useDB>,
+ tables: typeof schema,
+ albumId: number,
+ loggedIn: boolean,
+) {
+ const rows = await db
+ .select({
+ ...getTableColumns(tables.photos),
+ })
+ .from(tables.photos)
+ .innerJoin(
+ tables.albumPhotos,
+ eq(tables.photos.id, tables.albumPhotos.photoId),
+ )
+ .where(eq(tables.albumPhotos.albumId, albumId))
+ .orderBy(asc(tables.albumPhotos.position))
+ .all()
+
+ if (loggedIn) {
+ return rows
+ }
+
+ const hiddenPhotoIds = (
+ await db
+ .select({ photoId: tables.albumPhotos.photoId })
+ .from(tables.albumPhotos)
+ .innerJoin(
+ tables.albums,
+ eq(tables.albumPhotos.albumId, tables.albums.id),
+ )
+ .where(eq(tables.albums.isHidden, true))
+ .all()
+ ).map((r: { photoId: string }) => r.photoId)
+
+ if (hiddenPhotoIds.length === 0) {
+ return rows
+ }
+
+ const hidden = new Set(hiddenPhotoIds)
+ return rows.filter((row: { id: string }) => !hidden.has(row.id))
+}