@@ -26,6 +26,8 @@ import { env } from "~/env.server";
2626import { auditLogs } from "~/services/auditLogs/auditLogs.server" ;
2727import { logger } from "~/services/logger.server" ;
2828import {
29+ checkProxyPolicy ,
30+ DEFAULT_PROXY_ALLOWED_HEADERS ,
2931 intersectProxyPolicy ,
3032 parseProxyConfig ,
3133 type ProxyPolicy ,
@@ -198,6 +200,61 @@ type WrittenValue = {
198200 */
199201const POLICY_UPDATE_BATCH_SIZE = 1000 ;
200202
203+ /**
204+ * The policy `saveVaultValue` stores: the submitted fields over the value's current policy. Call
205+ * with the variable's lock held, so a concurrent save of another field isn't undone. `covered`
206+ * when the value was already in Vault, itself or through its parent.
207+ */
208+ async function vaultPolicyToSave (
209+ tx : PrismaClientOrTransaction ,
210+ variableId : string ,
211+ environmentId : string ,
212+ submitted : Partial < ProxyPolicy >
213+ ) : Promise < { policy : ProxyPolicy | "inherited" ; covered : boolean } > {
214+ const environment = await tx . runtimeEnvironment . findFirstOrThrow ( {
215+ where : { id : environmentId } ,
216+ select : { parentEnvironmentId : true } ,
217+ } ) ;
218+ const values = await tx . environmentVariableValue . findMany ( {
219+ where : {
220+ variableId,
221+ isProxyOnly : true ,
222+ environmentId : {
223+ in : boundedIn (
224+ environment . parentEnvironmentId
225+ ? [ environmentId , environment . parentEnvironmentId ]
226+ : [ environmentId ]
227+ ) ,
228+ } ,
229+ } ,
230+ select : { environmentId : true , proxyConfig : true } ,
231+ } ) ;
232+ const configOf = ( id : string | null ) =>
233+ values . find ( ( value ) => value . environmentId === id ) ?. proxyConfig ;
234+ const own = configOf ( environmentId ) ;
235+ const inherited = configOf ( environment . parentEnvironmentId ) ;
236+ const covered = own !== undefined || inherited !== undefined ;
237+ // A branch override in Vault through its parent follows the parent's policy until it's given
238+ // its own, so a save that sets no policy leaves it that way.
239+ if (
240+ own === undefined &&
241+ inherited !== undefined &&
242+ submitted . allowedHosts === undefined &&
243+ submitted . allowedHeaders === undefined
244+ ) {
245+ return { policy : "inherited" , covered } ;
246+ }
247+ const currentConfig = own ?? inherited ;
248+ const current = currentConfig === undefined ? undefined : parseProxyConfig ( currentConfig ) ;
249+ const checked = checkProxyPolicy ( {
250+ allowedHosts : submitted . allowedHosts ?? current ?. allowedHosts ?? [ ] ,
251+ allowedHeaders :
252+ submitted . allowedHeaders ?? current ?. allowedHeaders ?? DEFAULT_PROXY_ALLOWED_HEADERS ,
253+ } ) ;
254+ if ( ! checked . policy ) throw new Error ( checked . message ) ;
255+ return { policy : checked . policy , covered } ;
256+ }
257+
201258/**
202259 * Before these values are deleted or removed from Vault: a Preview branch's own value under one
203260 * of them keeps the policy it had through it, so a parent leaving Vault never unseals a branch or
@@ -1218,13 +1275,15 @@ export class EnvironmentVariablesRepository implements Repository {
12181275 /**
12191276 * Stores a value in Agent Vault, sent only as its policy allows. Moves an ordinary value in, or
12201277 * changes a Vault value's policy and, when `value` is set, its value. Never takes a value out.
1278+ * Policy fields left out keep the value's current policy, read under the variable's lock: its
1279+ * own, else its Vault parent's, else the defaults. There is no default for hosts.
12211280 */
12221281 async saveVaultValue (
12231282 projectId : string ,
12241283 options : {
12251284 id : string ;
12261285 environmentId : string ;
1227- policy : ProxyPolicy ;
1286+ policy : Partial < ProxyPolicy > ;
12281287 value ?: string ;
12291288 userId : string ;
12301289 }
@@ -1259,6 +1318,12 @@ export class EnvironmentVariablesRepository implements Repository {
12591318 await $transaction ( this . prismaClient , "save env var value in vault" , async ( tx ) => {
12601319 await lockVariableKeys ( tx , projectId , [ variable . key ] ) ;
12611320 await assertProjectVaultEnabled ( tx , projectId , [ options . environmentId ] ) ;
1321+ const { policy, covered } = await vaultPolicyToSave (
1322+ tx ,
1323+ variable . id ,
1324+ options . environmentId ,
1325+ options . policy
1326+ ) ;
12621327 if ( prepared && options . value !== undefined ) {
12631328 assertVaultValueSize ( options . value ) ;
12641329 await this . encryption . assertWrite ( tx , [ ...prepared . values ( ) ] ) ;
@@ -1272,30 +1337,36 @@ export class EnvironmentVariablesRepository implements Repository {
12721337 secretKey ( projectId , options . environmentId , variable . key ) ,
12731338 { secret : options . value }
12741339 ) ;
1275- } else {
1276- // Moving a value in keeps the stored credential, and the proxy refuses large ones.
1277- // Read only to measure it, inside the transaction, along with any branch overrides the
1278- // move newly brings into Vault.
1279- const covered = await tx . environmentVariableValue . findMany ( {
1280- where : {
1281- variableId : variable . id ,
1282- isProxyOnly : false ,
1283- OR : [
1284- { environmentId : options . environmentId } ,
1285- { environment : { parentEnvironmentId : options . environmentId , archivedAt : null } } ,
1286- ] ,
1287- } ,
1288- select : { environmentId : true } ,
1289- } ) ;
1340+ }
1341+ // The proxy refuses large credentials. A move into Vault newly covers this value, unless
1342+ // it was just replaced, and the branch overrides under it. Those stored values are read
1343+ // only to measure them, inside the transaction. A value already in Vault covers them
1344+ // already, so they aren't read again.
1345+ const newlyCovered = covered
1346+ ? [ ]
1347+ : await tx . environmentVariableValue . findMany ( {
1348+ where : {
1349+ variableId : variable . id ,
1350+ isProxyOnly : false ,
1351+ OR : [
1352+ ...( options . value === undefined
1353+ ? [ { environmentId : options . environmentId } ]
1354+ : [ ] ) ,
1355+ { environment : { parentEnvironmentId : options . environmentId , archivedAt : null } } ,
1356+ ] ,
1357+ } ,
1358+ select : { environmentId : true } ,
1359+ } ) ;
1360+ if ( newlyCovered . length ) {
12901361 const stored = await this . #secretStore(
12911362 tx ,
12921363 projectId ,
1293- covered . map ( ( value ) => value . environmentId ) ,
1364+ newlyCovered . map ( ( value ) => value . environmentId ) ,
12941365 undefined ,
12951366 new Set ( )
12961367 ) . getSecretsByKeys (
12971368 SecretValue ,
1298- covered . map ( ( value ) => secretKey ( projectId , value . environmentId , variable . key ) )
1369+ newlyCovered . map ( ( value ) => secretKey ( projectId , value . environmentId , variable . key ) )
12991370 ) ;
13001371 stored . forEach ( ( secret ) => assertVaultValueSize ( secret . value . secret ) ) ;
13011372 }
@@ -1307,9 +1378,9 @@ export class EnvironmentVariablesRepository implements Repository {
13071378 } ,
13081379 } ,
13091380 data : {
1310- isProxyOnly : true ,
1311- isSecret : true ,
1312- proxyConfig : options . policy ,
1381+ ... ( policy === "inherited"
1382+ ? { }
1383+ : { isProxyOnly : true , isSecret : true , proxyConfig : policy } ) ,
13131384 version : { increment : 1 } ,
13141385 lastUpdatedBy : { type : "user" , userId : options . userId } ,
13151386 } ,
0 commit comments