Skip to content

Commit

Permalink
cvss: add additional check for malformed input
Browse files Browse the repository at this point in the history
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed May 10, 2024
1 parent 068edb6 commit 34c3319
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
8 changes: 8 additions & 0 deletions toolkit/types/cvss/cvss_v2.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cvss

import (
"bytes"
"encoding"
"fmt"
"strings"
Expand Down Expand Up @@ -36,6 +37,13 @@ func (v *V2) UnmarshalText(text []byte) error {
return fmt.Errorf("cvss v2: %w: missing metric: %q", ErrMalformedVector, V3Metric(m).String())
}
}
chk, err := v.MarshalText()
if err != nil {
return fmt.Errorf("cvss v2: %w", err)

Check warning on line 42 in toolkit/types/cvss/cvss_v2.go

View check run for this annotation

Codecov / codecov/patch

toolkit/types/cvss/cvss_v2.go#L42

Added line #L42 was not covered by tests
}
if !bytes.Equal(chk, text) {
return fmt.Errorf("cvss v2: malformed input")
}
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions toolkit/types/cvss/cvss_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func TestV2(t *testing.T) {
{Vector: "AV:L/AC:H/Au:N/C:C/I:C/A:C/E:F/RL:OF/RC:C/CDP:H/TD:H/CR:M/IR:M/AR:H", Error: false},
{Vector: "CVSS:2.0/AV:N/AC:L/Au:N/C:N/I:N/A:C", Error: true},
{Vector: "AV:N/AC:L/Au:N/C:N/I:N", Error: true},
{Vector: "AV:A/AC:L/Au:N/C:C/I:C/A:C/CDP:H/TD:H/CR:H", Error: true},
{Vector: "AV:A/AC:L/Au:N/C:C/I:C/A:C/E:F", Error: true},
}
Error[V2, V2Metric, *V2](t, tcs)
})
Expand Down
8 changes: 8 additions & 0 deletions toolkit/types/cvss/cvss_v3.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cvss

import (
"bytes"
"encoding"
"fmt"
"strings"
Expand Down Expand Up @@ -53,6 +54,13 @@ func (v *V3) UnmarshalText(text []byte) error {
return fmt.Errorf("cvss v3: %w: missing metric: %q", ErrMalformedVector, V3Metric(m).String())
}
}
chk, err := v.MarshalText()
if err != nil {
return fmt.Errorf("cvss v3: %w", err)

Check warning on line 59 in toolkit/types/cvss/cvss_v3.go

View check run for this annotation

Codecov / codecov/patch

toolkit/types/cvss/cvss_v3.go#L59

Added line #L59 was not covered by tests
}
if !bytes.Equal(chk, text) {
return fmt.Errorf("cvss v3: malformed input")

Check warning on line 62 in toolkit/types/cvss/cvss_v3.go

View check run for this annotation

Codecov / codecov/patch

toolkit/types/cvss/cvss_v3.go#L62

Added line #L62 was not covered by tests
}
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions toolkit/types/cvss/cvss_v4.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cvss

import (
"bytes"
"encoding"
"fmt"
"strings"
Expand Down Expand Up @@ -32,6 +33,13 @@ func (v *V4) UnmarshalText(text []byte) error {
return fmt.Errorf("cvss v4: %w: missing metric: %q", ErrMalformedVector, V4Metric(m).String())
}
}
chk, err := v.MarshalText()
if err != nil {
return fmt.Errorf("cvss v4: %w", err)

Check warning on line 38 in toolkit/types/cvss/cvss_v4.go

View check run for this annotation

Codecov / codecov/patch

toolkit/types/cvss/cvss_v4.go#L38

Added line #L38 was not covered by tests
}
if !bytes.Equal(chk, text) {
return fmt.Errorf("cvss v4: malformed input")
}
return nil
}

Expand Down
1 change: 1 addition & 0 deletions toolkit/types/cvss/cvss_v4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestV4(t *testing.T) {
{Vector: "CVSS:/AV:A/AC:L/AT:N/PR:H/UI:A/VC:L/VI:L/VA:N/SC:N/SA:N/S:X", Error: true},
{Vector: "CVSS:4.0/AV:N/AC:L/AT:N/PR:H/UI:N/VC:L/VI:L/VA:N/SC:N/SI:N/E:X", Error: true},
{Vector: "CVSS:4.0/CVSS:4.0/AV:N/AC:L/AT:N/PR:H/UI:N/VC:L/VI:L/VA:N/SC:N/SI:N/SA:N", Error: true},
{Vector: "CVSS:4.0/AV:A/AC:L/AT:N/PR:H/UI:A/VC:LN/VI:L/VA:N/SC:N/SI:N/SA:N", Error: true},
}
Error[V4, V4Metric, *V4](t, tcs)
})
Expand Down

0 comments on commit 34c3319

Please sign in to comment.