Skip to content

Conversation

@filiptronicek
Copy link
Member

Description

If you called WorkspaceService.GetWorkspace with workspaceId: valid-workspace-id?hello, we would let SpiceDB throw an exception, because checkPermission would fail. This PR fixes that from 2 fronts:

  1. We no longer consider SpiceDB returning INVALID_ARGUMENT a system exception and instead throw a ErrorCodes.BAD_REQUEST.
  2. We now validate workspace IDs on every WorkspaceService method that needs it using a Regex that seems to have worked for us for ~ 2 years.

Related Issue(s)

Fixes ENT-842

How to test

Make a request to some API under WorkspaceServer with an invalid workspace ID, you should get back "a valid workspaceId is required"

curl 'https://ft-fix-spi5a78964ae9.preview.gitpod-dev.com/public-api/gitpod.v1.WorkspaceService/GetWorkspace' --compressed -X POST -H 'content-type: application/json' -H 'Origin: https://ft-fix-spi5a78964ae9.preview.gitpod-dev.com' -H 'Cookie: __Host-_ft_fix_spi5a78964ae9_preview_gitpod_dev_com_jwt2_=ey[...]' --data-raw '{"pagination":{"pageSize":50},"organizationId":"3e213667-9617-4020-99bd-e3b5ceba3999", "workspaceId": "gitpoddemos-springpetcl-pi2uhnyqm20?aaa"}'

For requests to other resources or things that this validity check just doesn't catch, the error message will be similar to the following:

{"code":"invalid_argument","message":"Invalid request for permission check: Error: 3 INVALID_ARGUMENT: invalid CheckPermissionRequest.Resource: embedded message failed validation | caused by: invalid ObjectReference.ObjectId: value does not match regex pattern \"^(([a-zA-Z0-9/_|\\\\-=+]{1,})|\\\\*)$\""}                                                             


// this pattern matches v4 UUIDs as well as the new generated workspace ids (e.g. pink-panda-ns35kd21)
const workspaceIDRegex = RegExp(`^(?:debug-)?${baseWorkspaceIDRegex}$`);
export const workspaceIDRegex = RegExp(`^(?:debug-)?${baseWorkspaceIDRegex}$`);
Copy link
Member

Choose a reason for hiding this comment

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

🫧 I wonder where this regex comes from... it hopefully is identical with this one?

@filiptronicek Could you check whether we can easily unify something here? 🤔 The code I cited should be the source of truth.

import { ContextParser } from "../workspace/context-parser-service";
import { workspaceIDRegex } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url";

const isWorkspaceId = (workspaceId?: string) => {
Copy link
Member

Choose a reason for hiding this comment

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

This kind of method should ideally be part of parse-workspace-id.ts.

async updateWorkspacePort(req: UpdateWorkspacePortRequest): Promise<UpdateWorkspacePortResponse> {
if (!req.workspaceId) {
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "workspaceId is required");
if (!isWorkspaceId(req.workspaceId)) {
Copy link
Member

Choose a reason for hiding this comment

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

🧡

const permitted = response.permissionship === v1.CheckPermissionResponse_Permissionship.HAS_PERMISSION;
return { permitted, checkedAt: response.checkedAt?.token };
} catch (err) {
if (isGrpcError(err) && err.code === grpc.status.INVALID_ARGUMENT) {
Copy link
Member

Choose a reason for hiding this comment

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

Please add a comment on the "why"/context 🙏

Copy link
Member

@geropl geropl left a comment

Choose a reason for hiding this comment

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

Code generally looks great, please have a look at the comments! 🙏

@roboquat roboquat merged commit 3f0fc73 into main Oct 7, 2024
33 checks passed
@roboquat roboquat deleted the ft/fix-spicedb-invalid-arg-exeptions branch October 7, 2024 07:55
filiptronicek added a commit that referenced this pull request Oct 7, 2024
roboquat pushed a commit that referenced this pull request Oct 7, 2024
* Follow-ups from #20269

* `matchesNewWorkspaceIdExactly` to handle `undefined`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants