Skip to content

Commit 54236fd

Browse files
carderneTrigger.dev RepoOps
authored andcommitted
feat(webapp,secret-proxy): substitute secrets only into each value's allowed headers
Mono-RevId: bc67e6f15f4cf4ba9aef0b6f1c5336a3152f697b
1 parent 61e17ab commit 54236fd

15 files changed

Lines changed: 479 additions & 145 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
area: webapp
3+
type: feature
4+
---
5+
6+
Secret Proxy only replaces a secret's placeholder in the request headers you allow for that secret, `Authorization` and `x-api-key` by default, so services that echo other request headers back can't return the real value to your task.

‎apps/webapp/app/presenters/v3/EnvironmentVariableKeyPresenter.server.ts‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { effectiveVaultHosts } from "~/services/secretProxy/variables.server";
2-
import { proxyConfigHosts } from "~/services/secretProxy/placeholder.server";
1+
import { effectiveVaultPolicies } from "~/services/secretProxy/variables.server";
2+
import { configuredVaultPolicy } from "~/services/secretProxy/placeholder.server";
33
import { boundedIn } from "@trigger.dev/database";
44
import type { PrismaClient, PrismaReplicaClient } from "~/db.server";
55
import { $replica, prisma } from "~/db.server";
@@ -202,7 +202,7 @@ export class EnvironmentVariableKeyPresenter {
202202
take: branchPageSize,
203203
});
204204

205-
const vaultHosts = await effectiveVaultHosts(
205+
const vaultPolicies = await effectiveVaultPolicies(
206206
this.#replicaClient,
207207
[...environmentValues, ...branchValues].map((value) => ({
208208
...value,
@@ -212,7 +212,9 @@ export class EnvironmentVariableKeyPresenter {
212212
);
213213
// A value in Vault through its parent is as unreadable as one in Vault itself.
214214
const values: ValueRecord[] = [...environmentValues, ...branchValues].map((value) =>
215-
vaultHosts.has(`${variable.id}:${value.environmentId}`) ? { ...value, isSecret: true } : value
215+
vaultPolicies.has(`${variable.id}:${value.environmentId}`)
216+
? { ...value, isSecret: true }
217+
: value
216218
);
217219

218220
const repository = new EnvironmentVariablesRepository(this.#prismaClient, this.#replicaClient);
@@ -249,20 +251,22 @@ export class EnvironmentVariableKeyPresenter {
249251
const lastUpdatedBy = value.lastUpdatedBy as EnvironmentVariableUpdater | null;
250252
const user = lastUpdatedBy?.type === "user" ? usersById.get(lastUpdatedBy.userId) : undefined;
251253

254+
const vault = vaultPolicies.get(`${variable.id}:${value.environmentId}`);
252255
return {
253256
id: variable.id,
254257
key: variable.key,
255258
environment: toKeyEnvironment(environment),
256259
value: plainValue,
257260
...(valueUnavailable ? { valueUnavailable: true } : {}),
258261
isSecret: value.isSecret,
259-
...(vaultHosts.has(`${variable.id}:${value.environmentId}`)
262+
...(vault
260263
? {
261-
vaultHosts: vaultHosts.get(`${variable.id}:${value.environmentId}`)!.hosts,
262-
vaultInherited: vaultHosts.get(`${variable.id}:${value.environmentId}`)!.inherited,
264+
vaultHosts: vault.policy.allowedHosts,
265+
vaultHeaders: vault.policy.allowedHeaders,
266+
vaultInherited: vault.inherited,
263267
}
264268
: {}),
265-
...(value.isProxyOnly ? { vaultConfiguredHosts: proxyConfigHosts(value.proxyConfig) } : {}),
269+
...(value.isProxyOnly ? configuredVaultPolicy(value.proxyConfig) : {}),
266270
version: value.version,
267271
lastUpdatedBy,
268272
updatedByUser: user

‎apps/webapp/app/presenters/v3/EnvironmentVariablesPresenter.server.ts‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import type { RuntimeEnvironmentType } from "@trigger.dev/database";
2727
import { boundedIn, Prisma } from "@trigger.dev/database";
2828
type Result = Awaited<ReturnType<EnvironmentVariablesPresenter["call"]>>;
2929
import { hasAgentVault } from "~/v3/featureFlags";
30-
import { effectiveVaultHosts } from "~/services/secretProxy/variables.server";
31-
import { proxyConfigHosts } from "~/services/secretProxy/placeholder.server";
30+
import { effectiveVaultPolicies } from "~/services/secretProxy/variables.server";
31+
import { configuredVaultPolicy } from "~/services/secretProxy/placeholder.server";
3232

3333
export type EnvironmentVariableWithSetValues = Result["environmentVariables"][number];
3434

@@ -282,15 +282,15 @@ export class EnvironmentVariablesPresenter {
282282

283283
const repository = new EnvironmentVariablesRepository(this.#prismaClient, this.#replicaClient);
284284

285-
const vaultHosts = await effectiveVaultHosts(
285+
const vaultPolicies = await effectiveVaultPolicies(
286286
this.#replicaClient,
287287
environmentVariables.flatMap((variable) =>
288288
variable.values.map((value) => ({ ...value, variableId: variable.id }))
289289
),
290290
listedEnvironments
291291
);
292292
const vaultOf = (variableId: string, environmentId: string) =>
293-
vaultHosts.get(`${variableId}:${environmentId}`);
293+
vaultPolicies.get(`${variableId}:${environmentId}`);
294294

295295
const nonSecretItems: Array<{ environmentId: string; key: string }> = [];
296296
for (const environmentVariable of environmentVariables) {
@@ -368,10 +368,14 @@ export class EnvironmentVariablesPresenter {
368368
value: isSecret || valueUnavailable ? "" : val!,
369369
...(valueUnavailable ? { valueUnavailable: true } : {}),
370370
isSecret,
371-
...(vault ? { vaultHosts: vault.hosts, vaultInherited: vault.inherited } : {}),
372-
...(valueRecord.isProxyOnly
373-
? { vaultConfiguredHosts: proxyConfigHosts(valueRecord.proxyConfig) }
371+
...(vault
372+
? {
373+
vaultHosts: vault.policy.allowedHosts,
374+
vaultHeaders: vault.policy.allowedHeaders,
375+
vaultInherited: vault.inherited,
376+
}
374377
: {}),
378+
...(valueRecord.isProxyOnly ? configuredVaultPolicy(valueRecord.proxyConfig) : {}),
375379
version: valueRecord.version,
376380
lastUpdatedBy,
377381
updatedByUser,

‎apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.environment-variables.new/route.tsx‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ import { findTargetEnvironments } from "~/v3/environmentVariables/targetEnvironm
9494
import { viewAfterCreate } from "~/v3/environmentVariables/viewAfterCreate.server";
9595
import { findUnauthorizedEnvironmentId } from "~/v3/writableEnvironments";
9696
import { pageMeta } from "~/utils/pageTitle";
97-
import { DomainInput } from "~/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.environment-variables/SecretProxy";
98-
import { proxyConfigHosts } from "~/services/secretProxy/placeholder.server";
97+
import {
98+
DomainInput,
99+
HeaderInput,
100+
} from "~/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.environment-variables/SecretProxy";
101+
import { DEFAULT_PROXY_ALLOWED_HEADERS } from "~/services/secretProxy/proxyPolicy";
102+
import { parseProxyPolicyForm, type ProxyPolicy } from "~/services/secretProxy/placeholder.server";
99103

100104
export const meta = pageMeta("New environment variable");
101105

@@ -120,6 +124,7 @@ const schema = z.object({
120124
secretTouched: z.preprocess((i) => i === "true", z.boolean()),
121125
vault: z.preprocess((i) => i === "true", z.boolean()),
122126
domains: z.string().optional(),
127+
headers: z.string().optional(),
123128
environmentIds: z.preprocess(
124129
(i) => {
125130
if (typeof i === "string") return [i];
@@ -217,11 +222,11 @@ export const action = dashboardAction(
217222
return json(submission.reply({ formErrors: ["Project not found"] }));
218223
}
219224

220-
const { vault, domains, secretTouched, isSecret, ...rest } = submission.value;
225+
const { vault, domains, headers, secretTouched, isSecret, ...rest } = submission.value;
221226
// An untouched switch only sets new values: replaced values keep their kind.
222227
const createOptions =
223228
secretTouched || vault ? { ...rest, isSecret } : { ...rest, newValueIsSecret: isSecret };
224-
let vaultHosts: string[] | undefined;
229+
let vaultPolicy: ProxyPolicy | undefined;
225230
if (vault) {
226231
if (!project.secretProxyEnabled) {
227232
return json(
@@ -240,18 +245,11 @@ export const action = dashboardAction(
240245
submission.reply({ formErrors: ["Add one variable at a time to Secret Proxy."] })
241246
);
242247
}
243-
vaultHosts = proxyConfigHosts({
244-
allowedHosts: (domains ?? "").toLowerCase().split(",").filter(Boolean),
245-
});
246-
if (!vaultHosts.length) {
247-
return json(
248-
submission.reply({
249-
fieldErrors: {
250-
domains: ["Enter exact domain names without protocols, paths, or wildcards."],
251-
},
252-
})
253-
);
248+
const parsed = parseProxyPolicyForm(domains ?? "", headers ?? "");
249+
if (!parsed.policy) {
250+
return json(submission.reply({ fieldErrors: { [parsed.error]: [parsed.message] } }));
254251
}
252+
vaultPolicy = parsed.policy;
255253
}
256254

257255
// The submitted `environmentIds` are user-supplied. Shared env types are
@@ -283,7 +281,7 @@ export const action = dashboardAction(
283281
const repository = new EnvironmentVariablesRepository(prisma);
284282
const result = await repository.create(project.id, {
285283
...createOptions,
286-
...(vaultHosts && { isSecret: true, vaultHosts }),
284+
...(vaultPolicy && { isSecret: true, vaultPolicy }),
287285
lastUpdatedBy: {
288286
type: "user",
289287
userId,
@@ -364,6 +362,7 @@ export default function Page() {
364362
} = parentData;
365363
const [vault, setVault] = useState(false);
366364
const [domains, setDomains] = useState<string[]>([]);
365+
const [headers, setHeaders] = useState<string[]>(DEFAULT_PROXY_ALLOWED_HEADERS);
367366
// Creating a variable is a write, so gate the targets on write access.
368367
const writableEnvironmentIdSet = new Set(writableEnvironmentIds);
369368
const lastSubmission = useActionData();
@@ -471,7 +470,7 @@ export default function Page() {
471470
variables: [{ key: initialKey, value: "" }],
472471
},
473472
});
474-
const { environmentIds, variables, domains: domainsField } = fields;
473+
const { environmentIds, variables, domains: domainsField, headers: headersField } = fields;
475474
// Every row counts, blank ones too: the server takes one variable at a time into Vault.
476475
const hasSeveralRows = variables.getFieldList().length > 1;
477476

@@ -757,6 +756,8 @@ export default function Page() {
757756
<InputGroup fullWidth className="pt-3.5">
758757
<DomainInput domains={domains} onChange={setDomains} />
759758
<FormError id={domainsField.errorId}>{domainsField.errors}</FormError>
759+
<HeaderInput headers={headers} onChange={setHeaders} />
760+
<FormError id={headersField.errorId}>{headersField.errors}</FormError>
760761
</InputGroup>
761762
}
762763
/>

‎apps/webapp/app/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.environment-variables/EnvironmentVariableControls.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ export function maskEnvironmentVariable(
139139
value: "",
140140
isSecret: false,
141141
vaultHosts: undefined,
142+
vaultHeaders: undefined,
142143
vaultConfiguredHosts: undefined,
144+
vaultConfiguredHeaders: undefined,
143145
vaultInherited: undefined,
144146
permissionDenied: true,
145147
lastUpdatedBy: null,

0 commit comments

Comments
 (0)