From 687b5a79d83e4c2dc97b017b5071f00921911cd3 Mon Sep 17 00:00:00 2001 From: Yiling-J Date: Thu, 29 Oct 2026 17:24:31 +0800 Subject: [PATCH] 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 --- README.md | 30 +++-- README_CN.md | 60 ++++----- .../docker-compose-aliyun.embedding.yaml | 1 + .../local/docker-compose-aliyun.full.yaml | 1 + .../local/docker-compose-aliyun.yaml | 1 + .../local/docker-compose.embedding.yaml | 1 + docker-compose/local/docker-compose.full.yaml | 1 + docker-compose/local/docker-compose.yaml | 1 + docs/configuration/env-variables.mdx | 10 +- docs/getting-started/quickstart.mdx | 121 +++++++++++++++--- packages/api/src/mcp-server.oauth.test.ts | 4 + packages/core/src/auth/auth.test.ts | 22 ++++ packages/core/src/auth/auth.ts | 5 +- .../core/src/mcp/mcp-oauth-provider.test.ts | 18 ++- packages/core/src/mcp/mcp-oauth-provider.ts | 2 +- 15 files changed, 206 insertions(+), 72 deletions(-) diff --git a/README.md b/README.md index bfc6e030..7821bd97 100644 --- a/README.md +++ b/README.md @@ -106,10 +106,20 @@ Docker Compose is the fastest way to get Shumai running. You do not need to clon > [!NOTE] > By default, the Docker Compose setups use CPU software transcoding. If your server has a GPU (NVIDIA NVENC, Intel QuickSync, AMD/Intel VA-API, or Rockchip RKMPP) and you want to enable GPU hardware acceleration, check the [Hardware Transcoding Guide](https://docs.shumai.one/configuration/hardware-transcoding). -3. Configure environment variables (optional): +3. Configure environment variables: - * `SHUMAI_SERVER_PORT` controls the port the Shumai server listens on. The default is `3000`. - * By default, Shumai uses the bundled local storage service in this Docker Compose setup. To use an external S3-compatible storage instead, configure the environment variables for your S3 provider. Here are examples for AWS S3 and Cloudflare R2: + * `BETTER_AUTH_URL`: The public base URL of your Shumai server (e.g., `https://shumai.example.com` or `http://12.34.56.78:3000`). This is required for remote servers and production deployments. + * `AWS_ENDPOINT_URL_S3`: By default, `AWS_ENDPOINT_URL_S3` is set to `http://localhost:{SHUMAI_SERVER_PORT}`. When you use local storage (the default storage backend with `STORAGE_BACKEND: local`) and expose Shumai on a remote server, custom domain, or custom host/port combination, set `AWS_ENDPOINT_URL_S3` to the external URL that client browsers will use to upload and download files. + + For example, if you change the port mapping in `docker-compose.yaml` from `3000:3000` to `12345:3000` and deploy on a server with IP address `12.34.56.78`, set: + + ```yaml + AWS_ENDPOINT_URL_S3: http://12.34.56.78:12345 + ``` + + This value must be reachable from client browsers and should include the externally exposed port. + * `SHUMAI_SERVER_PORT`: Controls the port the Shumai server listens on. The default is `3000`. + * **External S3 Storage**: By default, Shumai uses the bundled local storage service in this Docker Compose setup. To use an external S3-compatible storage instead, configure the environment variables for your S3 provider. Here are examples for AWS S3 and Cloudflare R2: **AWS S3 Example:** ```yaml @@ -129,21 +139,12 @@ Docker Compose is the fastest way to get Shumai running. You do not need to clon S3_SECRET_ACCESS_KEY: your-secret-access-key AWS_ENDPOINT_URL_S3: https://.r2.cloudflarestorage.com ``` - * By default, `AWS_ENDPOINT_URL_S3` is set to: `http://localhost:{SHUMAI_SERVER_PORT}`, if you use local storage and expose Shumai on a custom host/port combination, set `AWS_ENDPOINT_URL_S3` to the external URL that browsers will use to upload files. - For example, if you change the port mapping in `docker-compose.yaml` from `3000:3000` to `12345:3000` and deploy on a server with IP address `12.34.56.78`, set: - - ``` - AWS_ENDPOINT_URL_S3: http://12.34.56.78:12345 - ``` - - This value must be reachable from client browsers and should include the externally exposed port. - -5. Start the services in detached mode: +4. Start the services in detached mode: ```bash docker compose up -d ``` -6. Open your browser and access Shumai at `http://localhost:3000` (or `http://:3000` for remote deployments). +5. Open your browser and access Shumai at `http://localhost:3000` (or `http://:3000` for remote deployments). #### **Scaling Transcode Workers (Parallel Processing)** @@ -200,6 +201,7 @@ To set up Shumai locally for development: ```env DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public BETTER_AUTH_SECRET=ySxs7DxzHDZBbeeHNPEwBuspYwipBqz5Gk5XdBjNhWw= + BETTER_AUTH_URL=http://localhost:3000 STORAGE_BACKEND=local SHUMAI_SERVER_PORT=3000 AWS_ENDPOINT_URL_S3=http://localhost:3000 diff --git a/README_CN.md b/README_CN.md index bd8e2017..0e4dfa5b 100644 --- a/README_CN.md +++ b/README_CN.md @@ -125,39 +125,38 @@ https://gh-proxy.com/https://raw.githubusercontent.com/shumaiOne/shumai/main/REA > 默认的 Docker Compose 配置使用基于 CPU 的软件转码。如果你的服务器支持硬件加速(NVIDIA NVENC、Intel QuickSync、AMD/Intel VA-API 或 Rockchip RKMPP),请参考 [硬件加速转码配置文档](https://docs.shumai.one/configuration/hardware-transcoding)。 -3. 配置环境变量(可选): +3. 配置环境变量: + * `BETTER_AUTH_URL`:Shumai 服务的外部公开访问地址(例如 `https://shumai.example.com` 或 `http://12.34.56.78:3000`)。在远程服务器或生产环境中部署时必须配置该变量。 + * `AWS_ENDPOINT_URL_S3`:默认情况下指向 `http://localhost:{SHUMAI_SERVER_PORT}`。当你使用本地存储(即默认配置 `STORAGE_BACKEND: local`)并在远程服务器、自定义域名或自定义端口上公开访问 Shumai 时,请将 `AWS_ENDPOINT_URL_S3` 设置为客户端浏览器上传和下载文件所使用的外部 URL。 + + 例如,如果你将 `docker-compose.yaml` 中的端口映射从 `3000:3000` 修改为了 `12345:3000`,且服务部署在 IP 为 `12.34.56.78` 的服务器上,请进行如下设置: + + ```yaml + AWS_ENDPOINT_URL_S3: http://12.34.56.78:12345 + ``` + + 此地址必须能够从客户端浏览器正常访问,且必须包含对外暴露的实际端口号。 * `SHUMAI_SERVER_PORT` 用于控制 Shumai 服务监听的端口,默认值为 `3000`。 - * 默认情况下,该 Docker Compose 部署会自动启用集成的本地存储。若想切换至外部 S3 兼容存储,请在 `environment` 中配置 S3 提供商的环境变量。以下是 AWS S3 和 Cloudflare R2 的配置示例: + * **外部 S3 存储**:默认情况下,该 Docker Compose 部署会自动启用集成的本地存储。若想切换至外部 S3 兼容存储,请在 `environment` 中配置 S3 提供商的环境变量。以下是 AWS S3 和 Cloudflare R2 的配置示例: - **AWS S3 示例:** - ```yaml - STORAGE_BACKEND: s3 - S3_BUCKET: your-bucket-name - S3_ACCESS_KEY_ID: your-access-key-id - S3_SECRET_ACCESS_KEY: your-secret-access-key - AWS_ENDPOINT_URL_S3: https://s3.us-east-1.amazonaws.com - ``` + **AWS S3 示例:** + ```yaml + STORAGE_BACKEND: s3 + S3_BUCKET: your-bucket-name + S3_ACCESS_KEY_ID: your-access-key-id + S3_SECRET_ACCESS_KEY: your-secret-access-key + AWS_ENDPOINT_URL_S3: https://s3.us-east-1.amazonaws.com + ``` - **Cloudflare R2 示例:** - ```yaml - STORAGE_BACKEND: s3 - S3_BUCKET: your-bucket-name - S3_REGION: auto - S3_ACCESS_KEY_ID: your-access-key-id - S3_SECRET_ACCESS_KEY: your-secret-access-key - AWS_ENDPOINT_URL_S3: https://.r2.cloudflarestorage.com - ``` - - -* 默认情况下 `AWS_ENDPOINT_URL_S3` 指向 `http://localhost:{SHUMAI_SERVER_PORT}`。如果你使用本地存储,但希望在自定义的主机名或端口上公开访问 Shumai,请务必将 `AWS_ENDPOINT_URL_S3` 修改为浏览器访问该服务时所使用的外部 URL。注意就算不使用S3存储(STORAGE_BACKEND: local)也需要正确配置该环境变量。 -例如,如果你将 `docker-compose.yaml` 中的端口映射从 `3000:3000` 修改为了 `12345:3000`,且服务部署在 IP 为 `12.34.56.78` 的服务器上,请进行如下设置: - ``` - AWS_ENDPOINT_URL_S3: http://12.34.56.78:12345 - - ``` - - -此地址必须能够从客户端浏览器正常访问,且必须包含对外暴露的实际端口号。 + **Cloudflare R2 示例:** + ```yaml + STORAGE_BACKEND: s3 + S3_BUCKET: your-bucket-name + S3_REGION: auto + S3_ACCESS_KEY_ID: your-access-key-id + S3_SECRET_ACCESS_KEY: your-secret-access-key + AWS_ENDPOINT_URL_S3: https://.r2.cloudflarestorage.com + ``` 4. 在后台启动所有服务: @@ -227,6 +226,7 @@ Shumai 提供了独立的转码 Worker 镜像(支持 Docker Hub 与阿里云 ```env DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public BETTER_AUTH_SECRET=ySxs7DxzHDZBbeeHNPEwBuspYwipBqz5Gk5XdBjNhWw= + BETTER_AUTH_URL=http://localhost:3000 STORAGE_BACKEND=local SHUMAI_SERVER_PORT=3000 AWS_ENDPOINT_URL_S3=http://localhost:3000 diff --git a/docker-compose/local/docker-compose-aliyun.embedding.yaml b/docker-compose/local/docker-compose-aliyun.embedding.yaml index 07e7610a..c1835636 100644 --- a/docker-compose/local/docker-compose-aliyun.embedding.yaml +++ b/docker-compose/local/docker-compose-aliyun.embedding.yaml @@ -49,6 +49,7 @@ services: # Better Auth BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw= + BETTER_AUTH_URL: http://localhost:3000 # Sandbox Configuration ENABLE_WEAKER_NESTED_SANDBOX: 'true' diff --git a/docker-compose/local/docker-compose-aliyun.full.yaml b/docker-compose/local/docker-compose-aliyun.full.yaml index 38db1a7b..08d0761e 100644 --- a/docker-compose/local/docker-compose-aliyun.full.yaml +++ b/docker-compose/local/docker-compose-aliyun.full.yaml @@ -59,6 +59,7 @@ services: # Better Auth BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw= + BETTER_AUTH_URL: http://localhost:3000 # Sandbox Configuration ENABLE_WEAKER_NESTED_SANDBOX: 'true' diff --git a/docker-compose/local/docker-compose-aliyun.yaml b/docker-compose/local/docker-compose-aliyun.yaml index c0c13a2b..60b2d4c8 100644 --- a/docker-compose/local/docker-compose-aliyun.yaml +++ b/docker-compose/local/docker-compose-aliyun.yaml @@ -44,6 +44,7 @@ services: # Better Auth BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw= + BETTER_AUTH_URL: http://localhost:3000 # Sandbox Configuration ENABLE_WEAKER_NESTED_SANDBOX: 'true' diff --git a/docker-compose/local/docker-compose.embedding.yaml b/docker-compose/local/docker-compose.embedding.yaml index cd730225..e6837934 100644 --- a/docker-compose/local/docker-compose.embedding.yaml +++ b/docker-compose/local/docker-compose.embedding.yaml @@ -49,6 +49,7 @@ services: # Better Auth BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw= + BETTER_AUTH_URL: http://localhost:3000 # Sandbox Configuration ENABLE_WEAKER_NESTED_SANDBOX: 'true' diff --git a/docker-compose/local/docker-compose.full.yaml b/docker-compose/local/docker-compose.full.yaml index 5883a2a4..38f41c0a 100644 --- a/docker-compose/local/docker-compose.full.yaml +++ b/docker-compose/local/docker-compose.full.yaml @@ -59,6 +59,7 @@ services: # Better Auth BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw= + BETTER_AUTH_URL: http://localhost:3000 # Sandbox Configuration ENABLE_WEAKER_NESTED_SANDBOX: 'true' diff --git a/docker-compose/local/docker-compose.yaml b/docker-compose/local/docker-compose.yaml index 141364d9..84b5d8e7 100644 --- a/docker-compose/local/docker-compose.yaml +++ b/docker-compose/local/docker-compose.yaml @@ -44,6 +44,7 @@ services: # Better Auth BETTER_AUTH_SECRET: qSxs9DxzHDZBbeeTNPEwBuspYwipBqz5Gk5XdBjNhWw= + BETTER_AUTH_URL: http://localhost:3000 # Sandbox Configuration ENABLE_WEAKER_NESTED_SANDBOX: 'true' diff --git a/docs/configuration/env-variables.mdx b/docs/configuration/env-variables.mdx index 1a6297b9..a0923b96 100644 --- a/docs/configuration/env-variables.mdx +++ b/docs/configuration/env-variables.mdx @@ -26,11 +26,11 @@ Shumai is configured via environment variables. You can define these variables i ## Authentication & Application Config -| Variable | Description | Example / Default | -| :------------------- | :--------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------- | -| `BETTER_AUTH_SECRET` | A secure, random 32-byte secret used to sign session cookies and JWTs. | Generate with `openssl rand -base64 32` | -| `BETTER_AUTH_URL` | The public URL of your Hono backend API. Used for redirect flows and notification email links fallback. | `http://localhost:3000` (respects `SHUMAI_SERVER_PORT` fallback) | -| `APP_URL` | The public URL of the web application. Used for notification email deep links (falls back to `BETTER_AUTH_URL`). | `https://shumai.example.com` | +| Variable | Description | Example / Default | +| :------------------- | :------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------------- | +| `BETTER_AUTH_SECRET` | A secure, random 32-byte secret used to sign session cookies and JWTs. | Generate with `openssl rand -base64 32` | +| `BETTER_AUTH_URL` | The public URL of your Shumai server. Used for MCP OAuth 2.1 discovery, redirect flows, password reset links, and email notifications. | `http://localhost:3000` (respects `SHUMAI_SERVER_PORT` fallback) | +| `APP_URL` | The public URL of the web application. Used for notification email deep links (falls back to `BETTER_AUTH_URL`). | `https://shumai.example.com` | --- diff --git a/docs/getting-started/quickstart.mdx b/docs/getting-started/quickstart.mdx index bba040ac..fdb4033a 100644 --- a/docs/getting-started/quickstart.mdx +++ b/docs/getting-started/quickstart.mdx @@ -9,23 +9,84 @@ We support three installation options. For most administrators, **Docker Compose ## Option 1: Docker Compose (Recommended) -Docker Compose automatically configures and spins up all required services (the web application, a database, and local storage). +Docker Compose is the fastest way to get Shumai running. You do not need to clone the repository or install packages manually. Ensure you have [Docker](https://docs.docker.com/get-docker/) and [Docker Compose](https://docs.docker.com/compose/install/) installed, then follow these steps: -1. **Download the Configuration**: +1. **Create Directory**: + Create and navigate to a new directory for your configuration and data volumes: ```bash - curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose.yaml + mkdir shumai && cd shumai ``` - > [!TIP] - > **Hardware Acceleration:** By default, the standard Compose stack uses CPU-based software transcoding. If your server has a dedicated or integrated GPU (NVIDIA, Intel, AMD, or Rockchip), see the [Hardware Transcoding Guide](/configuration/hardware-transcoding) to enable GPU acceleration via the companion `hwaccel.transcoding.yaml`. +2. **Download the Configuration**: + Download your preferred `docker-compose.yaml` configuration based on your needs and available RAM: + + - **Lite (Suggested RAM: 2 GB+)**: Minimal deployment containing Shumai and PostgreSQL database. + + ```bash + curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose.yaml + ``` + + - **With Embedding Server (Suggested RAM: 4 GB+)**: Adds local multimodal semantic search powered by `shumaiOne/embedding-server` (`EmbeddingGemma 2 740M`). + + ```bash + curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose.embedding.yaml + ``` + + - **Full (Suggested RAM: 8 GB+)**: All-in-one setup including `embedding-server`, `turntable-renderer` (3D previews), and `Gotenberg` (document PDF proxying). + ```bash + curl -o docker-compose.yaml https://raw.githubusercontent.com/shumaiOne/shumai/main/docker-compose/local/docker-compose.full.yaml + ``` + + > [!NOTE] + > By default, the Docker Compose setups use CPU software transcoding. If your server has a GPU (NVIDIA NVENC, Intel QuickSync, AMD/Intel VA-API, or Rockchip RKMPP) and you want to enable GPU hardware acceleration, check the [Hardware Transcoding Guide](/configuration/hardware-transcoding). + +3. **Configure Environment Variables**: + Open `docker-compose.yaml` to adjust environment variables for your deployment: + + - `BETTER_AUTH_URL`: The public base URL of your Shumai server (e.g., `https://shumai.example.com` or `http://12.34.56.78:3000`). This is required for remote servers and production deployments. + - `AWS_ENDPOINT_URL_S3`: By default, `AWS_ENDPOINT_URL_S3` is set to `http://localhost:{SHUMAI_SERVER_PORT}`. When you use local storage (the default storage backend with `STORAGE_BACKEND: local`) and expose Shumai on a remote server, custom domain, or custom host/port combination, set `AWS_ENDPOINT_URL_S3` to the external URL that client browsers will use to upload and download files. + + For example, if you change the port mapping in `docker-compose.yaml` from `3000:3000` to `12345:3000` and deploy on a server with IP address `12.34.56.78`, set: + + ```yaml + AWS_ENDPOINT_URL_S3: http://12.34.56.78:12345 + ``` + + This value must be reachable from client browsers and should include the externally exposed port. + + - `SHUMAI_SERVER_PORT`: Controls the port the Shumai server listens on. The default is `3000`. + - **External S3 Storage**: By default, Shumai uses the bundled local storage service in this Docker Compose setup. To use an external S3-compatible storage instead, configure the environment variables for your S3 provider. Here are examples for AWS S3 and Cloudflare R2: + + **AWS S3 Example:** + + ```yaml + STORAGE_BACKEND: s3 + S3_BUCKET: your-bucket-name + S3_ACCESS_KEY_ID: your-access-key-id + S3_SECRET_ACCESS_KEY: your-secret-access-key + AWS_ENDPOINT_URL_S3: https://s3.us-east-1.amazonaws.com + ``` + + **Cloudflare R2 Example:** + + ```yaml + STORAGE_BACKEND: s3 + S3_BUCKET: your-bucket-name + S3_REGION: auto + S3_ACCESS_KEY_ID: your-access-key-id + S3_SECRET_ACCESS_KEY: your-secret-access-key + AWS_ENDPOINT_URL_S3: https://.r2.cloudflarestorage.com + ``` + +4. **Start the Services**: -2. **Start the Services**: ```bash docker compose up -d ``` -3. **Access the Dashboard**: - Open your browser and navigate to **`http://localhost:3000`** to access the Shumai Web UI. + +5. **Access the Dashboard**: + Open your browser and navigate to **`http://localhost:3000`** (or `http://:3000` for remote deployments) to access the Shumai Web UI. ### Production Deployments (Distributed Workers) @@ -79,12 +140,13 @@ To handle media processing and security isolation, the host machine must have th ```env DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public BETTER_AUTH_SECRET=your-random-32-character-secret-key + BETTER_AUTH_URL=http://localhost:3000 STORAGE_BACKEND=local AWS_ENDPOINT_URL_S3=http://localhost:3000 ``` > [!TIP] - > `AWS_ENDPOINT_URL_S3` must include the scheme and port (e.g. `http://localhost:3000`) where Shumai is publicly accessible. This URL is used directly by client browsers for file uploads. + > `BETTER_AUTH_URL` should be set to your public server address for remote or production deployments. `AWS_ENDPOINT_URL_S3` must include the scheme and port (e.g. `http://localhost:3000` or `http://12.34.56.78:3000`) where Shumai is publicly accessible, as client browsers use this URL directly for file uploads. 4. **Start Shumai**: @@ -101,25 +163,46 @@ To handle media processing and security isolation, the host machine must have th ## Option 3: Run from Source (For Developers) -If you wish to run Shumai locally to customize the platform or contribute: +To set up Shumai locally for development: -1. **Clone the Repository**: +1. **Clone the Repository and Install Dependencies**: ```bash git clone https://github.com/shumaiOne/shumai.git cd shumai - ``` -2. **Install Dependencies**: - ```bash bun install ``` -3. **Start a Database**: - Launch a local database container (as described in the NPM section above). -4. **Run Migrations**: - Set up your `.env` file at the root, then apply database migrations: +2. **Start a Database**: + Launch a local database container (as described in the NPM section above): + ```bash + docker run --name shumai_postgres \ + -e POSTGRES_USER=shumai \ + -e POSTGRES_PASSWORD=shumai_password \ + -e POSTGRES_DB=shumai_db \ + -p 5432:5432 \ + -d pgvector/pgvector:pg18 + ``` +3. **Create Environment File**: + Create a `.env` file at the root of the workspace: + ```env + DATABASE_URL=postgresql://shumai:shumai_password@localhost:5432/shumai_db?schema=public + BETTER_AUTH_SECRET=ySxs7DxzHDZBbeeHNPEwBuspYwipBqz5Gk5XdBjNhWw= + BETTER_AUTH_URL=http://localhost:3000 + STORAGE_BACKEND=local + SHUMAI_SERVER_PORT=3000 + AWS_ENDPOINT_URL_S3=http://localhost:3000 + ``` +4. **Generate i18n Translations**: + ```bash + bun run i18n:compile + ``` + > [!TIP] + > During frontend development, you can run `bun run i18n:watch` in a separate terminal to automatically recompile translation files whenever you edit strings in `packages/webui/messages/`. +5. **Run Migrations**: + Apply database schema migrations: ```bash bun run db:migrate ``` -5. **Start the Dev Server**: +6. **Start the Dev Server**: ```bash bun run dev ``` diff --git a/packages/api/src/mcp-server.oauth.test.ts b/packages/api/src/mcp-server.oauth.test.ts index b47790db..c4aa7eba 100644 --- a/packages/api/src/mcp-server.oauth.test.ts +++ b/packages/api/src/mcp-server.oauth.test.ts @@ -50,6 +50,10 @@ describe('MCP server OAuth 2.1 & Dual Auth', () => { it('serves RFC 8414 and RFC 9728 discovery endpoints', async () => { // RFC 9728 Protected Resource Metadata + expect(authBaseUrl.endsWith('/')).toBe(false) + expect(mcpResource).not.toContain('//mcp') + expect(mcpResource.endsWith('/mcp')).toBe(true) + const resourceMetaRes = await app.request('/.well-known/oauth-protected-resource') expect(resourceMetaRes.status).toBe(200) const resourceMeta = await resourceMetaRes.json() diff --git a/packages/core/src/auth/auth.test.ts b/packages/core/src/auth/auth.test.ts index 8a7399ef..b471279e 100644 --- a/packages/core/src/auth/auth.test.ts +++ b/packages/core/src/auth/auth.test.ts @@ -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 + } + } + }) }) diff --git a/packages/core/src/auth/auth.ts b/packages/core/src/auth/auth.ts index fc9e9c58..4d38114d 100644 --- a/packages/core/src/auth/auth.ts +++ b/packages/core/src/auth/auth.ts @@ -16,8 +16,9 @@ interface GeneratedResetLink { const resetLinkStorage = new AsyncLocalStorage() -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}` } diff --git a/packages/core/src/mcp/mcp-oauth-provider.test.ts b/packages/core/src/mcp/mcp-oauth-provider.test.ts index 3fccf4d3..4a7146a3 100644 --- a/packages/core/src/mcp/mcp-oauth-provider.test.ts +++ b/packages/core/src/mcp/mcp-oauth-provider.test.ts @@ -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 + } + } + }) }) diff --git a/packages/core/src/mcp/mcp-oauth-provider.ts b/packages/core/src/mcp/mcp-oauth-provider.ts index 636bf068..0e4f67b9 100644 --- a/packages/core/src/mcp/mcp-oauth-provider.ts +++ b/packages/core/src/mcp/mcp-oauth-provider.ts @@ -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. */