Skip to content

Commit

Permalink
[server] Fix workspace service's workspace ID validation
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptronicek committed Dec 19, 2024
1 parent b5bdd24 commit 39e1ec8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
14 changes: 12 additions & 2 deletions components/gitpod-protocol/src/util/parse-workspace-id.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import * as chai from "chai";
import { suite, test } from "@testdeck/mocha";
import {
isWorkspaceId,
matchesInstanceIdOrLegacyWorkspaceIdExactly,
matchesNewWorkspaceIdExactly,
parseWorkspaceIdFromHostname,
Expand Down Expand Up @@ -81,8 +82,17 @@ export class ParseWorkspaceIdTest {
const actual = matchesNewWorkspaceIdExactly("moccasin-ferret-15599b3");
expect(actual).to.be.false;
}
@test public matchesWorkspaceIdExactly_new_negative_empty() {
const actual = matchesNewWorkspaceIdExactly(undefined);

@test public isWorkspaceId_positive_new() {
const actual = isWorkspaceId("moccasin-ferret-155799b3");
expect(actual).to.be.true;
}
@test public isWorkspaceId_positive_legacy() {
const actual = isWorkspaceId("b7e0eaf8-ec73-44ec-81ea-04859263b656");
expect(actual).to.be.true;
}
@test public isWorkspaceId_negative_empty() {
const actual = isWorkspaceId(undefined);
expect(actual).to.be.false;
}
}
Expand Down
11 changes: 9 additions & 2 deletions components/gitpod-protocol/src/util/parse-workspace-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,17 @@ export const matchesInstanceIdOrLegacyWorkspaceIdExactly = function (maybeId: st
* @param maybeWorkspaceId
* @returns
*/
export const matchesNewWorkspaceIdExactly = function (maybeWorkspaceId?: string): boolean {
export const matchesNewWorkspaceIdExactly = function (maybeWorkspaceId: string): boolean {
return REGEX_WORKSPACE_ID_EXACT.test(maybeWorkspaceId);
};

/**
* Matches both new and legacy workspace ids
*/
export const isWorkspaceId = function (maybeWorkspaceId?: string): boolean {
if (!maybeWorkspaceId) {
return false;
}

return REGEX_WORKSPACE_ID_EXACT.test(maybeWorkspaceId);
return matchesNewWorkspaceIdExactly(maybeWorkspaceId) || REGEX_WORKSPACE_ID_LEGACY_EXACT.test(maybeWorkspaceId);
};
2 changes: 1 addition & 1 deletion components/server/src/api/workspace-service-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messag
import { ContextService } from "../workspace/context-service";
import { UserService } from "../user/user-service";
import { ContextParser } from "../workspace/context-parser-service";
import { matchesNewWorkspaceIdExactly as isWorkspaceId } from "@gitpod/gitpod-protocol/lib/util/parse-workspace-id";
import { isWorkspaceId } from "@gitpod/gitpod-protocol/lib/util/parse-workspace-id";
import { SYSTEM_USER, SYSTEM_USER_ID } from "../authorization/authorizer";

@injectable()
Expand Down

0 comments on commit 39e1ec8

Please sign in to comment.