8000 fix(site): sanitize login redirect by coadler · Pull Request #15208 · coder/coder · GitHub
[go: up one dir, main page]

Skip to content

fix(site): sanitize login redirect #15208

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 10 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"storybook": "pnpm run -C site/ storybook"
},
"devDependencies": {
"@biomejs/biome": "1.9.4",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

biome extension doesn't work in vscode unless you have dep in root dir 😢

"prettier": "3.3.3"
}
}
91 changes: 91 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 21 additions & 36 deletions site/src/pages/LoginPage/LoginPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export const LoginPage: FC = () => {
const navigate = useNavigate();
const { metadata } = useEmbeddedMetadata();
const buildInfoQuery = useQuery(buildInfo(metadata["build-info"]));
let redirectUrl: URL | null = null;
try {
redirectUrl = new URL(redirectTo);
} catch {
// Do nothing
}

const isApiRouteRedirect = redirectTo.startsWith("/api/v2");

useEffect(() => {
if (!buildInfoQuery.data || isSignedIn) {
Expand All @@ -41,42 +49,19 @@ export const LoginPage: FC = () => {
});
}, [isSignedIn, buildInfoQuery.data, user?.id]);

if (isSignedIn) {
if (buildInfoQuery.data) {
// This uses `navigator.sendBeacon`, so window.href
// will not stop the request from being sent!
sendDeploymentEvent(buildInfoQuery.data, {
type: "deployment_login",
user_id: user?.id,
});
}

// If the redirect is going to a workspace application, and we
// are missing authentication, then we need to change the href location
// to trigger a HTTP request. This allows the BE to generate the auth
// cookie required. Similarly for the OAuth2 exchange as the authorization
// page is serve 8000 d by the backend.
// If no redirect is present, then ignore this branched logic.
if (redirectTo !== "" && redirectTo !== "/") {
try {
// This catches any absolute redirects. Relative redirects
// will fail the try/catch. Subdomain apps are absolute redirects.
const redirectURL = new URL(redirectTo);
if (redirectURL.host !== window.location.host) {
window.location.href = redirectTo;
return null;
}
} catch {
// Do nothing
}
// Path based apps and OAuth2.
if (redirectTo.includes("/apps/") || redirectTo.includes("/oauth2/")) {
window.location.href = redirectTo;
return null;
}
}

return <Navigate to={redirectTo} replace />;
// The reason we need `window.location.href` for api redirects is that we
// need the page to reload and make a request to the backend. If we use
// `<Navigate>` react would handle the redirect itself and never request the
// page from the backend.
if (isSignedIn && isApiRouteRedirect) {
const sanitizedUrl = new URL(redirectTo, window.location.origin);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not work for subdomain apps though right? If they are on another domain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They're not on another domain. The redirect from a workspace proxy subdomain app is to /api/v2/applications/auth-redirect. The redirect sanitization happens there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll trust you 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be wrong but that's what I always noticed in my testing.

window.location.href = sanitizedUrl.pathname + sanitizedUrl.search;
return null;
}
if (isSignedIn && !isApiRouteRedirect) {
return (
<Navigate to={redirectUrl ? redirectUrl.pathname : redirectTo} replace />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I poked around a bit more of the code, and I'm just making sure: there's a second file that calls the retrieveRedirect function (LoginPageView.tsx). Do we need to update anything around that?

Because right now, the control flow is set up so that the component:

  1. Gets the redirectTo value from the search param's redirect param
  2. Passes it to the SignInForm
  3. Passes that to the OAuthSignInForm
  4. As long as GitHub is enabled as an auth method, a button shows up with an href that uses the raw redirectTo as a param

Not sure how the callback logic works, but would the updates to this file catch any issues from there?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good find. This is being sent directly to the backend which handles approving the redirect URLs, so it doesn't need any validation from the frontend.

);
}

if (isConfiguringTheFirstUser) {
Expand Down
Loading
0