@@ -177,6 +177,22 @@ async function seedDeployedEnvironment() {
177177 return { ...seed , environment } ;
178178}
179179
180+ async function packetReaderFor ( environmentId : string ) {
181+ const user = await seedTestUser ( database . prisma ) ;
182+ const key = `tr_prod_sk_${ randomBytes ( 12 ) . toString ( "hex" ) } ` ;
183+ await database . prisma . apiKey . create ( {
184+ data : {
185+ name : "Restricted packet reader" ,
186+ keyHash : createHash ( "sha256" ) . update ( key ) . digest ( "hex" ) ,
187+ lastFour : key . slice ( - 4 ) ,
188+ runtimeEnvironmentId : environmentId ,
189+ createdByUserId : user . id ,
190+ scopes : [ "read:packets" ] ,
191+ } ,
192+ } ) ;
193+ return key ;
194+ }
195+
180196describe ( "run-token API authorization" , ( ) => {
181197 it ( "creates usable preview and production waitpoint callbacks and public JWTs" , async ( ) => {
182198 await assertRunTokenWaitpoints ( database . prisma , tokenFor ) ;
@@ -1159,7 +1175,7 @@ describe("run-token API authorization", () => {
11591175 unscopedInitialization . mockRestore ( ) ;
11601176 } ) ;
11611177
1162- // Catches: offloaded wait results needing environment-wide packet access , or leaking other waitpoints.
1178+ // Catches: packet-only keys losing offloaded wait results , or run grants leaking other waitpoints.
11631179 it ( "serves offloaded output only for waitpoints the run resumed from" , async ( ) => {
11641180 const { environment, project, organization } = await seedDeployedEnvironment ( ) ;
11651181 const { run } = await seedTestRun ( database . prisma , {
@@ -1199,21 +1215,72 @@ describe("run-token API authorization", () => {
11991215 } ) ;
12001216 const token = await tokenFor ( environment , [ `read:runs:${ run . friendlyId } ` ] ) ;
12011217 const { loader } = await import ( "~/routes/api.v1.runs.$runId.waitpoints.$waitpointId.output" ) ;
1202- const load = ( waitpointId : string ) =>
1203- loader ( {
1204- request : new Request ( "https://api.example.com/test" , {
1205- headers : { Authorization : `Bearer ${ token } ` } ,
1206- } ) ,
1207- params : { runId : run . friendlyId , waitpointId } ,
1208- context : { } ,
1209- } ) ;
1218+ for ( const bearer of [ token , await packetReaderFor ( environment . id ) ] ) {
1219+ const load = ( waitpointId : string ) =>
1220+ loader ( {
1221+ request : new Request ( "https://api.example.com/test" , {
1222+ headers : { Authorization : `Bearer ${ bearer } ` } ,
1223+ } ) ,
1224+ params : { runId : run . friendlyId , waitpointId } ,
1225+ context : { } ,
1226+ } ) ;
12101227
1211- const response = await load ( resumed . friendlyId ) ;
1212- expect ( response . status ) . toBe ( 200 ) ;
1213- expect ( ( await response . json ( ) ) . presignedUrl ) . toContain ( "waitpoint_resumed/stream/record.json" ) ;
1214- expect ( ( await load ( other . friendlyId ) ) . status ) . toBe ( 404 ) ;
1228+ const response = await load ( resumed . friendlyId ) ;
1229+ expect ( response . status ) . toBe ( 200 ) ;
1230+ expect ( ( await response . json ( ) ) . presignedUrl ) . toContain (
1231+ "waitpoint_resumed/stream/record.json"
1232+ ) ;
1233+ expect ( ( await load ( other . friendlyId ) ) . status ) . toBe ( 404 ) ;
1234+ }
12151235 } ) ;
12161236
1237+ // Catches: packet-only keys losing run packets, or bypassing shared development namespace isolation.
1238+ it . each ( [ "PRODUCTION" , "DEVELOPMENT" ] as const ) (
1239+ "preserves run packet access and namespace isolation in %s" ,
1240+ async ( environmentType ) => {
1241+ const { environment, project } =
1242+ environmentType === "PRODUCTION"
1243+ ? await seedDeployedEnvironment ( )
1244+ : await seedTestEnvironment ( database . prisma ) ;
1245+ const { run } = await seedTestRun ( database . prisma , {
1246+ environmentId : environment . id ,
1247+ projectId : project . id ,
1248+ } ) ;
1249+ await database . prisma . taskRun . update ( {
1250+ where : { id : run . id } ,
1251+ data : {
1252+ payload : "stored/payload.json" ,
1253+ payloadType : "application/store" ,
1254+ output : "stored/output.json" ,
1255+ outputType : "application/store" ,
1256+ } ,
1257+ } ) ;
1258+ const key = await packetReaderFor ( environment . id ) ;
1259+ const runToken = await tokenFor ( environment , [ `read:runs:${ run . friendlyId } ` , "read:packets" ] ) ;
1260+ const foreign = await seedDeployedEnvironment ( ) ;
1261+ const foreignKey = await packetReaderFor ( foreign . environment . id ) ;
1262+ const { loader } = await import ( "~/routes/api.v1.runs.$runId.packets.$field" ) ;
1263+ const load = ( field : "payload" | "output" , bearer : string ) =>
1264+ loader ( {
1265+ request : new Request ( "https://api.example.com/test" , {
1266+ headers : { Authorization : `Bearer ${ bearer } ` } ,
1267+ } ) ,
1268+ params : { runId : run . friendlyId , field } ,
1269+ context : { } ,
1270+ } ) ;
1271+
1272+ for ( const field of [ "payload" , "output" ] as const ) {
1273+ const response = await load ( field , key ) ;
1274+ expect ( response . status ) . toBe ( environmentType === "PRODUCTION" ? 200 : 403 ) ;
1275+ if ( response . status === 200 ) {
1276+ expect ( ( await response . json ( ) ) . presignedUrl ) . toContain ( `stored/${ field } .json` ) ;
1277+ }
1278+ expect ( ( await load ( field , runToken ) ) . status ) . toBe ( 200 ) ;
1279+ expect ( ( await load ( field , foreignKey ) ) . status ) . toBe ( 404 ) ;
1280+ }
1281+ }
1282+ ) ;
1283+
12171284 // Catches: session output setup ignoring direction-scoped session grants, or a `.out` grant opening `.in`.
12181285 it ( "lets a direction-scoped run token initialize only that session direction" , async ( ) => {
12191286 const { environment } = await seedDeployedEnvironment ( ) ;
0 commit comments