version_check_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "os"
  21. "path/filepath"
  22. "regexp"
  23. "strconv"
  24. "strings"
  25. "testing"
  26. "github.com/jedisct1/go-minisign"
  27. )
  28. func TestVerification(t *testing.T) {
  29. // Signatures generated with `minisign`
  30. t.Run("minisig", func(t *testing.T) {
  31. // For this test, the pubkey is in testdata/minisign.pub
  32. // (the privkey is `minisign.sec`, if we want to expand this test. Password 'test' )
  33. pub := "RWQkliYstQBOKOdtClfgC3IypIPX6TAmoEi7beZ4gyR3wsaezvqOMWsp"
  34. testVerification(t, pub, "./testdata/vcheck/minisig-sigs/")
  35. })
  36. // Signatures generated with `signify-openbsd`
  37. t.Run("signify-openbsd", func(t *testing.T) {
  38. t.Skip("This currently fails, minisign expects 4 lines of data, signify provides only 2")
  39. // For this test, the pubkey is in testdata/signifykey.pub
  40. // (the privkey is `signifykey.sec`, if we want to expand this test. Password 'test' )
  41. pub := "RWSKLNhZb0KdATtRT7mZC/bybI3t3+Hv/O2i3ye04Dq9fnT9slpZ1a2/"
  42. testVerification(t, pub, "./testdata/vcheck/signify-sigs/")
  43. })
  44. }
  45. func testVerification(t *testing.T, pubkey, sigdir string) {
  46. // Data to verify
  47. data, err := os.ReadFile("./testdata/vcheck/data.json")
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. dataString := strings.ReplaceAll(string(data), "\r\n", "\n")
  52. data = []byte(dataString)
  53. // Signatures, with and without comments, both trusted and untrusted
  54. files, err := os.ReadDir(sigdir)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. for _, f := range files {
  59. sig, err := os.ReadFile(filepath.Join(sigdir, f.Name()))
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. sigdataString := string(sig)
  64. sigdataString = strings.ReplaceAll(sigdataString, "\r\n", "\n")
  65. err = verifySignature([]string{pubkey}, data, []byte(sigdataString))
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. }
  70. }
  71. func versionUint(v string) int {
  72. mustInt := func(s string) int {
  73. a, err := strconv.Atoi(s)
  74. if err != nil {
  75. panic(v)
  76. }
  77. return a
  78. }
  79. components := strings.Split(strings.TrimPrefix(v, "v"), ".")
  80. a := mustInt(components[0])
  81. b := mustInt(components[1])
  82. c := mustInt(components[2])
  83. return a*100*100 + b*100 + c
  84. }
  85. // TestMatching can be used to check that the regexps are correct
  86. func TestMatching(t *testing.T) {
  87. data, _ := os.ReadFile("./testdata/vcheck/vulnerabilities.json")
  88. var vulns []vulnJson
  89. if err := json.Unmarshal(data, &vulns); err != nil {
  90. t.Fatal(err)
  91. }
  92. check := func(version string) {
  93. vFull := fmt.Sprintf("Geth/%v-unstable-15339cf1-20201204/linux-amd64/go1.15.4", version)
  94. for _, vuln := range vulns {
  95. r, err := regexp.Compile(vuln.Check)
  96. vulnIntro := versionUint(vuln.Introduced)
  97. vulnFixed := versionUint(vuln.Fixed)
  98. current := versionUint(version)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. if vuln.Name == "Denial of service due to Go CVE-2020-28362" {
  103. // this one is not tied to geth-versions
  104. continue
  105. }
  106. if vulnIntro <= current && vulnFixed > current {
  107. // Should be vulnerable
  108. if !r.MatchString(vFull) {
  109. t.Errorf("Should be vulnerable, version %v, intro: %v, fixed: %v %v %v",
  110. version, vuln.Introduced, vuln.Fixed, vuln.Name, vuln.Check)
  111. }
  112. } else {
  113. if r.MatchString(vFull) {
  114. t.Errorf("Should not be flagged vulnerable, version %v, intro: %v, fixed: %v %v %d %d %d",
  115. version, vuln.Introduced, vuln.Fixed, vuln.Name, vulnIntro, current, vulnFixed)
  116. }
  117. }
  118. }
  119. }
  120. for major := 1; major < 2; major++ {
  121. for minor := 0; minor < 30; minor++ {
  122. for patch := 0; patch < 30; patch++ {
  123. vShort := fmt.Sprintf("v%d.%d.%d", major, minor, patch)
  124. check(vShort)
  125. }
  126. }
  127. }
  128. }
  129. func TestGethPubKeysParseable(t *testing.T) {
  130. for _, pubkey := range gethPubKeys {
  131. _, err := minisign.NewPublicKey(pubkey)
  132. if err != nil {
  133. t.Errorf("Should be parseable")
  134. }
  135. }
  136. }
  137. func TestKeyID(t *testing.T) {
  138. type args struct {
  139. id [8]byte
  140. }
  141. tests := []struct {
  142. name string
  143. args args
  144. want string
  145. }{
  146. {"@holiman key", args{id: extractKeyId(gethPubKeys[0])}, "FB1D084D39BAEC24"},
  147. {"second key", args{id: extractKeyId(gethPubKeys[1])}, "138B1CA303E51687"},
  148. {"third key", args{id: extractKeyId(gethPubKeys[2])}, "FD9813B2D2098484"},
  149. }
  150. for _, tt := range tests {
  151. t.Run(tt.name, func(t *testing.T) {
  152. if got := keyID(tt.args.id); got != tt.want {
  153. t.Errorf("keyID() = %v, want %v", got, tt.want)
  154. }
  155. })
  156. }
  157. }
  158. func extractKeyId(pubkey string) [8]byte {
  159. p, _ := minisign.NewPublicKey(pubkey)
  160. return p.KeyId
  161. }