From 39e1ec8ea88222232b0461c3d6bfd5de56313cb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Tron=C3=AD=C4=8Dek?= Date: Thu, 19 Dec 2024 14:06:00 +0000 Subject: [PATCH] [server] Fix workspace service's workspace ID validation --- .../src/util/parse-workspace-id.spec.ts | 14 ++++++++++++-- .../gitpod-protocol/src/util/parse-workspace-id.ts | 11 +++++++++-- components/server/src/api/workspace-service-api.ts | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/components/gitpod-protocol/src/util/parse-workspace-id.spec.ts b/components/gitpod-protocol/src/util/parse-workspace-id.spec.ts index bcccc540d963a9..a2cd748d6acd1f 100644 --- a/components/gitpod-protocol/src/util/parse-workspace-id.spec.ts +++ b/components/gitpod-protocol/src/util/parse-workspace-id.spec.ts @@ -7,6 +7,7 @@ import * as chai from "chai"; import { suite, test } from "@testdeck/mocha"; import { + isWorkspaceId, matchesInstanceIdOrLegacyWorkspaceIdExactly, matchesNewWorkspaceIdExactly, parseWorkspaceIdFromHostname, @@ -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; } } diff --git a/components/gitpod-protocol/src/util/parse-workspace-id.ts b/components/gitpod-protocol/src/util/parse-workspace-id.ts index 91617d96717734..21f38c6fd79e3e 100644 --- a/components/gitpod-protocol/src/util/parse-workspace-id.ts +++ b/components/gitpod-protocol/src/util/parse-workspace-id.ts @@ -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); }; diff --git a/components/server/src/api/workspace-service-api.ts b/components/server/src/api/workspace-service-api.ts index 0066f65822a090..f2cf300faf739b 100644 --- a/components/server/src/api/workspace-service-api.ts +++ b/components/server/src/api/workspace-service-api.ts @@ -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()