Skip to content

Commit d53fcce

Browse files
committed
improve backwards compat
1 parent 8bbd4c8 commit d53fcce

4 files changed

Lines changed: 24 additions & 56 deletions

File tree

apps/webapp/app/services/apiAuth.server.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ export async function authenticatedEnvironmentForAuthentication(
484484
);
485485
}
486486

487-
if (auth.result.environment.slug !== slug) {
487+
if (auth.result.environment.slug !== slug && auth.result.environment.branchName !== resolvedBranch) {
488488
throw json(
489489
{
490490
error:
@@ -494,16 +494,6 @@ export async function authenticatedEnvironmentForAuthentication(
494494
);
495495
}
496496

497-
if (auth.result.environment.branchName !== resolvedBranch) {
498-
throw json(
499-
{
500-
error:
501-
"Invalid environment branch for this API key. Make sure you are using an API key associated with that environment.",
502-
},
503-
{ status: 400 }
504-
);
505-
}
506-
507497
return auth.result.environment;
508498
}
509499
case "personalAccessToken": {

apps/webapp/app/services/upsertBranch.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class UpsertBranchService {
9393
// its parent's orgMemberId. Preview parents have no orgMember (orgMemberId is null).
9494
if (!parentEnvironment) {
9595
// This should never happen
96-
if (env === "preview") {
96+
if (env === "development") {
9797
return {
9898
success: false as const,
9999
error: "Error: No default dev runtime environment setup.",

apps/webapp/test/rbacFallbackBranch.test.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,20 +140,6 @@ describe("RBAC fallback — DEVELOPMENT branch pivot", () => {
140140
});
141141

142142
describe("RBAC fallback — branch header guards", () => {
143-
postgresTest("a non-branchable env rejects a branch header", async ({ prisma }) => {
144-
const { organization, project } = await createTestOrgProjectWithMember(prisma);
145-
const rbac = makeController(prisma);
146-
147-
const prod = await createEnv(prisma, project.id, organization.id, { type: "PRODUCTION" });
148-
149-
const result = await rbac.authenticateBearer(bearerRequest(prod.apiKey, "some-branch"));
150-
151-
expect(result.ok).toBe(false);
152-
if (result.ok) return;
153-
expect(result.status).toBe(401);
154-
expect(result.error).toContain("preview and dev");
155-
});
156-
157143
// The "default" sentinel is DEVELOPMENT-only: it maps the dev root env to its
158144
// (branchless) self. For PREVIEW, "default" is an ordinary branch name, so a
159145
// PREVIEW branch literally named "default" is reachable and the request pivots

internal-packages/rbac/src/fallback.ts

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -192,45 +192,37 @@ class RoleBaseAccessFallbackController implements RoleBaseAccessController {
192192
return { ok: false, status: 401, error: "Invalid API key" };
193193
}
194194

195-
// PREVIEW env requires a branch header; pivot to the child env so
196-
// downstream code operates on the branch (its own id, but the
197-
// parent's apiKey/orgMember/organization/project — exactly what
198-
// findEnvironmentByApiKey does for the legacy auth path).
199195
if (env.type === "PREVIEW" && !branchName) {
200196
return {
201197
ok: false,
202198
status: 401,
203199
error: "x-trigger-branch header required for preview env",
204200
};
205201
}
206-
// The "default" sentinel is DEVELOPMENT-only: it maps to the dev root env
207-
// (which carries no branch), so we skip the pivot there. For PREVIEW,
208-
// "default" is an ordinary branch name and must still pivot to its child.
209-
const isDevRootSentinel = env.type === "DEVELOPMENT" && isDefaultDevBranch(branchName);
210-
if (branchName !== null && !isDevRootSentinel) {
211-
if (env.type !== "PREVIEW" && env.type !== "DEVELOPMENT") {
212-
return {
213-
ok: false,
214-
status: 401,
215-
error: "x-trigger-branch header can only be used with preview and dev envs",
202+
203+
if (env.type === "PREVIEW" || env.type === "DEVELOPMENT") {
204+
// The "default" sentinel is DEVELOPMENT-only: it maps to the dev root env
205+
// (which carries no branch), so we skip the pivot there. For PREVIEW,
206+
// "default" is an ordinary branch name and must still pivot to its child.
207+
const isDevRootSentinel = env.type === "DEVELOPMENT" && isDefaultDevBranch(branchName);
208+
if (branchName !== null && !isDevRootSentinel) {
209+
const child = env.childEnvironments?.[0];
210+
if (!child) {
211+
return { ok: false, status: 401, error: "No matching branch env" };
212+
}
213+
// Pivot to the child env: child's id/type/branchName, parent's
214+
// apiKey/orgMember/organization/project. parentEnvironment is set
215+
// explicitly here so the slim shape stays internally consistent.
216+
env = {
217+
...child,
218+
apiKey: env.apiKey,
219+
orgMember: env.orgMember,
220+
organization: env.organization,
221+
project: env.project,
222+
parentEnvironment: { id: env.id, apiKey: env.apiKey },
223+
childEnvironments: [],
216224
};
217225
}
218-
const child = env.childEnvironments?.[0];
219-
if (!child) {
220-
return { ok: false, status: 401, error: "No matching branch env" };
221-
}
222-
// Pivot to the child env: child's id/type/branchName, parent's
223-
// apiKey/orgMember/organization/project. parentEnvironment is set
224-
// explicitly here so the slim shape stays internally consistent.
225-
env = {
226-
...child,
227-
apiKey: env.apiKey,
228-
orgMember: env.orgMember,
229-
organization: env.organization,
230-
project: env.project,
231-
parentEnvironment: { id: env.id, apiKey: env.apiKey },
232-
childEnvironments: [],
233-
};
234226
}
235227

236228
const subject: RbacSubject = {

0 commit comments

Comments
 (0)