fix(core): strip trailing slashes when normalizing auth and MCP resource URLs (#506)

* fix(core): strip trailing slashes when normalizing auth and MCP resource URLs

* docs: document BETTER_AUTH_URL in README and quickstart guides

* docs: prioritize AWS_ENDPOINT_URL_S3 and clarify applicability to local storage

* docs: refine AWS_ENDPOINT_URL_S3 guidance for local storage deployments
This commit is contained in:
Yiling-J
2026-10-29 17:24:31 +08:00
committed by GitHub
parent baf30730af
commit 687b5a79d8
15 changed files with 206 additions and 72 deletions
+22
View File
@@ -303,4 +303,26 @@ describe('AuthService Password Reset', () => {
expect(resetResA.success).toBe(true)
expect(resetResB.success).toBe(true)
})
it('generates reset link cleanly preserving path prefix and without double slashes when BETTER_AUTH_URL has trailing slashes', async () => {
const originalEnv = process.env.BETTER_AUTH_URL
try {
process.env.BETTER_AUTH_URL = 'https://staging.shumai.one/shumai///'
const email = `slash-test-${Date.now()}@example.com`
await auth.api.signUpEmail({
headers: new Headers(),
body: { email, password: 'password123', name: 'Slash User' },
})
const link = await authService.generatePasswordResetLink(email)
expect(link).toMatch(/^https:\/\/staging\.shumai\.one\/shumai\/reset-password\?token=/)
expect(link).not.toContain('//reset-password')
} finally {
if (originalEnv !== undefined) {
process.env.BETTER_AUTH_URL = originalEnv
} else {
delete process.env.BETTER_AUTH_URL
}
}
})
})
+3 -2
View File
@@ -16,8 +16,9 @@ interface GeneratedResetLink {
const resetLinkStorage = new AsyncLocalStorage<GeneratedResetLink>()
export const authBaseUrl =
export const authBaseUrl = (
process.env.BETTER_AUTH_URL || `http://localhost:${process.env.SHUMAI_SERVER_PORT || '3000'}`
).replace(/\/+$/, '')
export const mcpResource = `${authBaseUrl}/mcp`
export { hashPassword } from 'better-auth/crypto'
@@ -202,7 +203,7 @@ export class AuthService {
const rawBaseUrl =
process.env.BETTER_AUTH_URL ||
`http://localhost:${process.env.SHUMAI_SERVER_PORT || '3000'}`
const baseUrl = rawBaseUrl.replace(/\/$/, '')
const baseUrl = rawBaseUrl.replace(/\/+$/, '')
return `${baseUrl}/reset-password?token=${context.token}`
}
@@ -2,7 +2,7 @@ import { describe, it, expect, beforeAll } from 'vitest'
import { prisma } from '@shumai/db'
import { setupTestDbHooks } from '@shumai/db/test'
import { McpDbStore } from './mcp-db-store'
import { McpOauthProvider } from './mcp-oauth-provider'
import { getDefaultOauthRedirectUri, McpOauthProvider } from './mcp-oauth-provider'
/* eslint-disable @typescript-eslint/naming-convention */
// The OAuth wire format (RFC 6749 / OAuth 2.1) uses snake_case field names
@@ -144,4 +144,20 @@ describe('McpOauthProvider SEP-2352 issuer stamping', () => {
const info = await makeProvider().clientInformation()
expect((info as { issuer?: string }).issuer).toBeUndefined()
})
it('normalizes redirect URI cleanly preserving path prefix when base URL has trailing slashes', () => {
const originalEnv = process.env.MCP_OAUTH_REDIRECT_BASE_URL
try {
process.env.MCP_OAUTH_REDIRECT_BASE_URL = 'https://staging.shumai.one/shumai///'
expect(getDefaultOauthRedirectUri()).toBe(
'https://staging.shumai.one/shumai/api/mcp/oauth/callback',
)
} finally {
if (originalEnv !== undefined) {
process.env.MCP_OAUTH_REDIRECT_BASE_URL = originalEnv
} else {
delete process.env.MCP_OAUTH_REDIRECT_BASE_URL
}
}
})
})
+1 -1
View File
@@ -48,7 +48,7 @@ export function getOauthRedirectBaseUrl(): string {
export function getDefaultOauthRedirectUri(): string | undefined {
const base = getOauthRedirectBaseUrl()
if (!base) return undefined
return `${base.replace(/\/$/, '')}/api/mcp/oauth/callback`
return `${base.replace(/\/+$/, '')}/api/mcp/oauth/callback`
}
/** Configuration options for OAuth. */