@@ -58,6 +58,15 @@ import { ApplicationError, ErrorCodes } from "@gitpod/gitpod-protocol/lib/messag
58
58
import { ContextService } from "../workspace/context-service" ;
59
59
import { UserService } from "../user/user-service" ;
60
60
import { ContextParser } from "../workspace/context-parser-service" ;
61
+ import { workspaceIDRegex } from "@gitpod/gitpod-protocol/lib/util/gitpod-host-url" ;
62
+
63
+ const isWorkspaceId = ( workspaceId ?: string ) => {
64
+ if ( ! workspaceId ) {
65
+ return false ;
66
+ }
67
+
68
+ return workspaceIDRegex . test ( workspaceId ) ;
69
+ } ;
61
70
62
71
@injectable ( )
63
72
export class WorkspaceServiceAPI implements ServiceImpl < typeof WorkspaceServiceInterface > {
@@ -68,8 +77,8 @@ export class WorkspaceServiceAPI implements ServiceImpl<typeof WorkspaceServiceI
68
77
@inject ( ContextParser ) private contextParser : ContextParser ;
69
78
70
79
async getWorkspace ( req : GetWorkspaceRequest , _ : HandlerContext ) : Promise < GetWorkspaceResponse > {
71
- if ( ! req . workspaceId ) {
72
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "workspaceId is required" ) ;
80
+ if ( ! isWorkspaceId ( req . workspaceId ) ) {
81
+ throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "a valid workspaceId is required" ) ;
73
82
}
74
83
const info = await this . workspaceService . getWorkspace ( ctxUserId ( ) , req . workspaceId ) ;
75
84
const response = new GetWorkspaceResponse ( ) ;
@@ -198,8 +207,8 @@ export class WorkspaceServiceAPI implements ServiceImpl<typeof WorkspaceServiceI
198
207
199
208
async startWorkspace ( req : StartWorkspaceRequest ) : Promise < StartWorkspaceResponse > {
200
209
// We rely on FGA to do the permission checking
201
- if ( ! req . workspaceId ) {
202
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "workspaceId is required" ) ;
210
+ if ( ! isWorkspaceId ( req . workspaceId ) ) {
211
+ throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "a valid workspaceId is required" ) ;
203
212
}
204
213
const user = await this . userService . findUserById ( ctxUserId ( ) , ctxUserId ( ) ) ;
205
214
const { workspace, latestInstance : instance } = await this . workspaceService . getWorkspace (
@@ -227,8 +236,8 @@ export class WorkspaceServiceAPI implements ServiceImpl<typeof WorkspaceServiceI
227
236
req : GetWorkspaceDefaultImageRequest ,
228
237
_ : HandlerContext ,
229
238
) : Promise < GetWorkspaceDefaultImageResponse > {
230
- if ( ! req . workspaceId ) {
231
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "workspaceId is required" ) ;
239
+ if ( ! isWorkspaceId ( req . workspaceId ) ) {
240
+ throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "a valid workspaceId is required" ) ;
232
241
}
233
242
const result = await this . workspaceService . getWorkspaceDefaultImage ( ctxUserId ( ) , req . workspaceId ) ;
234
243
const response = new GetWorkspaceDefaultImageResponse ( {
@@ -246,8 +255,8 @@ export class WorkspaceServiceAPI implements ServiceImpl<typeof WorkspaceServiceI
246
255
}
247
256
248
257
async sendHeartBeat ( req : SendHeartBeatRequest , _ : HandlerContext ) : Promise < SendHeartBeatResponse > {
249
- if ( ! req . workspaceId ) {
250
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "workspaceId is required" ) ;
258
+ if ( ! isWorkspaceId ( req . workspaceId ) ) {
259
+ throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "a valid workspaceId is required" ) ;
251
260
}
252
261
const info = await this . workspaceService . getWorkspace ( ctxUserId ( ) , req . workspaceId ) ;
253
262
if ( ! info . latestInstance ?. id || info . latestInstance . status . phase !== "running" ) {
@@ -265,8 +274,8 @@ export class WorkspaceServiceAPI implements ServiceImpl<typeof WorkspaceServiceI
265
274
req : GetWorkspaceOwnerTokenRequest ,
266
275
_ : HandlerContext ,
267
276
) : Promise < GetWorkspaceOwnerTokenResponse > {
268
- if ( ! req . workspaceId ) {
269
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "workspaceId is required" ) ;
277
+ if ( ! isWorkspaceId ( req . workspaceId ) ) {
278
+ throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "a valid workspaceId is required" ) ;
270
279
}
271
280
const ownerToken = await this . workspaceService . getOwnerToken ( ctxUserId ( ) , req . workspaceId ) ;
272
281
const response = new GetWorkspaceOwnerTokenResponse ( ) ;
@@ -278,8 +287,8 @@ export class WorkspaceServiceAPI implements ServiceImpl<typeof WorkspaceServiceI
278
287
req : GetWorkspaceEditorCredentialsRequest ,
279
288
_ : HandlerContext ,
280
289
) : Promise < GetWorkspaceEditorCredentialsResponse > {
281
- if ( ! req . workspaceId ) {
282
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "workspaceId is required" ) ;
290
+ if ( ! isWorkspaceId ( req . workspaceId ) ) {
291
+ throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "a valid workspaceId is required" ) ;
283
292
}
284
293
const credentials = await this . workspaceService . getIDECredentials ( ctxUserId ( ) , req . workspaceId ) ;
285
294
const response = new GetWorkspaceEditorCredentialsResponse ( ) ;
@@ -288,8 +297,8 @@ export class WorkspaceServiceAPI implements ServiceImpl<typeof WorkspaceServiceI
288
297
}
289
298
290
299
async updateWorkspace ( req : UpdateWorkspaceRequest ) : Promise < UpdateWorkspaceResponse > {
291
- if ( ! req . workspaceId ) {
292
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "workspaceId is required" ) ;
300
+ if ( ! isWorkspaceId ( req . workspaceId ) ) {
301
+ throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "a valid workspaceId is required" ) ;
293
302
}
294
303
if ( req . spec ?. timeout ?. inactivity ?. seconds || ( req . spec ?. sshPublicKeys && req . spec ?. sshPublicKeys . length > 0 ) ) {
295
304
throw new ApplicationError ( ErrorCodes . UNIMPLEMENTED , "not implemented" ) ;
@@ -363,17 +372,17 @@ export class WorkspaceServiceAPI implements ServiceImpl<typeof WorkspaceServiceI
363
372
}
364
373
365
374
async stopWorkspace ( req : StopWorkspaceRequest ) : Promise < StopWorkspaceResponse > {
366
- if ( ! req . workspaceId ) {
367
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "workspaceId is required" ) ;
375
+ if ( ! isWorkspaceId ( req . workspaceId ) ) {
376
+ throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "a valid workspaceId is required" ) ;
368
377
}
369
378
await this . workspaceService . stopWorkspace ( ctxUserId ( ) , req . workspaceId , "stopped via API" ) ;
370
379
const response = new StopWorkspaceResponse ( ) ;
371
380
return response ;
372
381
}
373
382
374
383
async deleteWorkspace ( req : DeleteWorkspaceRequest ) : Promise < DeleteWorkspaceResponse > {
375
- if ( ! req . workspaceId ) {
376
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "workspaceId is required" ) ;
384
+ if ( ! isWorkspaceId ( req . workspaceId ) ) {
385
+ throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "a valid workspaceId is required" ) ;
377
386
}
378
387
await this . workspaceService . deleteWorkspace ( ctxUserId ( ) , req . workspaceId , "user" ) ;
379
388
const response = new DeleteWorkspaceResponse ( ) ;
@@ -389,8 +398,8 @@ export class WorkspaceServiceAPI implements ServiceImpl<typeof WorkspaceServiceI
389
398
}
390
399
391
400
async createWorkspaceSnapshot ( req : CreateWorkspaceSnapshotRequest ) : Promise < CreateWorkspaceSnapshotResponse > {
392
- if ( ! req . workspaceId ) {
393
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "workspaceId is required" ) ;
401
+ if ( ! isWorkspaceId ( req . workspaceId ) ) {
402
+ throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "a valid workspaceId is required" ) ;
394
403
}
395
404
const snapshot = await this . workspaceService . takeSnapshot ( ctxUserId ( ) , {
396
405
workspaceId : req . workspaceId ,
@@ -410,8 +419,8 @@ export class WorkspaceServiceAPI implements ServiceImpl<typeof WorkspaceServiceI
410
419
}
411
420
412
421
async updateWorkspacePort ( req : UpdateWorkspacePortRequest ) : Promise < UpdateWorkspacePortResponse > {
413
- if ( ! req . workspaceId ) {
414
- throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "workspaceId is required" ) ;
422
+ if ( ! isWorkspaceId ( req . workspaceId ) ) {
423
+ throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "a valid workspaceId is required" ) ;
415
424
}
416
425
if ( ! req . port ) {
417
426
throw new ApplicationError ( ErrorCodes . BAD_REQUEST , "port is required" ) ;
0 commit comments