Skip to content

Commit 6ae543a

Browse files
carderneTrigger.dev RepoOps
authored andcommitted
fix(webapp): preserve API key restrictions when reading env vars
Mono-RevId: c223e3c56d33e50c024789514cf218f46eded002
1 parent 45740b6 commit 6ae543a

4 files changed

Lines changed: 43 additions & 6 deletions

File tree

apps/webapp/app/routes/api.v1.projects.$projectRef.envvars.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { z } from "zod";
33
import { prisma } from "~/db.server";
44
import { env } from "~/env.server";
55
import { authenticateApiKeyWithScope } from "~/services/apiAuth.server";
6+
import { environmentVariablesForApiKeyResponse } from "~/v3/environmentVariables/environmentVariablesForApiKeyResponse.server";
67
import { resolveVariablesForEnvironment } from "~/v3/environmentVariables/environmentVariablesRepository.server";
78

89
const ParamsSchema = z.object({
@@ -65,9 +66,6 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
6566
);
6667

6768
return json({
68-
variables: variables.reduce((acc: Record<string, string>, variable) => {
69-
acc[variable.key] = variable.value;
70-
return acc;
71-
}, {}),
69+
variables: environmentVariablesForApiKeyResponse(variables, authenticationResult.apiKey),
7270
});
7371
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
type EnvironmentVariable = {
2+
key: string;
3+
value: string;
4+
};
5+
6+
/**
7+
* Preserve the resolved runtime variables while ensuring an API-key caller
8+
* only receives the credential it already presented.
9+
*/
10+
export function environmentVariablesForApiKeyResponse(
11+
variables: EnvironmentVariable[],
12+
presentedApiKey: string
13+
): Record<string, string> {
14+
return variables.reduce<Record<string, string>>((acc, variable) => {
15+
acc[variable.key] = variable.key === "TRIGGER_SECRET_KEY" ? presentedApiKey : variable.value;
16+
return acc;
17+
}, {});
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, it } from "vitest";
2+
import { environmentVariablesForApiKeyResponse } from "./environmentVariablesForApiKeyResponse.server";
3+
4+
describe("environmentVariablesForApiKeyResponse", () => {
5+
it("replaces the resolved root key with the presented API key", () => {
6+
expect(
7+
environmentVariablesForApiKeyResponse(
8+
[
9+
{ key: "USER_VARIABLE", value: "value" },
10+
{ key: "TRIGGER_SECRET_KEY", value: "tr_prod_root" },
11+
{ key: "TRIGGER_API_URL", value: "https://example.com" },
12+
],
13+
"tr_prod_sk_presented"
14+
)
15+
).toEqual({
16+
USER_VARIABLE: "value",
17+
TRIGGER_SECRET_KEY: "tr_prod_sk_presented",
18+
TRIGGER_API_URL: "https://example.com",
19+
});
20+
});
21+
});

apps/webapp/vitest.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import tsconfigPaths from "vite-tsconfig-paths";
55
export default defineConfig({
66
test: {
77
sequence: { sequencer: DurationShardingSequencer },
8-
// Webapp tests live under test/**; the run-ops migration family
9-
// colocates its *.server.test.ts next to source under app/v3/runOpsMigration/.
8+
// Webapp tests live under test/**; selected test families colocate next to source.
109
// The run-store seam test colocates next to its source at app/v3/runStore.server.test.ts.
1110
// Pure unit tests for runEngine concerns colocate next to their source file.
1211
include: [
1312
"test/**/*.test.ts",
13+
"app/v3/environmentVariables/**/*.test.ts",
1414
"app/v3/runOpsMigration/**/*.test.ts",
1515
"app/v3/runStore.server.test.ts",
1616
"app/v3/validateMinimumCronInterval.test.ts",

0 commit comments

Comments
 (0)