curl.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package rules
  2. import (
  3. "fmt"
  4. "github.com/zricethezav/gitleaks/v8/cmd/generate/config/utils"
  5. "regexp"
  6. "github.com/zricethezav/gitleaks/v8/config"
  7. )
  8. // https://curl.se/docs/manpage.html#-u
  9. func CurlBasicAuth() *config.Rule {
  10. r := config.Rule{
  11. RuleID: "curl-auth-user",
  12. // TODO: Description: "",
  13. Regex: regexp.MustCompile(`\bcurl\b(?:.*|.*(?:[\r\n]{1,2}.*){1,5})[ \t\n\r](?:-u|--user)[ =](?:("[^:"]{3,}:[^"]{3,}")|('[^:']{3,}:[^']{3,}')|((?:"[^"]{3,}"|'[^']{3,}'|[\w$@.-]+):(?:"[^"]{3,}"|'[^']{3,}'|[\w$@.-]+))|)(?:\s|\z)`),
  14. Keywords: []string{"curl"},
  15. Allowlists: []config.Allowlist{
  16. {
  17. Regexes: []*regexp.Regexp{
  18. regexp.MustCompile(`[^:]+:(changeme|pass(word)?|pwd|\*+|x+)`), // common placeholder passwords
  19. regexp.MustCompile(`<[^>]+>:<[^>]+>|<[^:]+:[^>]+>`), // <placeholder>
  20. regexp.MustCompile(`[^:]+:\[[^]]+]`), // [placeholder]
  21. regexp.MustCompile(`(?i)[^:]+:\$(\d|[a-z]\w+|(\{\d|[a-z]\w+}))`), // $1 or $VARIABLE
  22. },
  23. },
  24. },
  25. }
  26. // validate
  27. tps := []string{
  28. // short
  29. `curl --cacert ca.crt -u elastic:P@ssw0rd$1 https://localhost:9200`, // same lines, no quotes
  30. `sh-5.0$ curl -k -X POST https://infinispan:11222/rest/v2/caches/default/hello \
  31. -H 'Content-type: text/plain' \
  32. -d 'world' \
  33. -u developer:yqDVtkqPECriaLRi`, // different line
  34. // long
  35. `curl --user roger23@gmail.com:pQ9wTxu4Fg https://www.dropbox.com/cli_link?host_id=abcdefg -v`, // same line, no quotes
  36. `curl -s --user 'api:d2LkV78zLx!t' \
  37. https://api.mailgun.net/v2/sandbox91d3515882ecfaa1c65be642.mailgun.org/messages`, // same line, single quotes
  38. `curl -s -v --user "j.smith:dB2yF6@qL9vZm1P#4J" "https://api.contoso.org/user/me"`, // same line, double quotes
  39. `curl -X POST --user "{acd3c08b-74e8-4f44-a2d0-80694le24f46}":"{ZqL5kVrX1n8tA2}" --header "Accept: application/json" --data "{\"text\":\"Hello, world\",\"source\":\"en\",\"target\":\"es\"}" https://gateway.watsonplatform.net/language-translator/api`,
  40. `curl --user kevin:'pRf7vG2h1L8nQkW9' -iX PATCH -H "Content-Type: application/json" -d`, // same line, mixed quoting
  41. `$ curl https://api.dropbox.com/oauth2/token \
  42. --user c28wlsosanujy2z:qgsnai0xokrw4j1 --data grant_type=authorization_code`, // different line
  43. // TODO
  44. //` curl -s --insecure --url "imaps://whatever.imap.server" --user\
  45. //"myuserid:mypassword" --request "STATUS INBOX (UNSEEN)"`,
  46. }
  47. fps := []string{
  48. // short
  49. ` curl -sL --user "$1:$2" "$3" > "$4"`, // environment variable
  50. `curl -u <user:password> https://test.com/endpoint`, // placeholder
  51. `curl --user neo4j:[PASSWORD] http://[IP]:7474/db/data/`, // placeholder
  52. `curl -u "myusername" http://localhost:15130/api/check_user/`, // no password
  53. // long
  54. ` curl -sL --user "$GITHUB_USERNAME:$GITHUB_PASSWORD" "$GITHUB_URL" > "$TESTS_PATH"`, // environment variable
  55. `curl http://127.0.0.1:5000/file --user user:pass --digest # digest auth`, // placeholder
  56. ` curl -X GET --insecure --user "username:password" \`, // placeholder
  57. `curl --silent --insecure --user ${f5user}:${f5pass} \`, // placeholder
  58. `curl --insecure --ssl-reqd "smtps://smtp.gmail.com" --mail-from "src@gmail.com" --mail-rcpt "dst@gmail.com" --user "src@gmail.com" --upload-file out.txt`, // no password
  59. // different command
  60. `#HTTP command line test
  61. curl -X POST -H "Content-Type: application/json" -d '{"id":12345,"geo":{"latitude":28.50,"longitude":-81.14}}' http://<ip>:8080/serve
  62. #UDP command line test
  63. echo -n '{"type":"serve","channel":"/","data":{"site_id":8,"post_id":12345,"geo":{"lat":28.50,"long":-81.14}}}' >/dev/udp/127.0.0.1/41234
  64. #UDP Listener (for confirmation)
  65. nc -u -l 41234`,
  66. }
  67. return utils.Validate(r, tps, fps)
  68. }
  69. // https://curl.se/docs/manpage.html#-H
  70. func CurlHeaderAuth() *config.Rule {
  71. // language=regexp
  72. authPat := `(?i)(?:Authorization:[ \t]?(?:Basic[ \t]([a-z0-9+/]{8,}={0,3})|(?:Bearer|Token)[ \t]([\w=@.+/-]{8,})|([\w=.+/-]{8,}))|(?:ApiKey|Token|X-API-KEY):[ \t]?([\w=@.+/-]{8,}))`
  73. r := config.Rule{
  74. RuleID: "curl-auth-header",
  75. // TODO: Description: "",
  76. Regex: regexp.MustCompile(
  77. fmt.Sprintf(`\bcurl\b(?:.*?|.*?(?:[\r\n]{1,2}.*?){1,5})[ \t\n\r](?:-H|--header)[ =](?:"%s"|'%s')(?:\B|\s|\z)`, authPat, authPat)),
  78. Entropy: 2.75,
  79. Keywords: []string{"curl"},
  80. //Allowlists: []config.Allowlist{
  81. // {
  82. // Regexes: []*regexp.Regexp{},
  83. // },
  84. //},
  85. }
  86. tps := []string{
  87. // Short flag.
  88. `curl -H 'Authorization: Basic YnJvd3Nlcjo=' \`, // same line, single quotes
  89. // TODO: Handle short flags combined.
  90. //`TOKEN=$(curl -sH "Authorization: Basic $BASIC_TOKEN" "https://$REGISTRY/oauth2/token?service=$REGISTRY&scope=repository:$REPO:pull" | jq -r .access_token)`,
  91. // Long flag.
  92. `curl -k -X POST --header "Authorization: Basic djJlNEpYa0NJUHZ5a2FWT0VRXzRqZmZUdDkwYTp2emNBZGFzZWpmlWZiUDc2VUJjNDNNVDExclVh" "https://api-qa.example.com:8243/token" -d "grant_type=client_credentials"`, // same line, double quotes
  93. // Basic auth.
  94. ` curl -X POST -H "Content-Type: application/json" \
  95. -H "Authorization: Basic MzUzYjMwMmM0NDU3NGY1NjUwNDU2ODdlNTM0ZTdkNmE6Mjg2OTI0Njk3ZTYxNWE2NzJhNjQ2YTQ5MzU0NTY0NmM=" \
  96. -d '{"user":{"emailAddress":"test@example.com"}, "password":"password"}' \
  97. 'http://localhost:8080/oauth2-provider/v1.0/users'`, // different line, double quotes
  98. `#curl -X POST \
  99. # https://api.mailgun.net/v3/sandbox7dbcabccd4314c123e8b23599d35f5b6.mailgun.org/messages \
  100. # -H 'Authorization: Basic YXBpOmtleS1hN2MzNDJ3MzNhNWQxLTU2M2U3MjlwLTZhYjI3YzYzNzM0Ng==' \
  101. # -F from='Excited User <mailgun@sandbox7dbc123bccd4314c0aae8b23599d35f5b6.mailgun.org>' \
  102. # -F to='joe@example.com' \
  103. # -F subject='Hello' \
  104. # -F text='Testing some Mailgun awesomness!'`, // different line, single quotes
  105. // Bearer auth
  106. `# curl -X GET "http://localhost:3000/api/cron/status" -H "Authorization: Bearer cfcabd11c7ed9a41b1a3e063c32d5114"`, // same line, double quotes
  107. `curl -X PUT -H 'Authorization: Bearer jC+6TUUjCNHcVtAXpcqBCgxnA8r+qD6MatnYaf/+289y7HWpK0BWPyLHv/K4DMN32fufwmeVVjlo8zjgBh8kx3GfS6IqO70w1DVMSCTwX7fhEpiXaxzv0mhSMHDX9Kw63Q6DkavUWUV+MDNhCF5wGQrcdQNncVRF3YkuDHDT/xw2YWyZ/DX8k+gAYiC8gcD8Ueg0ljBVS1IDwPjuGoFPESJVxYr0MDPF2D8Pn2S5rq692U4D9ZLuluS46VA4DK6ig5P7QM5XVXi4V7vXM8qpN/zqneyz+w4PUh6NIX7QG6JczMhYd9maWRWVat5jDdyII63P6sNAy9QZjw+ClW211Q==' -d 'user={"account":"user@domain.com", "roles":["user"]}' http://127.0.0.1:8443/desks/1/occupy`, // same line, single quotes
  108. `curl https://api.openai.com/v1/chat/completions \
  109. -H "Content-Type: application/json" \
  110. -H "Authorization: Bearer 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" \`, // different line, double quotes
  111. `curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" \
  112. -H "Authorization: Bearer _FXNljbSRYMWx3TWrd7lgKhLtVZX6iskC8Wcbb4b" \
  113. -H "Content-Type:application/json"`,
  114. // Token auth
  115. `curl -H "Authorization: Token 22cb987851bc5659229114c62e60c79abd0d2c08" --request PUT https://appsecclass.report/api/use/635`, // token
  116. // Nothing
  117. `curl -L -H "Authorization:eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb25maWRlbmNlIjowLjh9.kvRPfjAhLhtRTczoRgctVGp7KY1QVH3UBZM-gM0x8ec" service1.local -> correct jwt `, // no prefix
  118. // Non-authorization headers.
  119. `curl -XPOST http://localhost:8080/api/tasks -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -H "Token: 3fea6af1349166ea" -d "content=hello-curl"`, // token
  120. `curl -X GET \
  121. -H "apikey: c4ed6c21-9dd5-4a05-8e3f-c56d1151cce8" \
  122. -H "Accept: application/json" \`, // API Key placeholder
  123. }
  124. fps := []string{
  125. // Placeholders
  126. `curl https://example.com/micropub -d h=entry -d "content=Hello World" -H "Authorization: Bearer XXXXXXXXXXXX"`,
  127. `curl -X POST https://accounts.spotify.com/api/token -d grant_type=client_credentials --header "Authorization: Basic ..."`,
  128. `curl \
  129. -H "Authorization: Bearer <Openverse API token>" \
  130. "https://api.openverse.org/v1/audio/?q=test"`,
  131. `curl -v -v -v -X POST https://domain/api/v1/authentication/sso/login-url/ \
  132. -H 'Content-Type: application/json' \
  133. -H "Authorization: Token **********" \
  134. -d '{"username": "test", "next": "/luna/"}'`,
  135. // Variables
  136. `curl -XPOST http://localhost:8080/api/token -H "Authorization: basic {base64(email:password[\n])}" => token`, // same line, invalid base64
  137. `curl -X GET \
  138. -H "apikey: $API_KEY" \
  139. -H "Accept: $FORMAT" \
  140. "$API_URL/rest/v1/stats_derniere_labellisation"`, // API Key placeholder
  141. `$ curl -X POST "http://localhost:8000/v1/chat/completions" \
  142. -H "Content-Type: application/json" \
  143. -H "Authorization: Bearer $API_KEY" \
  144. -d '{
  145. "model": "chatglm3-6b-32k",
  146. "messages": [{"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Hello!"}]
  147. }'`, // different line, placeholder
  148. `curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer $(gcloud auth print-access-token)" https://workflowexecutions.googleapis.com/v1/projects/244283331594/locations/us-central1/workflows/sample-workflow/executions/43c925aa-514a-44c1-a0a4-a9f8f26fd2cb/callbacks/1705791f-d446-4e92-a6d0-a13622422e80_31864a51-8c13-4b03-ad4d-945cdc8d0631`, // script
  149. // Not valid BASIC
  150. `curl -X POST -H "Content-Type: application/json" -H "Authorization: Basic eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb25maWRlbmNlIjowLjh9.kvRPfjAhLhtRTczoRgctVGp7KY1QVH3UBZM-gM0x8ec" \`,
  151. }
  152. return utils.Validate(r, tps, fps)
  153. }