Skip to content

Commit e8c70e2

Browse files
committed
[auth] RequestContext: add missing runWithSubjectId
1 parent c183ec8 commit e8c70e2

File tree

2 files changed

+59
-50
lines changed

2 files changed

+59
-50
lines changed

components/server/src/code-sync/code-sync-service.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import { Config } from "../config";
2828
import { CachingBlobServiceClientProvider } from "../util/content-service-sugar";
2929
import { Authorizer } from "../authorization/authorizer";
3030
import { FGAFunctionAccessGuard } from "../auth/resource-access";
31+
import { runWithSubjectId } from "../util/request-context";
32+
import { SubjectId } from "../auth/subject-id";
3133

3234
// By default: 5 kind of resources * 20 revs * 1Mb = 100Mb max in the content service for user data.
3335
const defaultRevLimit = 20;
@@ -106,31 +108,33 @@ export class CodeSyncService {
106108
}
107109

108110
const userId = req.user.id;
109-
try {
110-
await UserRateLimiter.instance(this.config.rateLimiter).consume(userId, accessCodeSyncStorage);
111-
} catch (e) {
112-
if (e instanceof Error) {
113-
throw e;
111+
await runWithSubjectId(SubjectId.fromUserId(userId), async () => {
112+
try {
113+
await UserRateLimiter.instance(this.config.rateLimiter).consume(userId, accessCodeSyncStorage);
114+
} catch (e) {
115+
if (e instanceof Error) {
116+
throw e;
117+
}
118+
res.setHeader("Retry-After", String(Math.round(e.msBeforeNext / 1000)) || 1);
119+
res.status(429).send("Too Many Requests");
120+
return;
121+
}
122+
123+
const functionGuard = (req.user as WithFunctionAccessGuard).functionGuard;
124+
if (!!functionGuard) {
125+
// If we authorize with scopes, there is a FunctionGuard
126+
const guard = new FGAFunctionAccessGuard(userId, functionGuard);
127+
if (!(await guard.canAccess(accessCodeSyncStorage))) {
128+
res.sendStatus(403);
129+
return;
130+
}
114131
}
115-
res.setHeader("Retry-After", String(Math.round(e.msBeforeNext / 1000)) || 1);
116-
res.status(429).send("Too Many Requests");
117-
return;
118-
}
119132

120-
const functionGuard = (req.user as WithFunctionAccessGuard).functionGuard;
121-
if (!!functionGuard) {
122-
// If we authorize with scopes, there is a FunctionGuard
123-
const guard = new FGAFunctionAccessGuard(userId, functionGuard);
124-
if (!(await guard.canAccess(accessCodeSyncStorage))) {
133+
if (!(await this.authorizer.hasPermissionOnUser(userId, "code_sync", userId))) {
125134
res.sendStatus(403);
126135
return;
127136
}
128-
}
129-
130-
if (!(await this.authorizer.hasPermissionOnUser(userId, "code_sync", userId))) {
131-
res.sendStatus(403);
132-
return;
133-
}
137+
});
134138

135139
return next();
136140
});

components/server/src/user/user-controller.ts

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -401,39 +401,44 @@ export class UserController {
401401
return;
402402
}
403403
const sessionId = req.body.sessionId;
404-
const resourceGuard = new FGAResourceAccessGuard(user.id, new OwnerResourceGuard(user.id));
405-
const server = this.createGitpodServer(user, resourceGuard);
406-
try {
407-
await server.sendHeartBeat({}, { wasClosed: true, instanceId: instanceID });
408-
/** no await */ server
409-
.trackEvent(
410-
{},
411-
{
412-
event: "ide_close_signal",
413-
properties: {
414-
sessionId,
415-
instanceId: instanceID,
416-
clientKind: "supervisor-frontend",
404+
405+
await runWithSubjectId(SubjectId.fromUserId(user.id), async () => {
406+
const resourceGuard = new FGAResourceAccessGuard(user.id, new OwnerResourceGuard(user.id));
407+
const server = this.createGitpodServer(user, resourceGuard);
408+
try {
409+
await server.sendHeartBeat({}, { wasClosed: true, instanceId: instanceID });
410+
/** no await */ server
411+
.trackEvent(
412+
{},
413+
{
414+
event: "ide_close_signal",
415+
properties: {
416+
sessionId,
417+
instanceId: instanceID,
418+
clientKind: "supervisor-frontend",
419+
},
417420
},
418-
},
419-
)
420-
.catch((err) => log.warn(logCtx, "workspacePageClose: failed to track ide close signal", err));
421-
res.sendStatus(200);
422-
} catch (e) {
423-
if (ApplicationError.hasErrorCode(e)) {
424-
res.status(e.code).send(e.message);
425-
log.warn(
426-
logCtx,
427-
`workspacePageClose: server sendHeartBeat respond with code: ${e.code}, message: ${e.message}`,
428-
);
421+
)
422+
.catch((err) =>
423+
log.warn(logCtx, "workspacePageClose: failed to track ide close signal", err),
424+
);
425+
res.sendStatus(200);
426+
} catch (e) {
427+
if (ApplicationError.hasErrorCode(e)) {
428+
res.status(e.code).send(e.message);
429+
log.warn(
430+
logCtx,
431+
`workspacePageClose: server sendHeartBeat respond with code: ${e.code}, message: ${e.message}`,
432+
);
433+
return;
434+
}
435+
log.error(logCtx, "workspacePageClose failed", e);
436+
res.sendStatus(500);
429437
return;
438+
} finally {
439+
server.dispose();
430440
}
431-
log.error(logCtx, "workspacePageClose failed", e);
432-
res.sendStatus(500);
433-
return;
434-
} finally {
435-
server.dispose();
436-
}
441+
});
437442
},
438443
);
439444
if (this.config.enableLocalApp) {

0 commit comments

Comments
 (0)