Skip to content
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

Improve hostname validation #4118

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions components/gitpod-protocol/src/util/gitpod-host-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,13 @@ export class GitpodHostUrl {

get workspaceId(): string | undefined {
const hostSegs = this.url.host.split(".");
if (hostSegs.length > 1 && hostSegs[0].match(workspaceIDRegex)) {
// URL has a workspace prefix
return hostSegs[0];
if (hostSegs.length > 1) {
const matchResults = hostSegs[0].match(workspaceIDRegex);
if (matchResults) {
// URL has a workspace prefix
// port prefixes are excluded
return matchResults[0];
}
}

const pathSegs = this.url.pathname.split("/")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { BitbucketTokenHelper } from "./bitbucket-token-handler";
const expect = chai.expect;
import { skipIfEnvVarNotSet } from "@gitpod/gitpod-protocol/lib/util/skip-if";

@suite.only(timeout(10000), skipIfEnvVarNotSet("GITPOD_TEST_TOKEN_BITBUCKET"))
Copy link
Member

Choose a reason for hiding this comment

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

💯

@suite(timeout(10000), skipIfEnvVarNotSet("GITPOD_TEST_TOKEN_BITBUCKET"))
class TestBitbucketContextParser {

protected parser: BitbucketContextParser;
Expand Down
11 changes: 9 additions & 2 deletions components/server/src/express-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { log } from '@gitpod/gitpod-protocol/lib/util/logging';
import { URL } from 'url';
import * as express from 'express';
import * as crypto from 'crypto';
import { GitpodHostUrl, workspaceIDRegex } from '@gitpod/gitpod-protocol/lib/util/gitpod-host-url';
import { GitpodHostUrl } from '@gitpod/gitpod-protocol/lib/util/gitpod-host-url';

export const pingPong: WsRequestHandler = (ws, req, next) => {
let pingSentTimer: any;
Expand Down Expand Up @@ -91,7 +91,14 @@ const looksLikeWorkspaceHostname = (originHostname: URL, gitpodHostName: string)
return false;
}
const url = new GitpodHostUrl(originHostname);
return workspaceIDRegex.test(url.workspaceId || '')
const workspaceId = url.workspaceId;
if (workspaceId) {
const hostname = url.url.hostname as string;
if (hostname.startsWith(workspaceId)) {
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice if we had one place for the workspace URL parsing. But might be out of scope for this PR, as it was here before, too.

Copy link
Member Author

Choose a reason for hiding this comment

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

GitpodHostUrl does the URL parsing. Here we explicitly check, that the workspace-like hostname is not prefixed.
Ah, perhaps you mean to extract the computation of the prefix and move it to GitpodHostUrl? 🤷🏻‍♂️

return true;
}
}
return false;
};

export function saveSession(reqOrSession: express.Request | Express.Session): Promise<void> {
Expand Down