4
0

auth.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package webhooks
  2. import (
  3. "crypto/hmac"
  4. "crypto/sha1"
  5. "crypto/sha256"
  6. "crypto/subtle"
  7. "encoding/hex"
  8. "net/http"
  9. "strings"
  10. "github.com/OliveTin/OliveTin/internal/config"
  11. log "github.com/sirupsen/logrus"
  12. )
  13. type AuthVerifier struct {
  14. config config.WebhookConfig
  15. }
  16. func NewAuthVerifier(cfg config.WebhookConfig) *AuthVerifier {
  17. return &AuthVerifier{config: cfg}
  18. }
  19. func (v *AuthVerifier) Verify(r *http.Request, payload []byte) bool {
  20. verifier := v.getVerifier()
  21. if verifier == nil {
  22. return v.handleUnknownAuthType()
  23. }
  24. return verifier(r, payload)
  25. }
  26. type authVerifierFunc func(*http.Request, []byte) bool
  27. // Webhooks match on headers, query parameters and the body, which are not
  28. // secrets, so a webhook with no authType is rejected. Operators who want an
  29. // open webhook must say so with authType: none.
  30. func (v *AuthVerifier) getVerifier() authVerifierFunc {
  31. if v.config.AuthType == "" {
  32. return rejectMissingAuthType
  33. }
  34. if v.config.AuthType == config.WebhookAuthTypeNone {
  35. return func(_ *http.Request, _ []byte) bool {
  36. return true
  37. }
  38. }
  39. verifierMap := v.buildVerifierMap()
  40. if verifier, ok := verifierMap[v.config.AuthType]; ok {
  41. return verifier
  42. }
  43. return nil
  44. }
  45. func (v *AuthVerifier) buildVerifierMap() map[string]authVerifierFunc {
  46. return map[string]authVerifierFunc{
  47. "hmac-sha256": v.verifyHMAC256,
  48. "hmac-sha1": v.verifyHMAC1,
  49. "bearer": func(r *http.Request, _ []byte) bool {
  50. return v.verifyBearer(r)
  51. },
  52. "basic": func(r *http.Request, _ []byte) bool {
  53. return v.verifyBasic(r)
  54. },
  55. }
  56. }
  57. func rejectMissingAuthType(_ *http.Request, _ []byte) bool {
  58. log.Warnf("Webhook has no authType, rejecting. Set an authType, or authType: none to allow unauthenticated requests")
  59. return false
  60. }
  61. func (v *AuthVerifier) handleUnknownAuthType() bool {
  62. log.WithFields(log.Fields{
  63. "authType": v.config.AuthType,
  64. }).Warnf("Unknown auth type, rejecting")
  65. return false
  66. }
  67. func (v *AuthVerifier) verifyHMAC256(r *http.Request, payload []byte) bool {
  68. if v.config.Secret == "" {
  69. log.Warnf("HMAC-SHA256 auth requires secret")
  70. return false
  71. }
  72. headerName := v.config.AuthHeader
  73. if headerName == "" {
  74. headerName = "X-Webhook-Signature"
  75. }
  76. signature := r.Header.Get(headerName)
  77. if signature == "" {
  78. log.Debugf("Missing signature header: %s", headerName)
  79. return false
  80. }
  81. expectedSig := strings.TrimPrefix(signature, "sha256=")
  82. mac := hmac.New(sha256.New, []byte(v.config.Secret))
  83. mac.Write(payload)
  84. computedSig := hex.EncodeToString(mac.Sum(nil))
  85. return hmac.Equal([]byte(expectedSig), []byte(computedSig))
  86. }
  87. func (v *AuthVerifier) verifyHMAC1(r *http.Request, payload []byte) bool {
  88. if v.config.Secret == "" {
  89. log.Warnf("HMAC-SHA1 auth requires secret")
  90. return false
  91. }
  92. headerName := v.config.AuthHeader
  93. if headerName == "" {
  94. headerName = "X-Webhook-Signature"
  95. }
  96. signature := r.Header.Get(headerName)
  97. if signature == "" {
  98. log.Debugf("Missing signature header: %s", headerName)
  99. return false
  100. }
  101. expectedSig := strings.TrimPrefix(signature, "sha1=")
  102. mac := hmac.New(sha1.New, []byte(v.config.Secret))
  103. mac.Write(payload)
  104. computedSig := hex.EncodeToString(mac.Sum(nil))
  105. return hmac.Equal([]byte(expectedSig), []byte(computedSig))
  106. }
  107. func (v *AuthVerifier) verifyBearer(r *http.Request) bool {
  108. if v.config.Secret == "" {
  109. log.Warnf("Bearer auth requires secret")
  110. return false
  111. }
  112. authHeader := r.Header.Get("Authorization")
  113. if !strings.HasPrefix(authHeader, "Bearer ") {
  114. log.Debugf("Missing or invalid Bearer token")
  115. return false
  116. }
  117. token := strings.TrimPrefix(authHeader, "Bearer ")
  118. tokenBytes := []byte(token)
  119. secretBytes := []byte(v.config.Secret)
  120. return len(tokenBytes) == len(secretBytes) && subtle.ConstantTimeCompare(tokenBytes, secretBytes) == 1
  121. }
  122. func (v *AuthVerifier) verifyBasic(r *http.Request) bool {
  123. if v.config.Secret == "" {
  124. log.Warnf("Basic auth requires secret")
  125. return false
  126. }
  127. username, password, ok := r.BasicAuth()
  128. if !ok {
  129. log.Debugf("Missing Basic auth header")
  130. return false
  131. }
  132. return v.verifyBasicCredentials(username, password)
  133. }
  134. func (v *AuthVerifier) verifyBasicCredentials(username, password string) bool {
  135. parts := strings.SplitN(v.config.Secret, ":", 2)
  136. if len(parts) == 2 {
  137. return v.verifyBasicWithUsername(username, password, parts[0], parts[1])
  138. }
  139. return v.verifyBasicPasswordOnly(password)
  140. }
  141. func (v *AuthVerifier) verifyBasicWithUsername(username, password, expectedUsername, expectedPassword string) bool {
  142. usernameMatch := subtle.ConstantTimeCompare([]byte(username), []byte(expectedUsername))
  143. passwordMatch := subtle.ConstantTimeCompare([]byte(password), []byte(expectedPassword))
  144. return usernameMatch == 1 && passwordMatch == 1
  145. }
  146. func (v *AuthVerifier) verifyBasicPasswordOnly(password string) bool {
  147. return subtle.ConstantTimeCompare([]byte(password), []byte(v.config.Secret)) == 1
  148. }