Welcome to mirror list, hosted at ThFree Co, Russian Federation.

validations.go « redirects « internal - gitlab.com/gitlab-org/gitlab-pages.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c469ecfc6aa9b1e2e48a9778aaacb55707574f02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package redirects

import (
	"fmt"
	"net/http"
	"net/url"
	"regexp"
	"strings"

	netlifyRedirects "github.com/tj/go-redirects"

	"gitlab.com/gitlab-org/gitlab-pages/internal/feature"
)

var (
	regexPlaceholder            = regexp.MustCompile(`(?i)^:[a-z]+$`)
	regexSplat                  = regexp.MustCompile(`^\*$`)
	regexPlaceholderReplacement = regexp.MustCompile(`(?i):(?P<placeholder>[a-z]+)`)
)

// validateFromURL validates the from URL in a redirect rule.
// It checks for various invalid cases like unsupported schemes,
// relative URLs, domain redirects without scheme, etc.
// Returns `nil` if the URL is valid.
// nolint: gocyclo
func validateFromURL(urlText string) error {
	fromURL, err := url.Parse(urlText)
	if err != nil {
		return errFailedToParseURL
	}

	// No support for domain level redirects starting with special characters without scheme:
	// - `//google.com`
	// - `/\google.com`
	if (fromURL.Host == "") != (fromURL.Scheme == "") || strings.HasPrefix(fromURL.Path, "/\\") {
		return errNoValidStartingInURLPath
	}

	if fromURL.Scheme != "" && fromURL.Scheme != "http" && fromURL.Scheme != "https" {
		return errNoValidStartingInURLPath
	}

	if fromURL.Scheme == "" && fromURL.Host == "" {
		// No parent traversing relative URL's with `./` or `../`
		// No ambiguous URLs like bare domains `GitLab.com`
		if !strings.HasPrefix(urlText, "/") {
			return errNoValidStartingInURLPath
		}
	}

	if feature.RedirectsPlaceholders.Enabled() && strings.Count(fromURL.Path, "/*") > 1 {
		return errMoreThanOneSplats
	}

	return validateSplatAndPlaceholders(fromURL.Path)
}

// validateURL runs validations against a rule URL.
// Returns `nil` if the URL is valid.
// nolint: gocyclo
func validateToURL(urlText string, status int) error {
	toURL, err := url.Parse(urlText)
	if err != nil {
		return errFailedToParseURL
	}

	allowedPrefix := []string{"/"}
	if feature.DomainRedirects.Enabled() {
		// No support for domain level redirects starting with // or special characters:
		// - `//google.com`
		// - `/\google.com`
		if (toURL.Host == "") != (toURL.Scheme == "") || strings.HasPrefix(toURL.Path, "/\\") {
			return errNoValidStartingInURLPath
		}

		// No support for domain level rewrite
		if isDomainURL(urlText) {
			if status == http.StatusOK {
				return errNoDomainLevelRewrite
			}
			allowedPrefix = append(allowedPrefix, "http://", "https://")
		}

		// No parent traversing relative URL's with `./` or `../`
		// No ambiguous URLs like bare domains `GitLab.com`
		if !startsWithAnyPrefix(urlText, allowedPrefix...) {
			return errNoValidStartingInURLPath
		}
	} else {
		// No support for domain-level redirects to outside sites:
		// - `https://google.com`
		// - `//google.com`
		// - `/\google.com`
		if toURL.Host != "" || toURL.Scheme != "" || strings.HasPrefix(toURL.Path, "/\\") {
			return errNoDomainLevelRedirects
		}

		// No parent traversing relative URL's with `./` or `../`
		// No ambiguous URLs like bare domains `GitLab.com`
		if !startsWithAnyPrefix(urlText, allowedPrefix...) {
			return errNoStartingForwardSlashInURLPath
		}
	}

	return validateSplatAndPlaceholders(toURL.Path)
}

func validateSplatAndPlaceholders(path string) error {
	if feature.RedirectsPlaceholders.Enabled() {
		maxPathSegments := cfg.MaxPathSegments
		// Limit the number of path segments a rule can contain.
		// This prevents the matching logic from generating regular
		// expressions that are too large/complex.
		if strings.Count(path, "/") > maxPathSegments {
			return fmt.Errorf("url path cannot contain more than %d forward slashes", cfg.MaxPathSegments)
		}
	} else {
		// No support for splats, https://docs.netlify.com/routing/redirects/redirect-options/#splats
		if strings.Contains(path, "*") {
			return errNoSplats
		}

		// No support for placeholders, https://docs.netlify.com/routing/redirects/redirect-options/#placeholders
		if regexpPlaceholder.MatchString(path) {
			return errNoPlaceholders
		}
	}

	return nil
}

// validateRule runs all validation rules on the provided rule.
// Returns `nil` if the rule is valid
func validateRule(r netlifyRedirects.Rule) error {
	if err := validateFromURL(r.From); err != nil {
		return err
	}

	if err := validateToURL(r.To, r.Status); err != nil {
		return err
	}

	// No support for query parameters, https://docs.netlify.com/routing/redirects/redirect-options/#query-parameters
	if r.Params != nil {
		return errNoParams
	}

	// We strictly validate return status codes
	switch r.Status {
	case http.StatusOK, http.StatusMovedPermanently, http.StatusFound:
		// noop
	default:
		return errUnsupportedStatus
	}

	// No support for rules that use ! force
	if r.Force {
		return errNoForce
	}

	return nil
}

func startsWithAnyPrefix(s string, prefixes ...string) bool {
	for _, prefix := range prefixes {
		if strings.HasPrefix(s, prefix) {
			return true
		}
	}
	return false
}