Skip to content

Commit 5b1caae

Browse files
authored
feat(security): Add env var to restrict api key creation (#577)
* add env var to restrict api key creation * changelog
1 parent 336b07d commit 5b1caae

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Added support for passing db connection url as seperate `DATABASE_HOST`, `DATABASE_USERNAME`, `DATABASE_PASSWORD`, `DATABASE_NAME`, and `DATABASE_ARGS` env vars. [#545](https://github.com/sourcebot-dev/sourcebot/pull/545)
1515
- Added support for GitHub Apps for service auth. [#570](https://github.com/sourcebot-dev/sourcebot/pull/570)
1616
- Added prometheus metrics for repo index manager. [#571](https://github.com/sourcebot-dev/sourcebot/pull/571)
17+
- Added experimental environment variable to disable API key creation for non-admin users. [#577](https://github.com/sourcebot-dev/sourcebot/pull/577)
1718

1819
### Fixed
1920
- Fixed "dubious ownership" errors when cloning / fetching repos. [#553](https://github.com/sourcebot-dev/sourcebot/pull/553)

packages/web/src/actions.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,16 @@ export const verifyApiKey = async (apiKeyPayload: ApiKeyPayload): Promise<{ apiK
393393

394394
export const createApiKey = async (name: string, domain: string): Promise<{ key: string } | ServiceError> => sew(() =>
395395
withAuth((userId) =>
396-
withOrgMembership(userId, domain, async ({ org }) => {
396+
withOrgMembership(userId, domain, async ({ org, userRole }) => {
397+
if (env.EXPERIMENT_DISABLE_API_KEY_CREATION_FOR_NON_ADMIN_USERS === 'true' && userRole !== OrgRole.OWNER) {
398+
logger.error(`API key creation is disabled for non-admin users. User ${userId} is not an owner.`);
399+
return {
400+
statusCode: StatusCodes.FORBIDDEN,
401+
errorCode: ErrorCode.INSUFFICIENT_PERMISSIONS,
402+
message: "API key creation is disabled for non-admin users.",
403+
} satisfies ServiceError;
404+
}
405+
397406
const existingApiKey = await prisma.apiKey.findFirst({
398407
where: {
399408
createdById: userId,

packages/web/src/env.mjs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const env = createEnv({
1818

1919
// Auth
2020
FORCE_ENABLE_ANONYMOUS_ACCESS: booleanSchema.default('false'),
21-
21+
2222
AUTH_SECRET: z.string(),
2323
AUTH_URL: z.string().url(),
2424
AUTH_CREDENTIALS_LOGIN_ENABLED: booleanSchema.default('true'),
@@ -130,10 +130,12 @@ export const env = createEnv({
130130

131131
SOURCEBOT_DEMO_EXAMPLES_PATH: z.string().optional(),
132132

133+
// Experimental Environment Variables
134+
// @note: These environment variables are subject to change at any time and are not garunteed to be backwards compatible.
135+
EXPERIMENT_DISABLE_API_KEY_CREATION_FOR_NON_ADMIN_USERS: booleanSchema.default('false'),
133136
EXPERIMENT_SELF_SERVE_REPO_INDEXING_ENABLED: booleanSchema.default('false'),
134137
// @NOTE: Take care to update actions.ts when changing the name of this.
135138
EXPERIMENT_SELF_SERVE_REPO_INDEXING_GITHUB_TOKEN: z.string().optional(),
136-
137139
EXPERIMENT_EE_PERMISSION_SYNC_ENABLED: booleanSchema.default('false'),
138140
},
139141
// @NOTE: Please make sure of the following:

0 commit comments

Comments
 (0)