@@ -14,6 +14,10 @@ const sigstore = require('sigstore')
1414const corgiDoc = 'application/vnd.npm.install-v1+json; q=1.0, application/json; q=0.8, */*'
1515const fullDoc = 'application/json'
1616
17+ // Some really old packages have no time field in their packument so we need a
18+ // cutoff date.
19+ const MISSING_TIME_CUTOFF = '2015-01-01T00:00:00.000Z'
20+
1721const fetch = require ( 'npm-registry-fetch' )
1822
1923const _headers = Symbol ( '_headers' )
@@ -115,6 +119,13 @@ class RegistryFetcher extends Fetcher {
115119 return this . package
116120 }
117121
122+ // When verifying signatures, we need to fetch the full/uncompressed
123+ // packument to get publish time as this is not included in the
124+ // corgi/compressed packument.
125+ if ( this . opts . verifySignatures ) {
126+ this . fullMetadata = true
127+ }
128+
118129 const packument = await this . packument ( )
119130 let mani = await pickManifest ( packument , this . spec . fetchSpec , {
120131 ...this . opts ,
@@ -124,6 +135,12 @@ class RegistryFetcher extends Fetcher {
124135 mani = rpj . normalize ( mani )
125136 /* XXX add ETARGET and E403 revalidation of cached packuments here */
126137
138+ // add _time from packument if fetched with fullMetadata
139+ const time = packument . time ?. [ mani . version ]
140+ if ( time ) {
141+ mani . _time = time
142+ }
143+
127144 // add _resolved and _integrity from dist object
128145 const { dist } = mani
129146 if ( dist ) {
@@ -171,8 +188,10 @@ class RegistryFetcher extends Fetcher {
171188 'but no corresponding public key can be found'
172189 ) , { code : 'EMISSINGSIGNATUREKEY' } )
173190 }
174- const validPublicKey =
175- ! publicKey . expires || ( Date . parse ( publicKey . expires ) > Date . now ( ) )
191+
192+ const publishedTime = Date . parse ( mani . _time || MISSING_TIME_CUTOFF )
193+ const validPublicKey = ! publicKey . expires ||
194+ publishedTime < Date . parse ( publicKey . expires )
176195 if ( ! validPublicKey ) {
177196 throw Object . assign ( new Error (
178197 `${ mani . _id } has a registry signature with keyid: ${ signature . keyid } ` +
@@ -254,8 +273,13 @@ class RegistryFetcher extends Fetcher {
254273 ) , { code : 'EMISSINGSIGNATUREKEY' } )
255274 }
256275
257- const validPublicKey =
258- ! publicKey . expires || ( Date . parse ( publicKey . expires ) > Date . now ( ) )
276+ const integratedTime = new Date (
277+ Number (
278+ bundle . verificationMaterial . tlogEntries [ 0 ] . integratedTime
279+ ) * 1000
280+ )
281+ const validPublicKey = ! publicKey . expires ||
282+ ( integratedTime < Date . parse ( publicKey . expires ) )
259283 if ( ! validPublicKey ) {
260284 throw Object . assign ( new Error (
261285 `${ mani . _id } has attestations with keyid: ${ keyid } ` +
0 commit comments