From fb2e565c7b01beb511cc3e5373fc6c6d429e852c Mon Sep 17 00:00:00 2001 From: Xueqin Cui Date: Tue, 3 Mar 2026 16:15:52 +1100 Subject: [PATCH 1/3] Use osvschema Go bindings --- vulnfeeds/cmd/ids/main.go | 66 ++++++++---- vulnfeeds/cmd/ids/main_test.go | 102 ++++++++++++++++++ vulnfeeds/cmd/ids/testdata/OSV-0000-abc.yaml | 31 ++++++ vulnfeeds/cmd/ids/testdata/OSV-2026-10.yaml | 2 + vulnfeeds/cmd/ids/testdata/OSV-2026-11.yaml | 31 ++++++ vulnfeeds/cmd/ids/testdata/TEST-0000-def.json | 55 ++++++++++ vulnfeeds/cmd/ids/testdata/TEST-2026-20.json | 4 + vulnfeeds/cmd/ids/testdata/TEST-2026-21.json | 55 ++++++++++ vulnfeeds/go.mod | 4 +- vulnfeeds/go.sum | 4 - 10 files changed, 325 insertions(+), 29 deletions(-) create mode 100644 vulnfeeds/cmd/ids/main_test.go create mode 100644 vulnfeeds/cmd/ids/testdata/OSV-0000-abc.yaml create mode 100644 vulnfeeds/cmd/ids/testdata/OSV-2026-10.yaml create mode 100644 vulnfeeds/cmd/ids/testdata/OSV-2026-11.yaml create mode 100644 vulnfeeds/cmd/ids/testdata/TEST-0000-def.json create mode 100644 vulnfeeds/cmd/ids/testdata/TEST-2026-20.json create mode 100644 vulnfeeds/cmd/ids/testdata/TEST-2026-21.json diff --git a/vulnfeeds/cmd/ids/main.go b/vulnfeeds/cmd/ids/main.go index a9dc2332ee7..33a9a04cbff 100644 --- a/vulnfeeds/cmd/ids/main.go +++ b/vulnfeeds/cmd/ids/main.go @@ -4,7 +4,6 @@ package main import ( "crypto/rand" "encoding/hex" - "encoding/json" "flag" "fmt" "io" @@ -17,9 +16,10 @@ import ( "strings" "time" - "github.com/google/osv-scanner/pkg/models" + "github.com/goccy/go-yaml" "github.com/google/osv/vulnfeeds/utility/logger" - "gopkg.in/yaml.v2" + "github.com/ossf/osv-schema/bindings/go/osvschema" + "google.golang.org/protobuf/encoding/protojson" ) const ( @@ -112,16 +112,16 @@ func assignID(prefix, path string, format fileFormat, yearCounters map[int]int, // If the vulnerability has a published date, use the year from that. // Otherwise, just default to the current year. year := defaultYear - if !vuln.Published.IsZero() { - year = vuln.Published.Year() + if vuln.Published != nil { + year = vuln.Published.AsTime().Year() } // Allocate a new ID and write the new file. id := yearCounters[year] + 1 yearCounters[year] = id - vuln.ID = fmt.Sprintf("%s-%d-%d", prefix, year, id) - newPath := filepath.Join(filepath.Dir(path), vuln.ID+formatToExtension[format]) + vuln.Id = fmt.Sprintf("%s-%d-%d", prefix, year, id) + newPath := filepath.Join(filepath.Dir(path), vuln.Id+formatToExtension[format]) writef, err := os.Create(newPath) if err != nil { @@ -193,36 +193,58 @@ func assignIDs(prefix, dir string, format fileFormat) error { return os.WriteFile(filepath.Join(dir, conflictFile), []byte(hex.EncodeToString(b)), 0600) } -func readVulnWithFormat(r io.Reader, format fileFormat) (*models.Vulnerability, error) { - var v models.Vulnerability +func readVulnWithFormat(r io.Reader, format fileFormat) (*osvschema.Vulnerability, error) { + data, err := io.ReadAll(r) + if err != nil { + return nil, err + } + + var v osvschema.Vulnerability switch format { case fileFormatJSON: - dec := json.NewDecoder(r) - if err := dec.Decode(&v); err != nil { - return nil, err - } + err = protojson.Unmarshal(data, &v) case fileFormatYAML: - dec := yaml.NewDecoder(r) - if err := dec.Decode(&v); err != nil { + jsonBytes, err := yaml.YAMLToJSON(data) + if err != nil { return nil, err } + err = protojson.Unmarshal(jsonBytes, &v) default: return nil, fmt.Errorf("unknown file format: %v", format) } + if err != nil { + return nil, err + } + return &v, nil } -func writeVulnWithFormat(v *models.Vulnerability, w io.Writer, format fileFormat) error { +func writeVulnWithFormat(v *osvschema.Vulnerability, w io.Writer, format fileFormat) error { + marshaler := protojson.MarshalOptions{ + Multiline: true, + Indent: " ", + } + switch format { case fileFormatJSON: - enc := json.NewEncoder(w) - enc.SetIndent("", " ") - - return enc.Encode(v) + data, err := marshaler.Marshal(v) + if err != nil { + return err + } + _, err = w.Write(data) + return err case fileFormatYAML: - enc := yaml.NewEncoder(w) - return enc.Encode(v) + jsonBytes, err := marshaler.Marshal(v) + if err != nil { + return err + } + yamlBytes, err := yaml.JSONToYAML(jsonBytes) + if err != nil { + return err + } + _, err = w.Write(yamlBytes) + return err default: return fmt.Errorf("unknown file format: %v", format) } diff --git a/vulnfeeds/cmd/ids/main_test.go b/vulnfeeds/cmd/ids/main_test.go new file mode 100644 index 00000000000..3507a05c188 --- /dev/null +++ b/vulnfeeds/cmd/ids/main_test.go @@ -0,0 +1,102 @@ +package main + +import ( + "bytes" + "os" + "path/filepath" + "testing" +) + +func TestAssignIDs(t *testing.T) { + tests := []struct { + name string + prefix string + format fileFormat + templateName string + existingName string + expectedID string + }{ + { + name: "OSV YAML", + prefix: "OSV", + format: fileFormatYAML, + templateName: "OSV-0000-abc.yaml", + existingName: "OSV-2026-10.yaml", + expectedID: "OSV-2026-11", + }, + { + name: "TEST JSON", + prefix: "TEST", + format: fileFormatJSON, + templateName: "TEST-0000-def.json", + existingName: "TEST-2026-20.json", + expectedID: "TEST-2026-21", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tmpDir := t.TempDir() + + // 1. Copy existing assigned vulnerability from testdata to set counter. + existingPath := filepath.Join("testdata", tt.existingName) + existingData, err := os.ReadFile(existingPath) + if err != nil { + t.Fatalf("failed to read existing ID: %v", err) + } + err = os.WriteFile(filepath.Join(tmpDir, tt.existingName), existingData, 0644) + if err != nil { + t.Fatalf("failed to copy existing ID: %v", err) + } + + // 2. Setup unassigned vulnerability using template. + templatePath := filepath.Join("testdata", tt.templateName) + templateData, err := os.ReadFile(templatePath) + if err != nil { + t.Fatalf("failed to read template %s: %v", templatePath, err) + } + destPath := filepath.Join(tmpDir, tt.templateName) + if err := os.WriteFile(destPath, templateData, 0644); err != nil { + t.Fatalf("failed to setup unassigned vuln: %v", err) + } + + // 3. Run assignIDs. + if err := assignIDs(tt.prefix, tmpDir, tt.format); err != nil { + t.Fatalf("assignIDs failed: %v", err) + } + + // 4. Verify results. + ext := formatToExtension[tt.format] + expectedFilename := tt.expectedID + ext + gotPath := filepath.Join(tmpDir, expectedFilename) + if _, err := os.Stat(gotPath); os.IsNotExist(err) { + t.Errorf("Expected %s to exist", gotPath) + return + } + + if _, err := os.Stat(destPath); !os.IsNotExist(err) { + t.Errorf("Expected old file %s to be removed", tt.templateName) + } + + gotData, err := os.ReadFile(gotPath) + if err != nil { + t.Fatalf("failed to read assigned vuln %s: %v", gotPath, err) + } + + wantData, err := os.ReadFile(filepath.Join("testdata", expectedFilename)) + if err != nil { + t.Fatalf("failed to read expected vuln: %v", err) + } + + if !bytes.Equal(gotData, wantData) { + t.Errorf("Data fidelity mismatch for %s:\nGot:\n%s\nWant:\n%s", tt.expectedID, string(gotData), string(wantData)) + } + + // Verify .id-allocator was created. + if _, err := os.Stat(filepath.Join(tmpDir, ".id-allocator")); os.IsNotExist(err) { + t.Errorf("Expected .id-allocator to exist") + } + }) + } +} + diff --git a/vulnfeeds/cmd/ids/testdata/OSV-0000-abc.yaml b/vulnfeeds/cmd/ids/testdata/OSV-0000-abc.yaml new file mode 100644 index 00000000000..ed9f7e7648d --- /dev/null +++ b/vulnfeeds/cmd/ids/testdata/OSV-0000-abc.yaml @@ -0,0 +1,31 @@ +id: +published: "2026-01-02T00:00:00Z" +modified: "2026-01-02T00:00:00Z" +summary: A vulnerability +details: | + Blah blah blah + Blah +affected: +- package: + name: blah.com/package + ecosystem: Go + ranges: + - type: GIT + repo: https://osv-test/repo/url + events: + - introduced: eefe8ec3f1f90d0e684890e810f3f21e8500a4cd + - fixed: 8d8242f545e9cec3e6d0d2e3f5bde8be1c659735 + versions: + - branch-v0.1.1 +references: +- type: WEB + url: https://ref.com/ref +database_specific: + specific: 1337 +severity: +- type: CVSS_V3 + score: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:L +credits: +- name: Foo bar + contact: + - mailto:foo@bar.com diff --git a/vulnfeeds/cmd/ids/testdata/OSV-2026-10.yaml b/vulnfeeds/cmd/ids/testdata/OSV-2026-10.yaml new file mode 100644 index 00000000000..6a9e3529ed8 --- /dev/null +++ b/vulnfeeds/cmd/ids/testdata/OSV-2026-10.yaml @@ -0,0 +1,2 @@ +id: OSV-2026-10 +modified: '2026-01-01T00:00:00Z' diff --git a/vulnfeeds/cmd/ids/testdata/OSV-2026-11.yaml b/vulnfeeds/cmd/ids/testdata/OSV-2026-11.yaml new file mode 100644 index 00000000000..981a997166c --- /dev/null +++ b/vulnfeeds/cmd/ids/testdata/OSV-2026-11.yaml @@ -0,0 +1,31 @@ +id: OSV-2026-11 +published: "2026-01-02T00:00:00Z" +modified: "2026-01-02T00:00:00Z" +summary: A vulnerability +details: | + Blah blah blah + Blah +affected: +- package: + name: blah.com/package + ecosystem: Go + ranges: + - type: GIT + repo: https://osv-test/repo/url + events: + - introduced: eefe8ec3f1f90d0e684890e810f3f21e8500a4cd + - fixed: 8d8242f545e9cec3e6d0d2e3f5bde8be1c659735 + versions: + - branch-v0.1.1 +references: +- type: WEB + url: https://ref.com/ref +database_specific: + specific: 1337 +severity: +- type: CVSS_V3 + score: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:L +credits: +- name: Foo bar + contact: + - mailto:foo@bar.com diff --git a/vulnfeeds/cmd/ids/testdata/TEST-0000-def.json b/vulnfeeds/cmd/ids/testdata/TEST-0000-def.json new file mode 100644 index 00000000000..3fec72bd2b0 --- /dev/null +++ b/vulnfeeds/cmd/ids/testdata/TEST-0000-def.json @@ -0,0 +1,55 @@ +{ + "id": "", + "published": "2026-01-02T00:00:00Z", + "modified": "2026-01-02T00:00:00Z", + "summary": "A vulnerability", + "details": "Blah blah blah\nBlah\n", + "affected": [ + { + "package": { + "name": "blah.com/package", + "ecosystem": "Go" + }, + "ranges": [ + { + "type": "GIT", + "repo": "https://osv-test/repo/url", + "events": [ + { + "introduced": "eefe8ec3f1f90d0e684890e810f3f21e8500a4cd" + }, + { + "fixed": "8d8242f545e9cec3e6d0d2e3f5bde8be1c659735" + } + ] + } + ], + "versions": [ + "branch-v0.1.1" + ] + } + ], + "references": [ + { + "type": "WEB", + "url": "https://ref.com/ref" + } + ], + "database_specific": { + "specific": 1337 + }, + "severity": [ + { + "type": "CVSS_V3", + "score": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:L" + } + ], + "credits": [ + { + "name": "Foo bar", + "contact": [ + "mailto:foo@bar.com" + ] + } + ] +} diff --git a/vulnfeeds/cmd/ids/testdata/TEST-2026-20.json b/vulnfeeds/cmd/ids/testdata/TEST-2026-20.json new file mode 100644 index 00000000000..6da85fc064b --- /dev/null +++ b/vulnfeeds/cmd/ids/testdata/TEST-2026-20.json @@ -0,0 +1,4 @@ +{ + "id": "TEST-2026-20", + "modified": "2026-01-01T00:00:00Z" +} diff --git a/vulnfeeds/cmd/ids/testdata/TEST-2026-21.json b/vulnfeeds/cmd/ids/testdata/TEST-2026-21.json new file mode 100644 index 00000000000..765756d5323 --- /dev/null +++ b/vulnfeeds/cmd/ids/testdata/TEST-2026-21.json @@ -0,0 +1,55 @@ +{ + "id": "TEST-2026-21", + "published": "2026-01-02T00:00:00Z", + "modified": "2026-01-02T00:00:00Z", + "summary": "A vulnerability", + "details": "Blah blah blah\nBlah\n", + "affected": [ + { + "package": { + "name": "blah.com/package", + "ecosystem": "Go" + }, + "ranges": [ + { + "type": "GIT", + "repo": "https://osv-test/repo/url", + "events": [ + { + "introduced": "eefe8ec3f1f90d0e684890e810f3f21e8500a4cd" + }, + { + "fixed": "8d8242f545e9cec3e6d0d2e3f5bde8be1c659735" + } + ] + } + ], + "versions": [ + "branch-v0.1.1" + ] + } + ], + "references": [ + { + "type": "WEB", + "url": "https://ref.com/ref" + } + ], + "database_specific": { + "specific": 1337 + }, + "severity": [ + { + "type": "CVSS_V3", + "score": "CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:L/I:L/A:L" + } + ], + "credits": [ + { + "name": "Foo bar", + "contact": [ + "mailto:foo@bar.com" + ] + } + ] +} diff --git a/vulnfeeds/go.mod b/vulnfeeds/go.mod index 1775b827336..c8d8722539f 100644 --- a/vulnfeeds/go.mod +++ b/vulnfeeds/go.mod @@ -10,8 +10,8 @@ require ( github.com/charmbracelet/lipgloss v1.1.0 github.com/gkampitakis/go-snaps v0.5.19 github.com/go-git/go-git/v5 v5.16.5 + github.com/goccy/go-yaml v1.19.2 github.com/google/go-cmp v0.7.0 - github.com/google/osv-scanner v1.9.2 github.com/knqyf263/go-cpe v0.0.0-20230627041855-cb0794d06872 github.com/ossf/osv-schema/bindings/go v0.0.0-20260129002236-09a17f85b44a github.com/sethvargo/go-retry v0.3.0 @@ -55,7 +55,6 @@ require ( github.com/go-jose/go-jose/v4 v4.1.3 // indirect github.com/go-logr/logr v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect - github.com/goccy/go-yaml v1.19.2 // indirect github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect github.com/google/s2a-go v0.1.9 // indirect github.com/google/uuid v1.6.0 // indirect @@ -70,7 +69,6 @@ require ( github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect github.com/muesli/termenv v0.16.0 // indirect - github.com/package-url/packageurl-go v0.1.3 // indirect github.com/pjbgf/sha1cd v0.3.2 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect diff --git a/vulnfeeds/go.sum b/vulnfeeds/go.sum index 5828ef10537..0937677f8c3 100644 --- a/vulnfeeds/go.sum +++ b/vulnfeeds/go.sum @@ -117,8 +117,6 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= github.com/google/martian/v3 v3.3.3 h1:DIhPTQrbPkgs2yJYdXU/eNACCG5DVQjySNRNlflZ9Fc= github.com/google/martian/v3 v3.3.3/go.mod h1:iEPrYcgCF7jA9OtScMFQyAlZZ4YXTKEtJ1E6RWzmBA0= -github.com/google/osv-scanner v1.9.2 h1:N5Arl9SA75afbjmX8mKURgOIaKyuK3NUjCaxDlj1KHI= -github.com/google/osv-scanner v1.9.2/go.mod h1:ZTL8Dp9z/7Jr9kkQSOGqo8z6Csqt83qMIr58aZVx+pM= github.com/google/s2a-go v0.1.9 h1:LGD7gtMgezd8a/Xak7mEWL0PjoTQFvpRudN895yqKW0= github.com/google/s2a-go v0.1.9/go.mod h1:YA0Ei2ZQL3acow2O62kdp9UlnvMmU7kA6Eutn0dXayM= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -154,8 +152,6 @@ github.com/onsi/gomega v1.34.1 h1:EUMJIKUjM8sKjYbtxQI9A4z2o+rruxnzNvpknOXie6k= github.com/onsi/gomega v1.34.1/go.mod h1:kU1QgUvBDLXBJq618Xvm2LUX6rSAfRaFRTcdOeDLwwY= github.com/ossf/osv-schema/bindings/go v0.0.0-20260129002236-09a17f85b44a h1:XHKiGf7vsACh43gFCBb5lOxDjnY6CQYNoQXsO/SlI6k= github.com/ossf/osv-schema/bindings/go v0.0.0-20260129002236-09a17f85b44a/go.mod h1:Eo7R19vlnflsCRdHW1ynyNUyoRwxdaTmTWD9MtKnJTc= -github.com/package-url/packageurl-go v0.1.3 h1:4juMED3hHiz0set3Vq3KeQ75KD1avthoXLtmE3I0PLs= -github.com/package-url/packageurl-go v0.1.3/go.mod h1:nKAWB8E6uk1MHqiS/lQb9pYBGH2+mdJ2PJc2s50dQY0= github.com/pjbgf/sha1cd v0.3.2 h1:a9wb0bp1oC2TGwStyn0Umc/IGKQnEgF0vVaZ8QF8eo4= github.com/pjbgf/sha1cd v0.3.2/go.mod h1:zQWigSxVmsHEZow5qaLtPYxpcKMMQpa09ixqBxuCS6A= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= From 92595b04e469a80ff08729b4e2f4bc351511f578 Mon Sep 17 00:00:00 2001 From: Xueqin Cui Date: Tue, 3 Mar 2026 19:21:37 +1100 Subject: [PATCH 2/3] test failure --- vulnfeeds/cmd/ids/main.go | 34 +++++++++---------- vulnfeeds/cmd/ids/testdata/TEST-0000-def.json | 2 +- vulnfeeds/cmd/ids/testdata/TEST-2026-20.json | 2 +- vulnfeeds/cmd/ids/testdata/TEST-2026-21.json | 2 +- 4 files changed, 19 insertions(+), 21 deletions(-) diff --git a/vulnfeeds/cmd/ids/main.go b/vulnfeeds/cmd/ids/main.go index 33a9a04cbff..c0c54d85e0e 100644 --- a/vulnfeeds/cmd/ids/main.go +++ b/vulnfeeds/cmd/ids/main.go @@ -199,21 +199,21 @@ func readVulnWithFormat(r io.Reader, format fileFormat) (*osvschema.Vulnerabilit return nil, err } - var v osvschema.Vulnerability + var jsonBytes []byte switch format { case fileFormatJSON: - err = protojson.Unmarshal(data, &v) + jsonBytes = data case fileFormatYAML: - jsonBytes, err := yaml.YAMLToJSON(data) + jsonBytes, err = yaml.YAMLToJSON(data) if err != nil { return nil, err } - err = protojson.Unmarshal(jsonBytes, &v) default: return nil, fmt.Errorf("unknown file format: %v", format) } - if err != nil { + var v osvschema.Vulnerability + if err := protojson.Unmarshal(jsonBytes, &v); err != nil { return nil, err } @@ -226,26 +226,24 @@ func writeVulnWithFormat(v *osvschema.Vulnerability, w io.Writer, format fileFor Indent: " ", } + jsonBytes, err := marshaler.Marshal(v) + if err != nil { + return err + } + + var data []byte switch format { case fileFormatJSON: - data, err := marshaler.Marshal(v) - if err != nil { - return err - } - _, err = w.Write(data) - return err + data = jsonBytes case fileFormatYAML: - jsonBytes, err := marshaler.Marshal(v) - if err != nil { - return err - } - yamlBytes, err := yaml.JSONToYAML(jsonBytes) + data, err = yaml.JSONToYAML(jsonBytes) if err != nil { return err } - _, err = w.Write(yamlBytes) - return err default: return fmt.Errorf("unknown file format: %v", format) } + + _, err = w.Write(data) + return err } diff --git a/vulnfeeds/cmd/ids/testdata/TEST-0000-def.json b/vulnfeeds/cmd/ids/testdata/TEST-0000-def.json index 3fec72bd2b0..017bde72791 100644 --- a/vulnfeeds/cmd/ids/testdata/TEST-0000-def.json +++ b/vulnfeeds/cmd/ids/testdata/TEST-0000-def.json @@ -52,4 +52,4 @@ ] } ] -} +} \ No newline at end of file diff --git a/vulnfeeds/cmd/ids/testdata/TEST-2026-20.json b/vulnfeeds/cmd/ids/testdata/TEST-2026-20.json index 6da85fc064b..c9521bff81f 100644 --- a/vulnfeeds/cmd/ids/testdata/TEST-2026-20.json +++ b/vulnfeeds/cmd/ids/testdata/TEST-2026-20.json @@ -1,4 +1,4 @@ { "id": "TEST-2026-20", "modified": "2026-01-01T00:00:00Z" -} +} \ No newline at end of file diff --git a/vulnfeeds/cmd/ids/testdata/TEST-2026-21.json b/vulnfeeds/cmd/ids/testdata/TEST-2026-21.json index 765756d5323..8286ed0e1ac 100644 --- a/vulnfeeds/cmd/ids/testdata/TEST-2026-21.json +++ b/vulnfeeds/cmd/ids/testdata/TEST-2026-21.json @@ -52,4 +52,4 @@ ] } ] -} +} \ No newline at end of file From df630d24f017434338024cac689c84b3ad1dc58f Mon Sep 17 00:00:00 2001 From: Xueqin Cui Date: Tue, 3 Mar 2026 21:22:00 +1100 Subject: [PATCH 3/3] lints --- vulnfeeds/cmd/ids/main.go | 13 ++++++++++--- vulnfeeds/cmd/ids/main_test.go | 9 +++++---- vulnfeeds/cmd/ids/testdata/TEST-0000-def.json | 2 +- vulnfeeds/cmd/ids/testdata/TEST-2026-20.json | 2 +- vulnfeeds/cmd/ids/testdata/TEST-2026-21.json | 2 +- 5 files changed, 18 insertions(+), 10 deletions(-) diff --git a/vulnfeeds/cmd/ids/main.go b/vulnfeeds/cmd/ids/main.go index c0c54d85e0e..8ae44175178 100644 --- a/vulnfeeds/cmd/ids/main.go +++ b/vulnfeeds/cmd/ids/main.go @@ -112,8 +112,8 @@ func assignID(prefix, path string, format fileFormat, yearCounters map[int]int, // If the vulnerability has a published date, use the year from that. // Otherwise, just default to the current year. year := defaultYear - if vuln.Published != nil { - year = vuln.Published.AsTime().Year() + if vuln.GetPublished() != nil { + year = vuln.GetPublished().AsTime().Year() } // Allocate a new ID and write the new file. @@ -121,7 +121,7 @@ func assignID(prefix, path string, format fileFormat, yearCounters map[int]int, yearCounters[year] = id vuln.Id = fmt.Sprintf("%s-%d-%d", prefix, year, id) - newPath := filepath.Join(filepath.Dir(path), vuln.Id+formatToExtension[format]) + newPath := filepath.Join(filepath.Dir(path), vuln.GetId()+formatToExtension[format]) writef, err := os.Create(newPath) if err != nil { @@ -244,6 +244,13 @@ func writeVulnWithFormat(v *osvschema.Vulnerability, w io.Writer, format fileFor return fmt.Errorf("unknown file format: %v", format) } + // Ensure the output has a trailing newline to match the behavior of + // json.Encoder, which was previously used. + if len(data) > 0 && data[len(data)-1] != '\n' { + data = append(data, '\n') + } + _, err = w.Write(data) + return err } diff --git a/vulnfeeds/cmd/ids/main_test.go b/vulnfeeds/cmd/ids/main_test.go index 3507a05c188..c5d909ff2ac 100644 --- a/vulnfeeds/cmd/ids/main_test.go +++ b/vulnfeeds/cmd/ids/main_test.go @@ -44,7 +44,7 @@ func TestAssignIDs(t *testing.T) { if err != nil { t.Fatalf("failed to read existing ID: %v", err) } - err = os.WriteFile(filepath.Join(tmpDir, tt.existingName), existingData, 0644) + err = os.WriteFile(filepath.Join(tmpDir, tt.existingName), existingData, 0600) if err != nil { t.Fatalf("failed to copy existing ID: %v", err) } @@ -56,7 +56,7 @@ func TestAssignIDs(t *testing.T) { t.Fatalf("failed to read template %s: %v", templatePath, err) } destPath := filepath.Join(tmpDir, tt.templateName) - if err := os.WriteFile(destPath, templateData, 0644); err != nil { + if err := os.WriteFile(destPath, templateData, 0600); err != nil { t.Fatalf("failed to setup unassigned vuln: %v", err) } @@ -88,7 +88,9 @@ func TestAssignIDs(t *testing.T) { t.Fatalf("failed to read expected vuln: %v", err) } - if !bytes.Equal(gotData, wantData) { + // Trim space to be robust against minor formatting differences, + // for example trailing newlines. + if !bytes.Equal(bytes.TrimSpace(gotData), bytes.TrimSpace(wantData)) { t.Errorf("Data fidelity mismatch for %s:\nGot:\n%s\nWant:\n%s", tt.expectedID, string(gotData), string(wantData)) } @@ -99,4 +101,3 @@ func TestAssignIDs(t *testing.T) { }) } } - diff --git a/vulnfeeds/cmd/ids/testdata/TEST-0000-def.json b/vulnfeeds/cmd/ids/testdata/TEST-0000-def.json index 017bde72791..3fec72bd2b0 100644 --- a/vulnfeeds/cmd/ids/testdata/TEST-0000-def.json +++ b/vulnfeeds/cmd/ids/testdata/TEST-0000-def.json @@ -52,4 +52,4 @@ ] } ] -} \ No newline at end of file +} diff --git a/vulnfeeds/cmd/ids/testdata/TEST-2026-20.json b/vulnfeeds/cmd/ids/testdata/TEST-2026-20.json index c9521bff81f..6da85fc064b 100644 --- a/vulnfeeds/cmd/ids/testdata/TEST-2026-20.json +++ b/vulnfeeds/cmd/ids/testdata/TEST-2026-20.json @@ -1,4 +1,4 @@ { "id": "TEST-2026-20", "modified": "2026-01-01T00:00:00Z" -} \ No newline at end of file +} diff --git a/vulnfeeds/cmd/ids/testdata/TEST-2026-21.json b/vulnfeeds/cmd/ids/testdata/TEST-2026-21.json index 8286ed0e1ac..765756d5323 100644 --- a/vulnfeeds/cmd/ids/testdata/TEST-2026-21.json +++ b/vulnfeeds/cmd/ids/testdata/TEST-2026-21.json @@ -52,4 +52,4 @@ ] } ] -} \ No newline at end of file +}