@@ -13,11 +13,20 @@ import {
1313
1414import * as GitHubCli from "../sourceControl/GitHubCli.ts" ;
1515
16- /** Hosts the credential is for. A redirect off them is answered with its own signature instead. */
17- const GITHUB_HOST_PATTERN = / ^ (?: [ \w - ] + \. ) * g i t h u b (?: u s e r c o n t e n t ) ? \. c o m $ / iu;
18- const isGitHubHost = ( url : string ) => {
16+ /**
17+ * Exactly the hosts the credential is for. Everything a redirect leads to — the presigned
18+ * object stores GitHub hands assets off to — authorizes with its own signature, and some of
19+ * them reject a request that also carries a bearer token.
20+ */
21+ const CREDENTIALED_HOSTS = new Set ( [
22+ "github.com" ,
23+ "www.github.com" ,
24+ "raw.githubusercontent.com" ,
25+ "media.githubusercontent.com" ,
26+ ] ) ;
27+ const isCredentialedHost = ( url : string ) => {
1928 try {
20- return GITHUB_HOST_PATTERN . test ( new URL ( url ) . hostname ) ;
29+ return CREDENTIALED_HOSTS . has ( new URL ( url ) . hostname . toLowerCase ( ) ) ;
2130 } catch {
2231 return false ;
2332 }
@@ -51,14 +60,18 @@ const SVG_CONTENT_SECURITY_POLICY = "default-src 'none'; style-src 'unsafe-inlin
5160 * The token is what `gh auth token` would print again on the next call, and it is held no longer
5261 * than a signed asset URL lives.
5362 */
54- const tokenCache = new Map < string , { readonly at : number ; readonly token : Redacted . Redacted } > ( ) ;
63+ const tokenCache = new Map <
64+ string ,
65+ { readonly at : number ; readonly token : Redacted . Redacted | null }
66+ > ( ) ;
5567
5668const githubToken = Effect . fn ( "GitHubMediaFetch.githubToken" ) ( function * ( input : {
5769 readonly cwd : string ;
5870 readonly host : string ;
5971} ) {
6072 // `gh` stores a token per host, not per repository, so the directory it runs in is not part
61- // of the answer and must not fragment the cache a client could otherwise churn.
73+ // of the answer and must not fragment the cache a client could otherwise churn. This route
74+ // pins no credential; if it ever does, the pin belongs in this key.
6275 const key = input . host ;
6376 const now = yield * Clock . currentTimeMillis ;
6477 const cached = tokenCache . get ( key ) ;
@@ -76,11 +89,12 @@ const githubToken = Effect.fn("GitHubMediaFetch.githubToken")(function* (input:
7689 Effect . map ( ( output ) => output . stdout . trim ( ) ) ,
7790 Effect . orElseSucceed ( ( ) => "" ) ,
7891 ) ;
79- if ( token . length === 0 ) return null ;
8092 if ( tokenCache . size >= TOKEN_CACHE_MAX_ENTRIES ) {
8193 tokenCache . delete ( tokenCache . keys ( ) . next ( ) . value ! ) ;
8294 }
83- const redacted = Redacted . make ( token ) ;
95+ // The absence of a credential is cached too, or an unauthenticated machine spawns `gh` again
96+ // for every image and every video range request.
97+ const redacted = token . length === 0 ? null : Redacted . make ( token ) ;
8498 tokenCache . set ( key , { at : now , token : redacted } ) ;
8599 return redacted ;
86100} ) ;
@@ -102,7 +116,7 @@ const fetchFollowingRedirects = Effect.fn("GitHubMediaFetch.fetchFollowingRedire
102116 // object URL that authorizes on its own, and the store it lives in has no business seeing
103117 // a token — deciding that from the target, not from the hop count, is what makes it so.
104118 const authorization =
105- token !== null && isGitHubHost ( target ) ? `Bearer ${ Redacted . value ( token ) } ` : null ;
119+ token !== null && isCredentialedHost ( target ) ? `Bearer ${ Redacted . value ( token ) } ` : null ;
106120 const response : HttpClientResponse . HttpClientResponse = yield * httpClient
107121 . execute (
108122 HttpClientRequest . get ( target ) . pipe (
@@ -130,7 +144,7 @@ const fetchFollowingRedirects = Effect.fn("GitHubMediaFetch.fetchFollowingRedire
130144 * only thing that distinguishes a readable private attachment from a 404.
131145 */
132146export const githubMediaResponse = Effect . fn ( "GitHubMediaFetch.githubMediaResponse" ) ( function * (
133- asset : { readonly url : string ; readonly cwd : string } ,
147+ asset : { readonly url : string ; readonly cwd : string ; readonly expiresAt : number } ,
134148 requestHeaders : Record < string , string | undefined > ,
135149) {
136150 // Both media hosts are served by github.com's account, which is the host `gh` stores it under.
@@ -141,9 +155,12 @@ export const githubMediaResponse = Effect.fn("GitHubMediaFetch.githubMediaRespon
141155 if ( value !== undefined ) forwarded [ name ] = value ;
142156 }
143157 const response = yield * fetchFollowingRedirects ( asset . url , forwarded , token ) ;
158+ // An upload GitHub hosts never changes under its URL, so the only thing a cached copy must
159+ // not outlive is the signed URL that granted it — which is the same bound the URL itself has.
160+ const remainingSeconds = Math . floor ( ( asset . expiresAt - ( yield * Clock . currentTimeMillis ) ) / 1000 ) ;
144161 const headers : Record < string , string > = {
145- // The signed asset URL is the grant; a cached copy must not outlive it.
146- "cache-control" : "private, no-store" ,
162+ "cache-control" :
163+ remainingSeconds > 0 ? `private, max-age= ${ remainingSeconds } ` : "private, no-store" ,
147164 "x-content-type-options" : "nosniff" ,
148165 } ;
149166 if ( response === null ) return HttpServerResponse . empty ( { status : 502 , headers } ) ;
@@ -167,7 +184,7 @@ export const githubMediaResponse = Effect.fn("GitHubMediaFetch.githubMediaRespon
167184 ? upstreamType
168185 : ( Mime . getType ( githubMediaFileName ( asset . url ) ) ?. toLowerCase ( ) ?? "" ) ;
169186 if ( ! MEDIA_CONTENT_TYPE_PATTERN . test ( contentType ) ) {
170- return HttpServerResponse . empty ( { status : 415 } ) ;
187+ return HttpServerResponse . empty ( { status : 415 , headers } ) ;
171188 }
172189 headers [ "content-type" ] = contentType ;
173190 if ( contentType === SVG_CONTENT_TYPE ) {
0 commit comments