semgrep.yml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Usage: semgrep scan -f semgrep.yml
  2. rules:
  3. - id: rule-0
  4. message: "http requests made without *fiber.Ctx"
  5. languages: [go]
  6. severity: WARNING
  7. patterns:
  8. - pattern-either:
  9. - pattern: |
  10. http.UnwrapWebAPIRequest(...)
  11. - pattern: |
  12. http.WebAPIRequest(...)
  13. - pattern-not-inside: |
  14. func $FUNC(c *http.Request, ...) $RET {
  15. ...
  16. }
  17. # note: the below two rules autofix have slight problems. where `http` is sometimes "net/http". need minor manual tweaking after --autofix.
  18. - id: rule-1-0
  19. message: "find http requests made to Pixiv"
  20. languages: [go]
  21. severity: INFO
  22. patterns:
  23. - pattern: |
  24. http.UnwrapWebAPIRequest($A, $B)
  25. fix: |
  26. http.UnwrapWebAPIRequest(c.Context(), $A, $B)
  27. - id: rule-1-1
  28. message: "find http requests made to Pixiv"
  29. languages: [go]
  30. severity: INFO
  31. patterns:
  32. - pattern: |
  33. http.WebAPIRequest($A, $B)
  34. fix: |
  35. http.WebAPIRequest(c.Context(), $A, $B)
  36. - id: rule-2
  37. message: "gjson.Get without gjson.Valid"
  38. languages: [go]
  39. severity: ERROR
  40. patterns:
  41. # - pattern-inside: |
  42. # func $FUNC(...) $RET {
  43. # ...
  44. # }
  45. - pattern: |
  46. gjson.Get($X, ...)
  47. - pattern-not-inside: |
  48. if !gjson.Valid($X) {
  49. $...DISCARD
  50. }
  51. ...
  52. - id: rule-3
  53. message: "http request without context"
  54. languages: [go]
  55. severity: WARNING
  56. # severity: INVENTORY
  57. patterns:
  58. - pattern: |
  59. $REQ, $ERR := http.NewRequestWithContext($...ARGV)
  60. if $ERR != nil {
  61. $...I
  62. }
  63. $REQ = $REQ.WithContext($CTX)
  64. fix: |
  65. $REQ, err := http.NewRequestWithContext($...ARGV)
  66. if err != nil {
  67. $...I
  68. }
  69. - id: rule-3a
  70. message: "http request without context"
  71. languages: [go]
  72. severity: WARNING
  73. # severity: INVENTORY
  74. patterns:
  75. - pattern: |
  76. http.NewRequest($...ARGV)
  77. fix: |
  78. http.NewRequestWithContext(r.Context(), $...ARGV)
  79. - id: rule-4
  80. message: "fmt.Sprint on string"
  81. languages: [go]
  82. severity: WARNING
  83. pattern: |
  84. fmt.Sprint(($S : string))
  85. - id: rule-5
  86. message: "unhandled error"
  87. languages: [go]
  88. severity: WARNING
  89. pattern: |
  90. (_ : error) = ...
  91. - id: rule-6
  92. message: "raw UserArtCategory string"
  93. languages: [go]
  94. severity: WARNING
  95. patterns:
  96. - pattern: |
  97. ($A : UserArtCategory) == $B
  98. - pattern-not: |
  99. ($A : UserArtCategory) == ($B : UserArtCategory)
  100. - id: rule-7
  101. message: "c.Render"
  102. languages: [go]
  103. severity: INFO
  104. pattern: |
  105. c.Render("$NAME", fiber.Map{$...INSIDE})
  106. paths:
  107. exclude:
  108. - "render_types.go"
  109. fix: |
  110. Render(w, r, Data_$NAME{$...INSIDE})
  111. - id: rule-8
  112. message: "c.Render"
  113. languages: [go]
  114. severity: INFO
  115. patterns:
  116. - pattern-inside: |
  117. c.Render(...)
  118. - pattern: |
  119. fiber.Map { $...BEFORE, "$A": $B, $...AFTER }
  120. fix: |
  121. fiber.Map { $...BEFORE, $A: $B, $...AFTER }
  122. paths:
  123. exclude:
  124. - "render.go"
  125. - id: rule-9
  126. message: "still using *fiber.Ctx"
  127. languages: [go]
  128. severity: INVENTORY
  129. patterns:
  130. - pattern: |
  131. func $NAME(c *fiber.Ctx) error {
  132. $...I
  133. }
  134. fix: |
  135. func $NAME(w http.ResponseWriter, r *http.Request) error {
  136. $...I
  137. }
  138. - id: rule-10
  139. message: "masquerading CompatRequest"
  140. languages: [go]
  141. severity: INFO
  142. patterns:
  143. - pattern: |
  144. $FUNC((r : *http.Request))
  145. # fix: |
  146. # func $NAME(w http.ResponseWriter, r *http.Request) error {
  147. # $...I
  148. # }
  149. - id: rule-11
  150. message: "Use StatusSeeOther or StatusPermanentRedirect"
  151. languages: [go]
  152. severity: WARNING
  153. patterns:
  154. - pattern: |
  155. StatusFound
  156. - id: rule-12
  157. message: "response body not closed"
  158. comment: "Needed to reuse connections"
  159. languages: [go]
  160. severity: ERROR
  161. patterns:
  162. - pattern: |
  163. ($RESP : *http.Response), $ERR := ...
  164. - pattern-not-inside: |
  165. ...
  166. defer $RESP.Body.Close()