example.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package webhooks provides example consumer code for Tailscale
  5. // webhooks.
  6. package webhooks
  7. import (
  8. "crypto/hmac"
  9. "crypto/sha256"
  10. "crypto/subtle"
  11. "encoding/hex"
  12. "encoding/json"
  13. "errors"
  14. "fmt"
  15. "io"
  16. "log"
  17. "net/http"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. type event struct {
  23. Timestamp string `json:"timestamp"`
  24. Version int `json:"version"`
  25. Type string `json:"type"`
  26. Tailnet string `json:"tailnet"`
  27. Message string `json:"message"`
  28. Data map[string]string `json:"data"`
  29. }
  30. const (
  31. currentVersion = "v1"
  32. secret = "tskey-webhook-xxxxx" // sensitive, here just as an example
  33. )
  34. var (
  35. errNotSigned = errors.New("webhook has no signature")
  36. errInvalidHeader = errors.New("webhook has an invalid signature")
  37. )
  38. func main() {
  39. http.HandleFunc("/webhook", webhooksHandler)
  40. if err := http.ListenAndServe(":80", nil); err != nil {
  41. log.Fatal(err)
  42. }
  43. }
  44. func webhooksHandler(w http.ResponseWriter, req *http.Request) {
  45. defer req.Body.Close()
  46. events, err := verifyWebhookSignature(req, secret)
  47. if err != nil {
  48. log.Printf("error validating signature: %v\n", err)
  49. } else {
  50. log.Printf("events received %v\n", events)
  51. // Do something with your events. :)
  52. }
  53. // The handler should always report 2XX except in the case of
  54. // transient failures (e.g. database backend is down).
  55. // Otherwise your future events will be blocked by retries.
  56. }
  57. // verifyWebhookSignature checks the request's "Tailscale-Webhook-Signature"
  58. // header to verify that the events were signed by your webhook secret.
  59. // If verification fails, an error is reported.
  60. // If verification succeeds, the list of contained events is reported.
  61. func verifyWebhookSignature(req *http.Request, secret string) (events []event, err error) {
  62. defer req.Body.Close()
  63. // Grab the signature sent on the request header.
  64. timestamp, signatures, err := parseSignatureHeader(req.Header.Get("Tailscale-Webhook-Signature"))
  65. if err != nil {
  66. return nil, err
  67. }
  68. // Verify that the timestamp is recent.
  69. // Here, we use a threshold of 5 minutes.
  70. if timestamp.Before(time.Now().Add(-time.Minute * 5)) {
  71. return nil, fmt.Errorf("invalid header: timestamp older than 5 minutes")
  72. }
  73. // Form the expected signature.
  74. b, err := io.ReadAll(req.Body)
  75. if err != nil {
  76. return nil, err
  77. }
  78. mac := hmac.New(sha256.New, []byte(secret))
  79. mac.Write([]byte(fmt.Sprint(timestamp.Unix())))
  80. mac.Write([]byte("."))
  81. mac.Write(b)
  82. want := hex.EncodeToString(mac.Sum(nil))
  83. // Verify that the signatures match.
  84. var match bool
  85. for _, signature := range signatures[currentVersion] {
  86. if subtle.ConstantTimeCompare([]byte(signature), []byte(want)) == 1 {
  87. match = true
  88. break
  89. }
  90. }
  91. if !match {
  92. return nil, fmt.Errorf("signature does not match: want = %q, got = %q", want, signatures[currentVersion])
  93. }
  94. // If verified, return the events.
  95. if err := json.Unmarshal(b, &events); err != nil {
  96. return nil, err
  97. }
  98. return events, nil
  99. }
  100. // parseSignatureHeader splits header into its timestamp and included signatures.
  101. // The signatures are reported as a map of version (e.g. "v1") to a list of signatures
  102. // found with that version.
  103. func parseSignatureHeader(header string) (timestamp time.Time, signatures map[string][]string, err error) {
  104. if header == "" {
  105. return time.Time{}, nil, fmt.Errorf("request has no signature")
  106. }
  107. signatures = make(map[string][]string)
  108. pairs := strings.Split(header, ",")
  109. for _, pair := range pairs {
  110. parts := strings.Split(pair, "=")
  111. if len(parts) != 2 {
  112. return time.Time{}, nil, errNotSigned
  113. }
  114. switch parts[0] {
  115. case "t":
  116. tsint, err := strconv.ParseInt(parts[1], 10, 64)
  117. if err != nil {
  118. return time.Time{}, nil, errInvalidHeader
  119. }
  120. timestamp = time.Unix(tsint, 0)
  121. case currentVersion:
  122. signatures[parts[0]] = append(signatures[parts[0]], parts[1])
  123. default:
  124. // Ignore unknown parts of the header.
  125. continue
  126. }
  127. }
  128. if len(signatures) == 0 {
  129. return time.Time{}, nil, errNotSigned
  130. }
  131. return
  132. }