version_check_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright 2020 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // go-ethereum is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "io/ioutil"
  21. "path/filepath"
  22. "regexp"
  23. "strconv"
  24. "strings"
  25. "testing"
  26. )
  27. func TestVerification(t *testing.T) {
  28. // Signatures generated with `minisign`
  29. t.Run("minisig", func(t *testing.T) {
  30. // For this test, the pubkey is in testdata/minisign.pub
  31. // (the privkey is `minisign.sec`, if we want to expand this test. Password 'test' )
  32. pub := "RWQkliYstQBOKOdtClfgC3IypIPX6TAmoEi7beZ4gyR3wsaezvqOMWsp"
  33. testVerification(t, pub, "./testdata/vcheck/minisig-sigs/")
  34. })
  35. // Signatures generated with `signify-openbsd`
  36. t.Run("signify-openbsd", func(t *testing.T) {
  37. t.Skip("This currently fails, minisign expects 4 lines of data, signify provides only 2")
  38. // For this test, the pubkey is in testdata/signifykey.pub
  39. // (the privkey is `signifykey.sec`, if we want to expand this test. Password 'test' )
  40. pub := "RWSKLNhZb0KdATtRT7mZC/bybI3t3+Hv/O2i3ye04Dq9fnT9slpZ1a2/"
  41. testVerification(t, pub, "./testdata/vcheck/signify-sigs/")
  42. })
  43. }
  44. func testVerification(t *testing.T, pubkey, sigdir string) {
  45. // Data to verify
  46. data, err := ioutil.ReadFile("./testdata/vcheck/data.json")
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. // Signatures, with and without comments, both trusted and untrusted
  51. files, err := ioutil.ReadDir(sigdir)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. for _, f := range files {
  56. sig, err := ioutil.ReadFile(filepath.Join(sigdir, f.Name()))
  57. if err != nil {
  58. t.Fatal(err)
  59. }
  60. err = verifySignature([]string{pubkey}, data, sig)
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. }
  65. }
  66. func versionUint(v string) int {
  67. mustInt := func(s string) int {
  68. a, err := strconv.Atoi(s)
  69. if err != nil {
  70. panic(v)
  71. }
  72. return a
  73. }
  74. components := strings.Split(strings.TrimPrefix(v, "v"), ".")
  75. a := mustInt(components[0])
  76. b := mustInt(components[1])
  77. c := mustInt(components[2])
  78. return a*100*100 + b*100 + c
  79. }
  80. // TestMatching can be used to check that the regexps are correct
  81. func TestMatching(t *testing.T) {
  82. data, _ := ioutil.ReadFile("./testdata/vcheck/vulnerabilities.json")
  83. var vulns []vulnJson
  84. if err := json.Unmarshal(data, &vulns); err != nil {
  85. t.Fatal(err)
  86. }
  87. check := func(version string) {
  88. vFull := fmt.Sprintf("Geth/%v-unstable-15339cf1-20201204/linux-amd64/go1.15.4", version)
  89. for _, vuln := range vulns {
  90. r, err := regexp.Compile(vuln.Check)
  91. vulnIntro := versionUint(vuln.Introduced)
  92. vulnFixed := versionUint(vuln.Fixed)
  93. current := versionUint(version)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. if vuln.Name == "Denial of service due to Go CVE-2020-28362" {
  98. // this one is not tied to geth-versions
  99. continue
  100. }
  101. if vulnIntro <= current && vulnFixed > current {
  102. // Should be vulnerable
  103. if !r.MatchString(vFull) {
  104. t.Errorf("Should be vulnerable, version %v, intro: %v, fixed: %v %v %v",
  105. version, vuln.Introduced, vuln.Fixed, vuln.Name, vuln.Check)
  106. }
  107. } else {
  108. if r.MatchString(vFull) {
  109. t.Errorf("Should not be flagged vulnerable, version %v, intro: %v, fixed: %v %v %d %d %d",
  110. version, vuln.Introduced, vuln.Fixed, vuln.Name, vulnIntro, current, vulnFixed)
  111. }
  112. }
  113. }
  114. }
  115. for major := 1; major < 2; major++ {
  116. for minor := 0; minor < 30; minor++ {
  117. for patch := 0; patch < 30; patch++ {
  118. vShort := fmt.Sprintf("v%d.%d.%d", major, minor, patch)
  119. check(vShort)
  120. }
  121. }
  122. }
  123. }