Skip to content

Commit 308feef

Browse files
authored
Merge pull request #50 from puerco/context-parse
Light parse VEX data to find Context locator
2 parents 55832c2 + 6bb2a42 commit 308feef

File tree

2 files changed

+45
-20
lines changed

2 files changed

+45
-20
lines changed

pkg/vex/vex.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"fmt"
1313
"io"
1414
"os"
15-
"regexp"
1615
"sort"
1716
"strconv"
1817
"strings"
@@ -58,11 +57,6 @@ const (
5857
// documents and nodes. It is set to the OpenVEX public namespace by default.
5958
var DefaultNamespace = PublicNamespace
6059

61-
var (
62-
contextRegExpPattern = fmt.Sprintf(`"@context":\s+"(%s\S*)"`, Context)
63-
contextRegExp *regexp.Regexp
64-
)
65-
6660
// The VEX type represents a VEX document and all of its contained information.
6761
type VEX struct {
6862
Metadata
@@ -265,28 +259,38 @@ func SortDocuments(docs []*VEX) []*VEX {
265259
return docs
266260
}
267261

262+
// parseContext light parses a JSON document to look for the OpenVEX context locator
263+
func parseContext(rawDoc []byte) (string, error) {
264+
pd := struct {
265+
Context string `json:"@context"`
266+
}{}
267+
268+
if err := json.Unmarshal(rawDoc, &pd); err != nil {
269+
return "", fmt.Errorf("parsing context from json data: %w", err)
270+
}
271+
272+
if strings.HasPrefix(pd.Context, Context) {
273+
return pd.Context, nil
274+
}
275+
return "", nil
276+
}
277+
268278
// Open tries to autodetect the vex format and open it
269279
func Open(path string) (*VEX, error) {
270280
data, err := os.ReadFile(path)
271281
if err != nil {
272282
return nil, fmt.Errorf("opening VEX file: %w", err)
273283
}
274284

275-
if bytes.Contains(data, []byte(ContextLocator())) {
276-
logrus.Info("opening current vex")
277-
return Parse(data)
278-
} else if bytes.Contains(data, []byte(Context)) {
279-
logrus.Info("Opening older openvex")
280-
if contextRegExp == nil {
281-
contextRegExp = regexp.MustCompile(contextRegExpPattern)
282-
}
283-
284-
res := contextRegExp.FindSubmatch(data)
285-
if len(res) == 0 {
286-
return nil, fmt.Errorf("unable to parse OpenVEX version in document context")
287-
}
285+
documentContextLocator, err := parseContext(data)
286+
if err != nil {
287+
return nil, err
288+
}
288289

289-
version := strings.TrimPrefix(string(res[1]), Context)
290+
if documentContextLocator == ContextLocator() {
291+
return Parse(data)
292+
} else if documentContextLocator != "" {
293+
version := strings.TrimPrefix(documentContextLocator, Context)
290294
version = strings.TrimPrefix(version, "/")
291295

292296
// If version is nil, then we assume v0.0.1

pkg/vex/vex_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,24 @@ func TestDocumentMatches(t *testing.T) {
413413
require.Equal(t, tc.numMatches, len(matches), fmt.Sprintf("failed: %s", testCase))
414414
}
415415
}
416+
417+
func TestParseContext(t *testing.T) {
418+
for tCase, tc := range map[string]struct {
419+
docData string
420+
expected string
421+
shouldErr bool
422+
}{
423+
"Normal": {`{"@context": "https://openvex.dev/ns"}`, "https://openvex.dev/ns", false},
424+
"Other JSON": {`{"document": { "category": "csaf_vex" } }`, "", false},
425+
"Invalid JSON": {`@context": "https://openvex.dev/ns`, "", true},
426+
"Other json-ld": {`{"@context": "https://spdx.dev/"}`, "", false},
427+
} {
428+
res, err := parseContext([]byte(tc.docData))
429+
if tc.shouldErr {
430+
require.Error(t, err, tCase)
431+
continue
432+
}
433+
require.NoError(t, err, tCase)
434+
require.Equal(t, res, tc.expected, tCase)
435+
}
436+
}

0 commit comments

Comments
 (0)