You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

binding.go 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package validation
  5. import (
  6. "fmt"
  7. "net/url"
  8. "regexp"
  9. "strings"
  10. "github.com/go-macaron/binding"
  11. )
  12. const (
  13. // ErrGitRefName is git reference name error
  14. ErrGitRefName = "GitRefNameError"
  15. )
  16. var (
  17. // GitRefNamePattern is regular expression with unallowed characters in git reference name
  18. GitRefNamePattern = regexp.MustCompile("[^\\d\\w-_\\./]")
  19. )
  20. // AddBindingRules adds additional binding rules
  21. func AddBindingRules() {
  22. addGitRefNameBindingRule()
  23. addValidURLBindingRule()
  24. }
  25. func addGitRefNameBindingRule() {
  26. // Git refname validation rule
  27. binding.AddRule(&binding.Rule{
  28. IsMatch: func(rule string) bool {
  29. return strings.HasPrefix(rule, "GitRefName")
  30. },
  31. IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
  32. str := fmt.Sprintf("%v", val)
  33. if GitRefNamePattern.MatchString(str) {
  34. errs.Add([]string{name}, ErrGitRefName, "GitRefName")
  35. return false, errs
  36. }
  37. // Additional rules as described at https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
  38. if strings.HasPrefix(str, "/") || strings.HasSuffix(str, "/") ||
  39. strings.HasSuffix(str, ".") || strings.Contains(str, "..") ||
  40. strings.Contains(str, "//") {
  41. errs.Add([]string{name}, ErrGitRefName, "GitRefName")
  42. return false, errs
  43. }
  44. parts := strings.Split(str, "/")
  45. for _, part := range parts {
  46. if strings.HasSuffix(part, ".lock") || strings.HasPrefix(part, ".") {
  47. errs.Add([]string{name}, ErrGitRefName, "GitRefName")
  48. return false, errs
  49. }
  50. }
  51. return true, errs
  52. },
  53. })
  54. }
  55. func addValidURLBindingRule() {
  56. // URL validation rule
  57. binding.AddRule(&binding.Rule{
  58. IsMatch: func(rule string) bool {
  59. return strings.HasPrefix(rule, "ValidUrl")
  60. },
  61. IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
  62. str := fmt.Sprintf("%v", val)
  63. if len(str) != 0 {
  64. if u, err := url.ParseRequestURI(str); err != nil ||
  65. (u.Scheme != "http" && u.Scheme != "https") ||
  66. !validPort(portOnly(u.Host)) {
  67. errs.Add([]string{name}, binding.ERR_URL, "Url")
  68. return false, errs
  69. }
  70. }
  71. return true, errs
  72. },
  73. })
  74. }
  75. func portOnly(hostport string) string {
  76. colon := strings.IndexByte(hostport, ':')
  77. if colon == -1 {
  78. return ""
  79. }
  80. if i := strings.Index(hostport, "]:"); i != -1 {
  81. return hostport[i+len("]:"):]
  82. }
  83. if strings.Contains(hostport, "]") {
  84. return ""
  85. }
  86. return hostport[colon+len(":"):]
  87. }
  88. func validPort(p string) bool {
  89. for _, r := range []byte(p) {
  90. if r < '0' || r > '9' {
  91. return false
  92. }
  93. }
  94. return true
  95. }