-
Notifications
You must be signed in to change notification settings - Fork 70
[Production Deploy] Tidy up gateway code (#469) #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { createOauthCallbackRouter } from "@director.run/mcp/oauth/oauth-callback-router"; | ||
| import { getLogger } from "@director.run/utilities/logger"; | ||
| import {} from "@director.run/utilities/middleware/index"; | ||
| import { joinURL } from "@director.run/utilities/url"; | ||
| import { auth } from "../auth"; | ||
| import { env } from "../env"; | ||
| import { PlaybookStore } from "../playbooks/playbook-store"; | ||
|
|
||
| const logger = getLogger("OauthClientRouter"); | ||
|
|
||
| export const createOauthClientRouter = ({ | ||
| playbookStore, | ||
| }: { playbookStore: PlaybookStore }) => | ||
| createOauthCallbackRouter({ | ||
| getSession: async (req) => { | ||
| const session = await auth.api.getSession({ | ||
| headers: req.headers as Record<string, string>, | ||
| }); | ||
| if (!session) { | ||
| return null; | ||
| } | ||
| return { userId: session.user.id }; | ||
| }, | ||
| onAuthorizationSuccess: async (factoryId, providerId, code, userId) => { | ||
| await playbookStore.onAuthorizationSuccess( | ||
| factoryId, | ||
| providerId, | ||
| code, | ||
| userId, | ||
| ); | ||
| return { | ||
| redirectUrl: joinURL( | ||
| env.BASE_URL, | ||
| `studio/oauth/${factoryId}/${providerId}/callback`, | ||
| ), | ||
| }; | ||
| }, | ||
| onAuthorizationError: (factoryId, providerId, error) => { | ||
| logger.error({ | ||
| error, | ||
| message: `failed to authorize ${factoryId} ${providerId}: ${error.message}`, | ||
| }); | ||
|
|
||
| return { | ||
| redirectUrl: joinURL( | ||
| env.BASE_URL, | ||
| `studio/oauth/${factoryId}/${providerId}/callback?error=${encodeURIComponent(error.message)}`, | ||
| ), | ||
| }; | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { isDevelopment } from "@director.run/utilities/env"; | ||
| import { getLogger } from "@director.run/utilities/logger"; | ||
| import { spaMiddleware } from "@director.run/utilities/middleware/spa"; | ||
| import express from "express"; | ||
| import { env } from "../env"; | ||
|
|
||
| const logger = getLogger("studio_router"); | ||
|
|
||
| export function createStudioRouter({ | ||
| assetsPath, | ||
| }: { | ||
| assetsPath?: string; | ||
| }): express.Router { | ||
| const router = express.Router(); | ||
| if (assetsPath) { | ||
| logger.debug({ | ||
| message: "serving studio assets from", | ||
| distPath: assetsPath, | ||
| }); | ||
| router.use( | ||
| "/studio", | ||
| spaMiddleware({ | ||
| distPath: assetsPath, | ||
| config: { | ||
| basePath: "/studio", | ||
| gatewayUrl: env.BASE_URL, | ||
| registryUrl: env.REGISTRY_URL, | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| router.get("/", (_, res) => { | ||
| res.redirect("/studio"); | ||
| }); | ||
| } else if (isDevelopment()) { | ||
| router.use("/studio", (req, res) => { | ||
| res.redirect(`http://localhost:3000${req.originalUrl}`); | ||
| }); | ||
| } | ||
|
|
||
| return router; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Server-side URL redirect Medium
Copilot Autofix
AI 6 months ago
The best way to fix this problem is to ensure that only local, trusted paths are appended to the redirect URL, and block or normalize any user input that could result in an external or unexpected redirect. Since the code is proxying requests to a local development server at
localhost:3000, simply redirecting to the same path without allowing the user to control the host or scheme is ideal. We can ensure thatreq.originalUrlis always a path under/studio, and avoid redirecting to user-injected absolute URLs.To implement this, parse and normalize
req.originalUrlto extract only the path after/studio, and use it to construct the redirect. Alternatively, reject any requests wherereq.originalUrlcontains suspicious elements (//,http, hosts, etc.), or always redirect to a canonical/studiopath with a clean suffix.Changes needed in
apps/gateway/src/routers/studio.ts:req.originalUrlto only take the “rest” of the path after/studio, ensuring it does not contain any dangerous characters (like//or leading protocols), and append it to the local redirect URL.urlor the globalURL), but minimal string manipulation suffices here.