GITHUB_OAUTH_CALLBACK_URL's hardcoded default points at the frontend's port 3000, not the backend's 4000, in two places
Maintainer thường phản hồi trong vòng 1 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 85/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- node.js, typescript
- Lĩnh vực
- authentication, backend
Hướng nghiên cứu
Đọc src/config/configuration.ts và src/auth/strategies/github.strategy.ts để lần theo các giá trị mặc định của URL callback, sau đó kiểm tra route do AuthController đăng ký và các bài kiểm thử cấu hình hiện có. Cập nhật các giá trị mặc định để callback dựa theo APP_URL và chỉ có một nguồn dữ liệu chuẩn; thêm trường hợp được đề xuất vào configuration.spec.ts. Hoàn tất khi callback mặc định sử dụng origin của backend và bài kiểm thử xác nhận điều đó.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Location
src/config/configuration.ts:90-92— thegithub.oauthCallbackUrldefaultsrc/auth/strategies/github.strategy.ts:19-21— a second copy of the same defaultsrc/config/configuration.ts:73-74—appUrl/frontendUrldefaults, for contrast.env.example:34— the documented value
Problem
The config factory defaults the OAuth callback to the frontend's origin:
appUrl: process.env.APP_URL ?? 'http://localhost:4000', // backend
frontendUrl: process.env.FRONTEND_URL ?? 'http://localhost:3000', // Next.js
...
oauthCallbackUrl:
process.env.GITHUB_OAUTH_CALLBACK_URL ||
'http://localhost:3000/api/auth/github/callback', // :3000, not :4000
GET /api/auth/github/callback is a backend route (AuthController.githubCallback, served under the api global prefix set at main.ts:42), and the backend listens on port — default 4000. The default callback therefore names a path that exists on the backend, on the origin of the frontend.
.env.example:34 has the correct value (http://localhost:4000/api/auth/github/callback), which confirms :3000 is a mistake rather than an intentional proxy arrangement — and it means the two sources of truth for the same setting disagree.
The wrong literal appears twice: once in configuration.ts and again as the strategy's own fallback at github.strategy.ts:19-21, so fixing only the config file leaves the second copy to take over whenever githubConfig?.oauthCallbackUrl is falsy.
Why it matters
OAuth redirect URIs are matched exactly by the provider. The callback URL has to agree in three places — the GitHub OAuth app registration, the value Passport sends as redirect_uri on the authorize request, and the route that actually handles the redirect. This default breaks that agreement in the one scenario the defaults exist for: a developer running the stack without setting the variable.
What a new contributor following the README experiences:
- If their GitHub OAuth app is registered with
http://localhost:4000/api/auth/github/callback(the value.env.exampletells them to use) but they do not setGITHUB_OAUTH_CALLBACK_URL, Passport sendsredirect_uri=http://localhost:3000/..., and GitHub refuses the authorize request outright with "The redirect_uri MUST match the registered callback URL for this application". Login never starts. - If they instead register
http://localhost:3000/...to match the default, GitHub accepts the handshake and sends the browser to the Next.js dev server with?code=<github-oauth-code>. Next.js has no/api/auth/github/callbackroute, so the developer gets a 404 page, andAuthController.githubCallbacknever runs — noUserrow, no JWT, no cookie. The failure looks like a frontend routing problem, which is a long way from the real cause. - In either case the GitHub authorization code is delivered to the wrong origin. For a local dev setup that is merely confusing; for anyone who copies this pattern into a deployed environment (setting
APP_URLandFRONTEND_URLcorrectly but leaving the callback unset), the authorization code is handed to the public frontend origin instead of the backend that holds the client secret.
The duplicated literal makes this harder to notice than it should be: grep GITHUB_OAUTH_CALLBACK_URL finds the config entry, and a reader who fixes it there will not realise the strategy still carries its own copy.
Reproduction / failure path
- Register a GitHub OAuth app with callback
http://localhost:4000/api/auth/github/callback(as.env.exampleinstructs). - Set
GITHUB_CLIENT_IDandGITHUB_CLIENT_SECRETbut deliberately leaveGITHUB_OAUTH_CALLBACK_URLunset. - Start the backend (
npm run start:dev, listening on 4000) and visithttp://localhost:4000/api/auth/github. - Observed: the browser is sent to
https://github.com/login/oauth/authorize?...&redirect_uri=http%3A%2F%2Flocalhost%3A3000%2Fapi%2Fauth%2Fgithub%2Fcallback, and GitHub responds with a redirect_uri mismatch error. Expected:redirect_urion port 4000, matching the registration and the route that serves it.
Suggested fix
Derive the default from appUrl so it can never drift from the port the backend actually listens on:
const appUrl = process.env.APP_URL ?? 'http://localhost:4000';
...
oauthCallbackUrl:
process.env.GITHUB_OAUTH_CALLBACK_URL ?? `${appUrl}/api/auth/github/callback`,
That also keeps the default correct when PORT/APP_URL are changed, which a hardcoded literal cannot.
Remove the duplicate fallback at src/auth/strategies/github.strategy.ts:19-21 entirely and read the configured value directly — a single source of truth. (The same file's clientId/clientSecret fallbacks have their own problem; see the separate issue about the dead production boot guard.)
Add a configuration.spec.ts case asserting that with APP_URL set and GITHUB_OAUTH_CALLBACK_URL unset, github.oauthCallbackUrl is rooted at APP_URL — and that the resulting path matches the route AuthController actually registers.
- Ngôn ngữ chính
- TypeScript
- Star
- 2
- Fork
- 90
- Merge trung bình
- 31 phút
- Pull request đã merge (30 ngày)
- 20
Chuẩn bị môi trường
- Có Dockerfile hoặc tệp Docker Compose
- Không có mẫu pull request
- Không có hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của MergeFi/backend
-
bug security
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 74/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug testing
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug security
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 79/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của MergeFi/backend
Issue tương tự
-
ログイン中の404画面にヘッダーとナビを出すĐang mởCodex使用可 コード
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
ryuki-moro/Nicomaru#218 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
sveltejs/svelte-eslint-parser#1008 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
silverbucket/inbox-rs#351 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug]: The H2 series' and the H3C's fan 3 is labelled "Chamber Fan", Bambu calls it the exhaust fanĐang mởarea:ui
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug front P2
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
invoicerr-app/invoicerr#964 ·
Maintainer thường phản hồi trong vòng 1 ngày