Skip to content

Commit 5481ac2

Browse files
authored
Merge pull request #377 from vulncheck-oss/feature/semver-wrapper
Add SemVer constraint wrapper and add framework test files
2 parents e90bca5 + 655eb06 commit 5481ac2

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

framework.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import (
7171
"sync/atomic"
7272
"time"
7373

74+
"github.com/Masterminds/semver"
7475
"github.com/vulncheck-oss/go-exploit/c2"
7576
"github.com/vulncheck-oss/go-exploit/c2/channel"
7677
"github.com/vulncheck-oss/go-exploit/cli"
@@ -428,6 +429,27 @@ func StoreVersion(conf *config.Config, version string) {
428429
db.UpdateVerified(conf.Product, true, version, conf.Rhost, conf.Rport)
429430
}
430431

432+
// Compare a version to a semantic version constraint using the [Masterminds semver constraints](https://github.com/Masterminds/semver?tab=readme-ov-file#checking-version-constraints).
433+
// Provide a version string and a constraint and if the semver is within the constraint a boolean
434+
// response of whether the version is constrained or not will occur. Any errors from the constraint
435+
// or version will propagate through the framework errors and the value will be false.
436+
func CheckSemVer(version string, constraint string) bool {
437+
c, err := semver.NewConstraint(constraint)
438+
if err != nil {
439+
output.PrintfFrameworkError("Invalid constraint: %s", err.Error())
440+
441+
return false
442+
}
443+
v, err := semver.NewVersion(version)
444+
if err != nil {
445+
output.PrintfFrameworkError("Invalid version: %s", err.Error())
446+
447+
return false
448+
}
449+
450+
return c.Check(v)
451+
}
452+
431453
// modify godebug to re-enable old cipher suites that were removed in 1.22. This does have implications for our
432454
// client fingerprint, and we should consider how to improve/fix that in the future. We also should be respectful
433455
// of other disabling this feature, so we will check for it before re-enabling it.

framework_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package exploit_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/vulncheck-oss/go-exploit"
7+
)
8+
9+
func TestCheckSemVer_Full(t *testing.T) {
10+
if !exploit.CheckSemVer("1.0.0", "<= 1.0.0") {
11+
t.Error("Constraint should have passed")
12+
}
13+
if exploit.CheckSemVer("1.0.0", "> 1.0.0") {
14+
t.Error("Constraint should not have passed")
15+
}
16+
}
17+
18+
func TestCheckSemVer_BadVersion(t *testing.T) {
19+
if exploit.CheckSemVer("uwu", "<= 1.0.0") {
20+
t.Error("Version was invalid, should not have passed")
21+
}
22+
if exploit.CheckSemVer("1.0.0 ", "<= 1.0.0") {
23+
t.Error("Version was invalid, should not have passed")
24+
}
25+
}
26+
27+
func TestCheckSemVer_BadConstraint(t *testing.T) {
28+
if exploit.CheckSemVer("1.0.0", "<== 1.0.0") {
29+
t.Error("Constraint was invalid, should not have passed")
30+
}
31+
if exploit.CheckSemVer("1.0.0", "xp") {
32+
t.Error("Constraint was invalid, should not have passed")
33+
}
34+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
)
1313

1414
require (
15+
github.com/Masterminds/semver v1.5.0 // indirect
1516
github.com/dustin/go-humanize v1.0.1 // indirect
1617
github.com/google/uuid v1.6.0 // indirect
1718
github.com/mattn/go-isatty v0.0.20 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
2+
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
13
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
24
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
35
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=

0 commit comments

Comments
 (0)