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.
 
 
 

144 lines
3.7 KiB

  1. // Copyright 2015 Google Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package martianurl
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "net/http"
  19. "net/url"
  20. "strings"
  21. "github.com/google/martian"
  22. "github.com/google/martian/parse"
  23. "github.com/google/martian/verify"
  24. )
  25. const (
  26. errFormat = "request(%s) url verify failure:\n%s"
  27. errPartFormat = "\t%s: got %q, want %q"
  28. )
  29. func init() {
  30. parse.Register("url.Verifier", verifierFromJSON)
  31. }
  32. // Verifier verifies the structure of URLs.
  33. type Verifier struct {
  34. url *url.URL
  35. err *martian.MultiError
  36. }
  37. type verifierJSON struct {
  38. Scheme string `json:"scheme"`
  39. Host string `json:"host"`
  40. Path string `json:"path"`
  41. Query string `json:"query"`
  42. Scope []parse.ModifierType `json:"scope"`
  43. }
  44. // NewVerifier returns a new URL verifier.
  45. func NewVerifier(url *url.URL) verify.RequestVerifier {
  46. return &Verifier{
  47. url: url,
  48. err: martian.NewMultiError(),
  49. }
  50. }
  51. // ModifyRequest verifies that the request URL matches all parts of url. If the
  52. // value in url is non-empty it must be an exact match.
  53. func (v *Verifier) ModifyRequest(req *http.Request) error {
  54. // skip requests to API
  55. ctx := martian.NewContext(req)
  56. if ctx.IsAPIRequest() {
  57. return nil
  58. }
  59. var failures []string
  60. u := req.URL
  61. if v.url.Scheme != "" && v.url.Scheme != u.Scheme {
  62. f := fmt.Sprintf(errPartFormat, "Scheme", u.Scheme, v.url.Scheme)
  63. failures = append(failures, f)
  64. }
  65. if v.url.Host != "" && !MatchHost(u.Host, v.url.Host) {
  66. f := fmt.Sprintf(errPartFormat, "Host", u.Host, v.url.Host)
  67. failures = append(failures, f)
  68. }
  69. if v.url.Path != "" && v.url.Path != u.Path {
  70. f := fmt.Sprintf(errPartFormat, "Path", u.Path, v.url.Path)
  71. failures = append(failures, f)
  72. }
  73. if v.url.RawQuery != "" && v.url.RawQuery != u.RawQuery {
  74. f := fmt.Sprintf(errPartFormat, "Query", u.RawQuery, v.url.RawQuery)
  75. failures = append(failures, f)
  76. }
  77. if v.url.Fragment != "" && v.url.Fragment != u.Fragment {
  78. f := fmt.Sprintf(errPartFormat, "Fragment", u.Fragment, v.url.Fragment)
  79. failures = append(failures, f)
  80. }
  81. if len(failures) > 0 {
  82. err := fmt.Errorf(errFormat, u, strings.Join(failures, "\n"))
  83. v.err.Add(err)
  84. }
  85. return nil
  86. }
  87. // VerifyRequests returns an error if verification for any request failed.
  88. // If an error is returned it will be of type *martian.MultiError.
  89. func (v *Verifier) VerifyRequests() error {
  90. if v.err.Empty() {
  91. return nil
  92. }
  93. return v.err
  94. }
  95. // ResetRequestVerifications clears all failed request verifications.
  96. func (v *Verifier) ResetRequestVerifications() {
  97. v.err = martian.NewMultiError()
  98. }
  99. // verifierFromJSON builds a martianurl.Verifier from JSON.
  100. //
  101. // Example modifier JSON:
  102. // {
  103. // "martianurl.Verifier": {
  104. // "scope": ["request"],
  105. // "scheme": "https",
  106. // "host": "www.google.com",
  107. // "path": "/proxy",
  108. // "query": "testing=true"
  109. // }
  110. // }
  111. func verifierFromJSON(b []byte) (*parse.Result, error) {
  112. msg := &verifierJSON{}
  113. if err := json.Unmarshal(b, msg); err != nil {
  114. return nil, err
  115. }
  116. v := NewVerifier(&url.URL{
  117. Scheme: msg.Scheme,
  118. Host: msg.Host,
  119. Path: msg.Path,
  120. RawQuery: msg.Query,
  121. })
  122. return parse.NewResult(v, msg.Scope)
  123. }