Skip to content

Commit ce0605b

Browse files
author
Maceo Thompson
committed
internal/openvex: add hash for doc ID
updates golang/go#62486 Change-Id: I741ee275288b978becb46d5072ae22857152f2b6 Reviewed-on: https://go-review.googlesource.com/c/vuln/+/575860 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
1 parent 745db65 commit ce0605b

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

cmd/govulncheck/testdata/common/testfiles/binary-call/binary_vex.ct

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
$ govulncheck -format openvex -mode binary ${common_vuln_binary}
44
{
55
"@context": "https://openvex.dev/ns/v0.2.0",
6-
"@id": "govulncheckVEX",
6+
"@id": "govulncheck/vex:b2e8274f24820051d79285827c4fe6e1912c99143a4693804b9a5c366ec5fb8d",
77
"author": "Unknown Author",
88
"timestamp": "2024-01-01T00:00:00",
99
"version": 1,

cmd/govulncheck/testdata/common/testfiles/source-call/source_call_vex.ct

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
$ govulncheck -C ${moddir}/vuln -format openvex ./...
44
{
55
"@context": "https://openvex.dev/ns/v0.2.0",
6-
"@id": "govulncheckVEX",
6+
"@id": "govulncheck/vex:b2e8274f24820051d79285827c4fe6e1912c99143a4693804b9a5c366ec5fb8d",
77
"author": "Unknown Author",
88
"timestamp": "2024-01-01T00:00:00",
99
"version": 1,

internal/openvex/handler.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package openvex
66

77
import (
8+
"crypto/sha256"
89
"encoding/json"
910
"fmt"
1011
"io"
@@ -88,14 +89,16 @@ func (h *handler) Flush() error {
8889

8990
func toVex(h *handler) Document {
9091
doc := Document{
91-
ID: "govulncheckVEX", // TODO: create hash from document for ID
9292
Context: ContextURI,
9393
Author: DefaultAuthor,
9494
Timestamp: time.Now().UTC(),
9595
Version: 1,
9696
Tooling: Tooling,
9797
Statements: statements(h),
9898
}
99+
100+
id := hashVex(doc)
101+
doc.ID = "govulncheck/vex:" + id
99102
return doc
100103
}
101104

@@ -160,3 +163,21 @@ func statements(h *handler) []Statement {
160163
})
161164
return statements
162165
}
166+
167+
func hashVex(doc Document) string {
168+
// json.Marshal should never error here (because of the structure of Document).
169+
// If an error does occur, it won't be a jsonerror, but instead a panic
170+
d := Document{
171+
Context: doc.Context,
172+
ID: doc.ID,
173+
Author: doc.Author,
174+
Version: doc.Version,
175+
Tooling: doc.Tooling,
176+
Statements: doc.Statements,
177+
}
178+
out, err := json.Marshal(d)
179+
if err != nil {
180+
panic(err)
181+
}
182+
return fmt.Sprintf("%x", sha256.Sum256(out))
183+
}

0 commit comments

Comments
 (0)