@@ -97,6 +97,7 @@ func handleImportREST(ctx context.Context, ch chan<- SourceRecord, config Config
9797 hasUpdateTime = true
9898 }
9999 timeOfRun := time .Now ()
100+ lastModTime := time.Time {}
100101 if hasUpdateTime {
101102 // HEAD request to check if there are updates
102103 req , err := http .NewRequest (http .MethodHead , sourceRepo .REST .URL , nil )
@@ -110,8 +111,8 @@ func handleImportREST(ctx context.Context, ch chan<- SourceRecord, config Config
110111 }
111112 resp .Body .Close ()
112113 lastModified := resp .Header .Get ("Last-Modified" )
113- mod , err : = time .Parse (time .RFC1123 , lastModified )
114- if err == nil && mod .Before (lastUpdated ) {
114+ lastModTime , err = time .Parse (time .RFC1123 , lastModified )
115+ if err == nil && lastModTime .Before (lastUpdated ) {
115116 logger .Info ("No changes since last update." ,
116117 slog .String ("source" , sourceRepo .Name ),
117118 slog .String ("url" , sourceRepo .REST .URL ))
@@ -157,6 +158,7 @@ func handleImportREST(ctx context.Context, ch chan<- SourceRecord, config Config
157158
158159 return fmt .Errorf ("REST API response is not an array for %s" , sourceRepo .REST .URL )
159160 }
161+ maxModified := time.Time {}
160162 result .ForEach (func (_ , vuln gjson.Result ) bool {
161163 id := vuln .Get ("id" )
162164 if ! id .Exists () {
@@ -176,6 +178,9 @@ func handleImportREST(ctx context.Context, ch chan<- SourceRecord, config Config
176178
177179 return true
178180 }
181+ if mod .After (maxModified ) {
182+ maxModified = mod
183+ }
179184 if hasUpdateTime && mod .Before (lastUpdated ) {
180185 return true
181186 }
@@ -196,7 +201,19 @@ func handleImportREST(ctx context.Context, ch chan<- SourceRecord, config Config
196201 return true
197202 })
198203
199- sourceRepo .REST .LastUpdated = & timeOfRun
204+ // Set the last updated time to the minimum of:
205+ // - the time of run
206+ // - the max vulnerability modified time
207+ // - the Last-Modified time of the REST API response
208+ // This is to be more robust in case of misbehaving servers.
209+ timeToUpdate := timeOfRun
210+ if ! maxModified .IsZero () && maxModified .Before (timeToUpdate ) {
211+ timeToUpdate = maxModified
212+ }
213+ if ! lastModTime .IsZero () && lastModTime .Before (timeToUpdate ) {
214+ timeToUpdate = lastModTime
215+ }
216+ sourceRepo .REST .LastUpdated = & timeToUpdate
200217 sourceRepo .REST .IgnoreLastImportTime = false
201218 if err := config .SourceRepoStore .Update (ctx , sourceRepo .Name , sourceRepo ); err != nil {
202219 logger .Error ("Failed to update source repository" , slog .Any ("error" , err ), slog .String ("source" , sourceRepo .Name ))
0 commit comments