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(auth): harden provider credentials and avatar encoding

Timothy Yin35a127030ec3
diff --git a/apps/cloud/app/components/CloudAccountProfile.vue b/apps/cloud/app/components/CloudAccountProfile.vue
index 115b4405..916ca184 100644
--- a/apps/cloud/app/components/CloudAccountProfile.vue
+++ b/apps/cloud/app/components/CloudAccountProfile.vue
@@ -29,20 +29,23 @@ async function chooseImage(event: Event) {
const canvas = document.createElement('canvas')
canvas.width = canvas.height = 128
const size = Math.min(bitmap.width, bitmap.height)
- canvas
- .getContext('2d')!
- .drawImage(
- bitmap,
- (bitmap.width - size) / 2,
- (bitmap.height - size) / 2,
- size,
- size,
- 0,
- 0,
- 128,
- 128,
- )
- const result = canvas.toDataURL('image/webp', 0.85)
+ const drawing = canvas.getContext('2d')!
+ drawing.fillStyle = '#ffffff'
+ drawing.fillRect(0, 0, 128, 128)
+ drawing.drawImage(
+ bitmap,
+ (bitmap.width - size) / 2,
+ (bitmap.height - size) / 2,
+ size,
+ size,
+ 0,
+ 0,
+ 128,
+ 128,
+ )
+ let result = canvas.toDataURL('image/webp', 0.85)
+ if (!result.startsWith('data:image/webp;'))
+ result = canvas.toDataURL('image/jpeg', 0.85)
if (result.length > 65536) throw new Error('Image too large')
image.value = result
} finally {
diff --git a/packages/contracts/src/social-login.ts b/packages/contracts/src/social-login.ts
index 36d7ebd0..1fa5f996 100644
--- a/packages/contracts/src/social-login.ts
+++ b/packages/contracts/src/social-login.ts
@@ -1,8 +1,8 @@
-/** Provider identifiers are fixed; arbitrary OAuth endpoints cannot be configured. */
+/** Microsoft is excluded until email ownership can be established independently.
+ * Provider identifiers are fixed; arbitrary OAuth endpoints cannot be configured. */
export const SOCIAL_LOGIN_PROVIDERS = [
{ id: 'github', name: 'GitHub', icon: 'i-tabler-brand-github' },
{ id: 'google', name: 'Google', icon: 'i-tabler-brand-google' },
- { id: 'microsoft', name: 'Microsoft', icon: 'i-tabler-brand-windows' },
{ id: 'discord', name: 'Discord', icon: 'i-tabler-brand-discord' },
] as const
export type SocialLoginProvider = (typeof SOCIAL_LOGIN_PROVIDERS)[number]['id']
diff --git a/packages/platform/src/social-login.ts b/packages/platform/src/social-login.ts
index d91d1c69..56e1f456 100644
--- a/packages/platform/src/social-login.ts
+++ b/packages/platform/src/social-login.ts
@@ -94,17 +94,27 @@ export async function saveSocialLoginProvider(
if (input.operation === 'save') {
let clientSecret = input.clientSecret
if (clientSecret === '' && row.credentials_ciphertext) {
- clientSecret = (
- await (
- await socialLoginCipher(secret)
- ).open(row.credentials_ciphertext, `${provider}:${row.revision}`)
- ).clientSecret
- // Reusing a secret for a different OAuth application is almost always a mistake.
- if (input.clientId !== row.client_id)
+ // Check the application identity before attempting to reuse its secret.
+ if (
+ typeof input.clientId !== 'string' ||
+ input.clientId.trim() !== row.client_id
+ )
throw new SocialLoginError(
400,
'Provide a new secret when changing the client ID',
)
+ try {
+ clientSecret = (
+ await (
+ await socialLoginCipher(secret)
+ ).open(row.credentials_ciphertext, `${provider}:${row.revision}`)
+ ).clientSecret
+ } catch {
+ throw new SocialLoginError(
+ 400,
+ 'Stored secret is unreadable; provide a new secret',
+ )
+ }
}
try {
credentials = parseSocialLoginCredentials({
diff --git a/tests/social-login.test.ts b/tests/social-login.test.ts
index 55dd897b..a124a2c1 100644
--- a/tests/social-login.test.ts
+++ b/tests/social-login.test.ts
@@ -12,13 +12,16 @@ import {
saveSocialLoginProvider,
} from '../packages/platform/src/social-login.ts'
import { parseAccountProfile } from '../packages/contracts/src/account-profile.ts'
+type TestEvent = { path: string; origin?: string; raw?: string }
+type TestError = { statusMessage?: string; statusCode?: number }
+type TestSession = { user: { id: string; emailVerified: boolean } } | null
const require = createRequire(import.meta.url)
const { Miniflare, convertV4MiniflareOptions } = createRequire(
require.resolve('wrangler/package.json'),
)('miniflare')
test('OAuth callbacks expose only the supported provider surface', () => {
- for (const provider of ['github', 'google', 'microsoft', 'discord']) {
+ for (const provider of ['github', 'google', 'discord']) {
assert(accountRouteAllowed(`/api/auth/callback/${provider}`, 'GET'))
assert(accountRouteAllowed(`/api/auth/callback/${provider}`, 'POST'))
assert(!accountRouteAllowed(`/api/auth/callback/${provider}`, 'DELETE'))
@@ -26,6 +29,7 @@ test('OAuth callbacks expose only the supported provider surface', () => {
assert(accountRouteAllowed('/api/auth/sign-in/social', 'POST'))
for (const path of [
'/api/auth/callback/unknown',
+ '/api/auth/callback/microsoft',
'/api/auth/callback/github/extra',
'/api/auth/update-user',
'/api/auth/link-social',
@@ -151,8 +155,88 @@ test('credentials are root-only, encrypted, redacted, revision fenced and disabl
await saveSocialLoginProvider(db, secret, actor, {
...input,
revision: 1,
+ clientId: ' client-test ',
clientSecret: '',
})
+ // Wrong/malformed application IDs are rejected before opening an unreadable secret.
+ for (const clientId of ['another-client', null]) {
+ await assert.rejects(
+ saveSocialLoginProvider(db, 'wrong-key'.repeat(5), actor, {
+ ...input,
+ revision: 2,
+ clientId,
+ clientSecret: '',
+ }),
+ {
+ status: 400,
+ message: 'Provide a new secret when changing the client ID',
+ },
+ )
+ }
+ await assert.rejects(
+ saveSocialLoginProvider(db, 'wrong-key'.repeat(5), actor, {
+ ...input,
+ revision: 2,
+ clientSecret: '',
+ }),
+ {
+ status: 400,
+ message: 'Stored secret is unreadable; provide a new secret',
+ },
+ )
+ const beforeFailure = await db
+ .prepare(
+ "SELECT revision,credentials_ciphertext FROM platform_social_login_providers WHERE provider='github'",
+ )
+ .first()
+ assert.equal(beforeFailure.revision, 2)
+ for (const key of ['', 'short'])
+ await assert.rejects(
+ saveSocialLoginProvider(db, key, actor, {
+ ...input,
+ revision: 2,
+ clientSecret: '',
+ }),
+ {
+ status: 400,
+ message: 'Stored secret is unreadable; provide a new secret',
+ },
+ )
+ await db
+ .prepare(
+ "UPDATE platform_social_login_providers SET credentials_ciphertext='corrupt' WHERE provider='github'",
+ )
+ .run()
+ await assert.rejects(
+ saveSocialLoginProvider(db, secret, actor, {
+ ...input,
+ revision: 2,
+ clientSecret: '',
+ }),
+ {
+ status: 400,
+ message: 'Stored secret is unreadable; provide a new secret',
+ },
+ )
+ await db
+ .prepare(
+ "UPDATE platform_social_login_providers SET credentials_ciphertext=? WHERE provider='github'",
+ )
+ .bind(beforeFailure.credentials_ciphertext)
+ .run()
+ assert.equal(
+ (await cipher.open(beforeFailure.credentials_ciphertext, 'github:2'))
+ .clientSecret,
+ 'secret-test',
+ )
+ // Unsupported providers cannot be configured, even by root.
+ await assert.rejects(
+ saveSocialLoginProvider(db, secret, actor, {
+ ...input,
+ provider: 'microsoft',
+ }),
+ { status: 400 },
+ )
const audit = await db
.prepare(
"SELECT json_group_array(json_object('before',before_json,'after',after_json)) AS rows FROM platform_admin_audit",
@@ -187,9 +271,9 @@ test('credentials are root-only, encrypted, redacted, revision fenced and disabl
platformPortalOrigin: () => {},
requirePlatformSession: async () => ({ userId: 42 }),
platformDatabase: () => db,
- siteManagementBody: async (event: any) => event.body,
+ siteManagementBody: async (event: { body: unknown }) => event.body,
parseAccountProfile,
- createError: (value: any) =>
+ createError: (value: TestError) =>
Object.assign(new Error(value.statusMessage), value),
}
const profileHandler = new Function(
@@ -326,9 +410,10 @@ test('credentials are root-only, encrypted, redacted, revision fenced and disabl
headers: headers(),
}),
)
- return (await session.json()) as any
+ return (await session.json()) as TestSession
}
const session = await login('/workspace')
+ assert(session)
assert.equal(Number(session.user.id), 42)
assert.equal(
(
@@ -357,6 +442,7 @@ test('credentials are root-only, encrypted, redacted, revision fenced and disabl
githubId = 103
const newUser = await login('/workspace')
+ assert(newUser)
assert(newUser.user.emailVerified)
assert(
await db
@@ -413,19 +499,24 @@ test('auth forwarding checks web origins and rejects ID-token entry while accept
useRuntimeConfig: () => ({
platform: { portalOrigin: 'https://app.chronoframe.art' },
}),
- getRequestURL: (e: any) => new URL(e.path, 'https://app.chronoframe.art'),
+ getRequestURL: (e: TestEvent) =>
+ new URL(e.path, 'https://app.chronoframe.art'),
platformRequestURL: () => new URL('https://app.chronoframe.art'),
- platformPortalOrigin: (e: any, _origin: string, required: boolean) => {
+ platformPortalOrigin: (
+ e: TestEvent,
+ _origin: string,
+ required: boolean,
+ ) => {
if (required && e.origin !== 'https://app.chronoframe.art')
throw Object.assign(new Error('Origin'), { statusCode: 403 })
},
accountRouteAllowed,
socialLoginCallback: (path: string) =>
- /^\/api\/auth\/callback\/(github|google|microsoft|discord)$/.test(path),
+ /^\/api\/auth\/callback\/(github|google|discord)$/.test(path),
isSocialLoginProvider: (id: string) =>
- ['github', 'google', 'microsoft', 'discord'].includes(id),
+ ['github', 'google', 'discord'].includes(id),
getHeader: () => undefined,
- readRawBody: async (e: any) => e.raw,
+ readRawBody: async (e: TestEvent) => e.raw,
getRequestHeaders: () => ({}),
cloudAccount: async () => ({
handler: async () => {
@@ -433,8 +524,8 @@ test('auth forwarding checks web origins and rejects ID-token entry while accept
return new Response('ok')
},
}),
- sendWebResponse: (_e: any, r: Response) => r,
- createError: (v: any) => Object.assign(new Error(v.statusMessage), v),
+ sendWebResponse: (_e: TestEvent, r: Response) => r,
+ createError: (v: TestError) => Object.assign(new Error(v.statusMessage), v),
}
const { forwardAccountRequest } = new Function(
'exports',