Skip to content

Commit b1c53ea

Browse files
committed
[server] improve hostname validation
1 parent 4ac8ec4 commit b1c53ea

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

components/gitpod-protocol/src/util/gitpod-host-url.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,13 @@ export class GitpodHostUrl {
128128

129129
get workspaceId(): string | undefined {
130130
const hostSegs = this.url.host.split(".");
131-
if (hostSegs.length > 1 && hostSegs[0].match(workspaceIDRegex)) {
132-
// URL has a workspace prefix
133-
return hostSegs[0];
131+
if (hostSegs.length > 1) {
132+
const matchResults = hostSegs[0].match(workspaceIDRegex);
133+
if (matchResults) {
134+
// URL has a workspace prefix
135+
// port prefixes are excluded
136+
return matchResults[0];
137+
}
134138
}
135139

136140
const pathSegs = this.url.pathname.split("/")

components/server/src/bitbucket/bitbucket-context-parser.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { BitbucketTokenHelper } from "./bitbucket-token-handler";
1818
const expect = chai.expect;
1919
import { skipIfEnvVarNotSet } from "@gitpod/gitpod-protocol/lib/util/skip-if";
2020

21-
@suite.only(timeout(10000), skipIfEnvVarNotSet("GITPOD_TEST_TOKEN_BITBUCKET"))
21+
@suite(timeout(10000), skipIfEnvVarNotSet("GITPOD_TEST_TOKEN_BITBUCKET"))
2222
class TestBitbucketContextParser {
2323

2424
protected parser: BitbucketContextParser;

components/server/src/express-util.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { log } from '@gitpod/gitpod-protocol/lib/util/logging';
99
import { URL } from 'url';
1010
import * as express from 'express';
1111
import * as crypto from 'crypto';
12-
import { GitpodHostUrl, workspaceIDRegex } from '@gitpod/gitpod-protocol/lib/util/gitpod-host-url';
12+
import { GitpodHostUrl } from '@gitpod/gitpod-protocol/lib/util/gitpod-host-url';
1313

1414
export const pingPong: WsRequestHandler = (ws, req, next) => {
1515
let pingSentTimer: any;
@@ -91,7 +91,14 @@ const looksLikeWorkspaceHostname = (originHostname: URL, gitpodHostName: string)
9191
return false;
9292
}
9393
const url = new GitpodHostUrl(originHostname);
94-
return workspaceIDRegex.test(url.workspaceId || '')
94+
const workspaceId = url.workspaceId;
95+
if (workspaceId) {
96+
const hostname = url.url.hostname as string;
97+
if (hostname.startsWith(workspaceId)) {
98+
return true;
99+
}
100+
}
101+
return false;
95102
};
96103

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

0 commit comments

Comments
 (0)