Skip to content

feat(dir): add prune-untrusted job - #2220

Merged
csirmazbendeguz merged 1 commit into
mainfrom
feat/prune-untrusted-job
Oct 6, 2026
Merged

csirmazbendeguz merged 1 commit into
mainfrom
feat/prune-untrusted-job

Conversation

@csirmazbendeguz

@csirmazbendeguz csirmazbendeguz commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

#2136

Adds the prune job to reconciler.
The behaviour is similar to the prune-untrusted K8S CronJob.

@github-actions github-actions Bot added the size/M Denotes a PR that changes 200-999 lines label Sep 29, 2026
@csirmazbendeguz
csirmazbendeguz marked this pull request as ready for review September 30, 2026 17:40
@csirmazbendeguz
csirmazbendeguz requested a review from a team as a code owner September 30, 2026 17:40
@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.50943% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
reconciler/service/service.go 20.0% 4 Missing ⚠️
reconciler/tasks/prune/task.go 96.7% 2 Missing ⚠️
server/types/search.go 0.0% 2 Missing ⚠️
server/database/gorm/record.go 50.0% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@lgecse
lgecse force-pushed the feat/prune-untrusted-job branch from 339d8af to 81e4635 Compare October 1, 2026 08:31

@tkircsi tkircsi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scan_severity is a free-form string that nothing validates, and the database layer responds to a value it doesn't recognize by silently dropping the severity condition from the SQL rather than by returning an error. The task's query then degenerates from "untrusted AND scanning at MEDIUM or worse" to just "untrusted", and the task deletes everything that comes back. Check : dir/server/database/gorm/record.go / scanSeveritiesGTE - returns nil for "MEDUIM"

Optionally extend the existing TestConfig_GetScanSeverity neighbourhood in reconciler/tasks/pruneuntrusted/task_test.go with a case asserting NewTask(Config{ScanSeverity: "MEDUIM"}, ...) returns an error.

Comment thread reconciler/tasks/prune/task.go Resolved
Comment thread reconciler/tasks/pruneuntrusted/task.go Resolved
Comment thread reconciler/tasks/prune/task.go Resolved

@tkircsi tkircsi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No limit and no dry-run for a destructive policy. Is it safe?

Comment thread reconciler/tasks/prune/task.go Resolved
@csirmazbendeguz
csirmazbendeguz force-pushed the feat/prune-untrusted-job branch from 81e4635 to c592980 Compare October 2, 2026 12:34
@csirmazbendeguz

Copy link
Copy Markdown
Contributor Author

thanks @tkircsi

you're right - first need to refactor scan severities a bit: #2274

@csirmazbendeguz
csirmazbendeguz force-pushed the feat/prune-untrusted-job branch 2 times, most recently from 0e9ad87 to 4a8740f Compare October 5, 2026 10:23
@csirmazbendeguz

Copy link
Copy Markdown
Contributor Author

scan_severity is a free-form string that nothing validates, and the database layer responds to a value it doesn't recognize by silently dropping the severity condition from the SQL rather than by returning an error. The task's query then degenerates from "untrusted AND scanning at MEDIUM or worse" to just "untrusted", and the task deletes everything that comes back. Check : dir/server/database/gorm/record.go / scanSeveritiesGTE - returns nil for "MEDUIM"

Optionally extend the existing TestConfig_GetScanSeverity neighbourhood in reconciler/tasks/pruneuntrusted/task_test.go with a case asserting NewTask(Config{ScanSeverity: "MEDUIM"}, ...) returns an error.

Great find, thanks @tkircsi ! I did some refactoring on scan severities and added a validation

@csirmazbendeguz
csirmazbendeguz force-pushed the feat/prune-untrusted-job branch 2 times, most recently from c3f5cdf to 25a7ef5 Compare October 5, 2026 12:28
@csirmazbendeguz

Copy link
Copy Markdown
Contributor Author

No limit and no dry-run for a destructive policy. Is it safe?

It's a good point about limit, I added it.

About the dry-run... ok, I added it and enabled it by default. However, I'm not sure this is really necessary

@csirmazbendeguz
csirmazbendeguz force-pushed the feat/prune-untrusted-job branch from 25a7ef5 to f9671ef Compare October 5, 2026 12:29
Comment thread cli/cmd/daemon/daemon.config.yaml Resolved
Comment thread cli/cmd/daemon/daemon.config.yaml Resolved
Comment thread reconciler/tasks/prune/task.go Resolved
Comment thread reconciler/tasks/prune/task.go Outdated
Comment on lines +76 to +83
createdBefore := "<" + time.Now().Add(-olderThan).UTC().Format(time.RFC3339)

cids, err := t.db.GetRecordCIDs(
types.WithTrusted(t.config.Criteria.Trusted),
types.WithScanSeverities(t.config.GetMinSeverity()),
types.WithCreatedAts(createdBefore),
types.WithLimit(t.config.GetLimit()),
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The age floor doesn't protect what it was added to protect. older_than filters on the record's self-declared OASF timestamp, because WithCreatedAts maps to records.oasf_created_at (server/database/gorm/record.go:546), a nullable string column populated verbatim from record.GetCreatedAt().

Three consequences.

  • First, created_at is optional on a record, and the repo's own comment at server/database/gorm/record_exclude.go:56-57 states that "AddRecord always writes '' today" — and '' < '2026-09-29T...' is TRUE in SQL, so any record omitting created_at gets no grace period at all and is eligible on the first run.
  • Second, the value is attacker-controlled for exactly the records this task targets: an untrusted record claiming created_at: 2099-… is never pruned, one claiming 1970 is pruned immediately.
  • Third, it's a lexicographic string comparison rather than a timestamp one, so non-UTC offsets or fractional seconds misorder near the cutoff.

Proposal
Switch the age floor from the record's self-declared oasf_created_at to the index's own records.created_at, which is the local first-seen timestamp. That column already exists on the model (server/database/gorm/record.go:48), it just has no filter option. Make it a time.Time compared as a timestamp rather than a string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a WithIndexedBefore filter option for this

@csirmazbendeguz
csirmazbendeguz force-pushed the feat/prune-untrusted-job branch from 16eceb9 to 3e373c5 Compare October 6, 2026 11:23
Signed-off-by: Bendegúz Csirmaz <[email protected]>
@csirmazbendeguz
csirmazbendeguz force-pushed the feat/prune-untrusted-job branch from 3e373c5 to c1eb756 Compare October 6, 2026 11:29

@ramizpolic ramizpolic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM w/ nit

please create an issue for the verification state UNKNOWN vs FAILED so that we can patch it up later

@csirmazbendeguz
csirmazbendeguz added this pull request to the merge queue Oct 6, 2026
Merged via the queue into main with commit 79344d4 Oct 6, 2026
35 checks passed
@csirmazbendeguz
csirmazbendeguz deleted the feat/prune-untrusted-job branch October 6, 2026 12:06
@csirmazbendeguz

Copy link
Copy Markdown
Contributor Author

LGTM w/ nit

please create an issue for the verification state UNKNOWN vs FAILED so that we can patch it up later

#2294

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M Denotes a PR that changes 200-999 lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants