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

binding.go 2.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "regexp"
  8. "strings"
  9. "github.com/go-macaron/binding"
  10. )
  11. const (
  12. // ErrGitRefName is git reference name error
  13. ErrGitRefName = "GitRefNameError"
  14. )
  15. var (
  16. // GitRefNamePattern is regular expression with unallowed characters in git reference name
  17. GitRefNamePattern = regexp.MustCompile("[^\\d\\w-_\\./]")
  18. )
  19. // AddBindingRules adds additional binding rules
  20. func AddBindingRules() {
  21. addGitRefNameBindingRule()
  22. addValidURLBindingRule()
  23. }
  24. func addGitRefNameBindingRule() {
  25. // Git refname validation rule
  26. binding.AddRule(&binding.Rule{
  27. IsMatch: func(rule string) bool {
  28. return strings.HasPrefix(rule, "GitRefName")
  29. },
  30. IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
  31. str := fmt.Sprintf("%v", val)
  32. if GitRefNamePattern.MatchString(str) {
  33. errs.Add([]string{name}, ErrGitRefName, "GitRefName")
  34. return false, errs
  35. }
  36. // Additional rules as described at https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
  37. if strings.HasPrefix(str, "/") || strings.HasSuffix(str, "/") ||
  38. strings.HasSuffix(str, ".") || strings.Contains(str, "..") ||
  39. strings.Contains(str, "//") {
  40. errs.Add([]string{name}, ErrGitRefName, "GitRefName")
  41. return false, errs
  42. }
  43. parts := strings.Split(str, "/")
  44. for _, part := range parts {
  45. if strings.HasSuffix(part, ".lock") || strings.HasPrefix(part, ".") {
  46. errs.Add([]string{name}, ErrGitRefName, "GitRefName")
  47. return false, errs
  48. }
  49. }
  50. return true, errs
  51. },
  52. })
  53. }
  54. func addValidURLBindingRule() {
  55. // URL validation rule
  56. binding.AddRule(&binding.Rule{
  57. IsMatch: func(rule string) bool {
  58. return strings.HasPrefix(rule, "ValidUrl")
  59. },
  60. IsValid: func(errs binding.Errors, name string, val interface{}) (bool, binding.Errors) {
  61. str := fmt.Sprintf("%v", val)
  62. if len(str) != 0 && !IsValidURL(str) {
  63. errs.Add([]string{name}, binding.ERR_URL, "Url")
  64. return false, errs
  65. }
  66. return true, errs
  67. },
  68. })
  69. }
  70. func portOnly(hostport string) string {
  71. colon := strings.IndexByte(hostport, ':')
  72. if colon == -1 {
  73. return ""
  74. }
  75. if i := strings.Index(hostport, "]:"); i != -1 {
  76. return hostport[i+len("]:"):]
  77. }
  78. if strings.Contains(hostport, "]") {
  79. return ""
  80. }
  81. return hostport[colon+len(":"):]
  82. }
  83. func validPort(p string) bool {
  84. for _, r := range []byte(p) {
  85. if r < '0' || r > '9' {
  86. return false
  87. }
  88. }
  89. return true
  90. }