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

[server] Fix workspace service's workspace ID validation #20468

Merged
merged 1 commit into from
Dec 19, 2024
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
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
Loading