-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[codex] Revoke credentials when role permissions change #2262
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
Merged
benjaminshafii
merged 1 commit into
dev
from
codex/security-review-delegated-admin-hardening
Jun 14, 2026
+178
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
ee/apps/den-api/src/organization-role-credential-revocation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { and, eq, isNull } from "@openwork-ee/den-db/drizzle" | ||
| import { MemberTable } from "@openwork-ee/den-db/schema" | ||
| import { revokeOrganizationApiKeysForMember } from "./api-keys.js" | ||
| import { revokeMembershipSessionCredentials } from "./credential-revocation.js" | ||
| import { db } from "./db.js" | ||
|
|
||
| type OrganizationId = typeof MemberTable.$inferSelect.organizationId | ||
|
|
||
| export type OrganizationRoleCredentialRevocationCounts = { | ||
| members: number | ||
| apiKeys: number | ||
| sessions: number | ||
| oauthAccessTokens: number | ||
| oauthRefreshTokens: number | ||
| } | ||
|
|
||
| function splitRoleValue(value: string) { | ||
| return value | ||
| .split(",") | ||
| .map((entry) => entry.trim()) | ||
| .filter(Boolean) | ||
| } | ||
|
|
||
| export async function revokeCredentialsForOrganizationRoleMembers(input: { | ||
| organizationId: OrganizationId | ||
| role: string | ||
| }): Promise<OrganizationRoleCredentialRevocationCounts> { | ||
| const members = await db | ||
| .select({ | ||
| id: MemberTable.id, | ||
| role: MemberTable.role, | ||
| userId: MemberTable.userId, | ||
| }) | ||
| .from(MemberTable) | ||
| .where(and(eq(MemberTable.organizationId, input.organizationId), isNull(MemberTable.removedAt))) | ||
|
|
||
| const counts: OrganizationRoleCredentialRevocationCounts = { | ||
| members: 0, | ||
| apiKeys: 0, | ||
| sessions: 0, | ||
| oauthAccessTokens: 0, | ||
| oauthRefreshTokens: 0, | ||
| } | ||
|
|
||
| for (const member of members) { | ||
| if (!splitRoleValue(member.role).includes(input.role)) { | ||
| continue | ||
| } | ||
|
|
||
| counts.members += 1 | ||
| counts.apiKeys += await revokeOrganizationApiKeysForMember({ | ||
| organizationId: input.organizationId, | ||
| orgMembershipId: member.id, | ||
| userId: member.userId, | ||
| }) | ||
|
|
||
| const credentials = await revokeMembershipSessionCredentials({ | ||
| organizationId: input.organizationId, | ||
| userId: member.userId, | ||
| }) | ||
| counts.sessions += credentials.sessions | ||
| counts.oauthAccessTokens += credentials.oauthAccessTokens | ||
| counts.oauthRefreshTokens += credentials.oauthRefreshTokens | ||
| } | ||
|
|
||
| return counts | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
ee/apps/den-api/test/organization-role-credential-revocation.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import { beforeAll, expect, mock, test } from "bun:test" | ||
| import { MemberTable } from "@openwork-ee/den-db/schema" | ||
|
|
||
| const members = [ | ||
| { id: "member_one", role: "member,security-admin", userId: "user_one" }, | ||
| { id: "member_two", role: "security-admin", userId: null }, | ||
| { id: "member_three", role: "admin", userId: "user_three" }, | ||
| ] | ||
|
|
||
| const apiKeyRevocations: unknown[] = [] | ||
| const credentialRevocations: unknown[] = [] | ||
| let selectCalls = 0 | ||
|
|
||
| function resetCalls() { | ||
| apiKeyRevocations.length = 0 | ||
| credentialRevocations.length = 0 | ||
| selectCalls = 0 | ||
| } | ||
|
|
||
| let roleCredentialRevocationModule: typeof import("../src/organization-role-credential-revocation.js") | ||
|
|
||
| beforeAll(async () => { | ||
| mock.module("../src/db.js", () => ({ | ||
| db: { | ||
| select: () => ({ | ||
| from: (table: unknown) => ({ | ||
| where: () => { | ||
| selectCalls += 1 | ||
| return Promise.resolve(table === MemberTable ? members : []) | ||
| }, | ||
| }), | ||
| }), | ||
| }, | ||
| })) | ||
|
|
||
| mock.module("../src/api-keys.js", () => ({ | ||
| revokeOrganizationApiKeysForMember: (input: unknown) => { | ||
| apiKeyRevocations.push(input) | ||
| return Promise.resolve(1) | ||
| }, | ||
| })) | ||
|
|
||
| mock.module("../src/credential-revocation.js", () => ({ | ||
| revokeMembershipSessionCredentials: (input: unknown) => { | ||
| credentialRevocations.push(input) | ||
| return Promise.resolve({ | ||
| sessions: input && typeof input === "object" && "userId" in input && input.userId ? 2 : 0, | ||
| oauthAccessTokens: input && typeof input === "object" && "userId" in input && input.userId ? 1 : 0, | ||
| oauthRefreshTokens: input && typeof input === "object" && "userId" in input && input.userId ? 1 : 0, | ||
| }) | ||
| }, | ||
| })) | ||
|
|
||
| roleCredentialRevocationModule = await import("../src/organization-role-credential-revocation.js") | ||
| }) | ||
|
|
||
| test("role credential revocation touches active members using the changed role", async () => { | ||
| resetCalls() | ||
|
|
||
| const counts = await roleCredentialRevocationModule.revokeCredentialsForOrganizationRoleMembers({ | ||
| organizationId: "org_123", | ||
| role: "security-admin", | ||
| }) | ||
|
|
||
| expect(counts).toEqual({ | ||
| members: 2, | ||
| apiKeys: 2, | ||
| sessions: 2, | ||
| oauthAccessTokens: 1, | ||
| oauthRefreshTokens: 1, | ||
| }) | ||
| expect(selectCalls).toBe(1) | ||
| expect(apiKeyRevocations).toEqual([ | ||
| { organizationId: "org_123", orgMembershipId: "member_one", userId: "user_one" }, | ||
| { organizationId: "org_123", orgMembershipId: "member_two", userId: null }, | ||
| ]) | ||
| expect(credentialRevocations).toEqual([ | ||
| { organizationId: "org_123", userId: "user_one" }, | ||
| { organizationId: "org_123", userId: null }, | ||
| ]) | ||
| }) | ||
|
|
||
| test("role credential revocation skips members without the changed role", async () => { | ||
| resetCalls() | ||
|
|
||
| const counts = await roleCredentialRevocationModule.revokeCredentialsForOrganizationRoleMembers({ | ||
| organizationId: "org_123", | ||
| role: "billing-admin", | ||
| }) | ||
|
|
||
| expect(counts).toEqual({ | ||
| members: 0, | ||
| apiKeys: 0, | ||
| sessions: 0, | ||
| oauthAccessTokens: 0, | ||
| oauthRefreshTokens: 0, | ||
| }) | ||
| expect(selectCalls).toBe(1) | ||
| expect(apiKeyRevocations).toEqual([]) | ||
| expect(credentialRevocations).toEqual([]) | ||
| }) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P1: Credential revocation is non-atomic with the role permission update, so revocation failures can leave stale credentials after a successful permission change. This is a fail-open security gap in the new hardening path.
Prompt for AI agents