Repository navigation
Expand file tree
/
Copy pathrequest.go
More file actions
216 lines (207 loc) · 6.79 KB
/
Copy pathrequest.go
File metadata and controls
216 lines (207 loc) · 6.79 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package edge
import (
"bytes"
"context"
"encoding/base64"
"io"
"maps"
"net/http"
"net/netip"
"net/url"
"slices"
"strconv"
"strings"
"github.com/aws/aws-lambda-go/events"
)
func requestV1(ctx context.Context, event events.APIGatewayProxyRequest) (*http.Request, error) {
if err := validateV1(event); err != nil {
return nil, err
}
if !strings.HasPrefix(event.Path, "/") {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "path must begin with a slash")
}
query := make(url.Values)
for key, values := range event.MultiValueQueryStringParameters {
query[key] = slices.Clone(values)
}
for key, value := range event.QueryStringParameters {
if !slices.Contains(event.MultiValueQueryStringParameters[key], value) {
query[key] = append(query[key], value)
}
}
header, err := requestHeaders(event.Headers, event.MultiValueHeaders)
if err != nil {
return nil, err
}
u := &url.URL{Path: event.Path, RawQuery: query.Encode()}
return newRequest(ctx, &http.Request{
Method: event.HTTPMethod,
URL: u,
Header: header,
Host: event.RequestContext.DomainName,
Proto: event.RequestContext.Protocol,
RemoteAddr: event.RequestContext.Identity.SourceIP,
}, event.Body, event.IsBase64Encoded)
}
func requestV2(ctx context.Context, event events.APIGatewayV2HTTPRequest) (*http.Request, error) {
if err := validateV2(event); err != nil {
return nil, err
}
if !strings.HasPrefix(event.RawPath, "/") {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "rawPath must begin with a slash")
}
path, err := url.PathUnescape(event.RawPath)
if err != nil {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid rawPath encoding")
}
u := &url.URL{Path: path, RawPath: event.RawPath, RawQuery: event.RawQueryString}
// EscapedPath ignores an invalid RawPath hint.
// Reject rather than silently replacing raw delimiters, whitespace, or an invalid encoded spelling.
if u.EscapedPath() != event.RawPath {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid rawPath syntax")
}
// Query decoding remains the handler's job, but a request target cannot contain literal ASCII whitespace or control bytes on an HTTP request line.
for i := 0; i < len(event.RawQueryString); i++ {
if event.RawQueryString[i] <= ' ' || event.RawQueryString[i] == 0x7f {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid raw query syntax")
}
}
header, err := requestHeaders(event.Headers, nil)
if err != nil {
return nil, err
}
if len(event.Cookies) != 0 {
cookie := strings.Join(event.Cookies, "; ")
if !validHeaderValue(cookie) {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid cookie header")
}
header.Set("Cookie", cookie)
}
return newRequest(ctx, &http.Request{
Method: event.RequestContext.HTTP.Method,
URL: u,
Header: header,
Host: event.RequestContext.DomainName,
Proto: event.RequestContext.HTTP.Protocol,
RemoteAddr: event.RequestContext.HTTP.SourceIP,
}, event.Body, event.IsBase64Encoded)
}
func requestHeaders(single map[string]string, multi map[string][]string) (http.Header, error) {
header := make(http.Header)
for _, key := range slices.Sorted(maps.Keys(multi)) {
if !validToken(key) {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid header name")
}
name := http.CanonicalHeaderKey(key)
for _, value := range multi[key] {
if !validHeaderValue(value) {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid header value")
}
header[name] = append(header[name], value)
}
}
// Keep the original multivalue lists for cross-map deduplication.
// Repeated single values under differently cased keys must not deduplicate each other.
multiValues := header.Clone()
for _, key := range slices.Sorted(maps.Keys(single)) {
if !validToken(key) {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid header name")
}
value := single[key]
if !validHeaderValue(value) {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid header value")
}
name := http.CanonicalHeaderKey(key)
if !slices.Contains(multiValues[name], value) {
header[name] = append(header[name], value)
}
}
return header, nil
}
func newRequest(ctx context.Context, r *http.Request, body string, binary bool) (*http.Request, error) {
if ctx == nil {
return nil, invocationError(OperationValidate, ErrInvalidInvocation, "nil invocation context")
}
if !validToken(r.Method) {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid HTTP method")
}
host := r.Host
header := r.Header
if hosts, present := header["Host"]; present {
if len(hosts) != 1 || hosts[0] == "" {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid Host header")
}
host = hosts[0]
}
if host != "" && !validAuthority(host) {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid request host")
}
delete(header, "Host")
if r.Proto == "" {
r.Proto = "HTTP/1.1"
}
major, minor, ok := http.ParseHTTPVersion(r.Proto)
if !ok {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid HTTP protocol")
}
var data []byte
if binary {
var err error
data, err = base64.StdEncoding.DecodeString(body)
if err != nil {
return nil, invocationError(OperationRequest, ErrInvalidEvent, "invalid base64 request body")
}
} else {
data = []byte(body)
}
if _, present := header["Content-Length"]; present {
header.Set("Content-Length", strconv.Itoa(len(data)))
}
delete(header, "Transfer-Encoding")
delete(header, "Trailer")
r.RequestURI = r.URL.RequestURI()
r.Host = host
r.ProtoMajor, r.ProtoMinor = major, minor
r.Body = http.NoBody
r.ContentLength = int64(len(data))
if len(data) != 0 {
r.Body = io.NopCloser(bytes.NewReader(data))
}
return r.WithContext(ctx), nil
}
// validToken implements the shared HTTP method/field-name token grammar.
func validToken(s string) bool {
if s == "" {
return false
}
for i := 0; i < len(s); i++ {
c := s[i]
if c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || strings.ContainsRune("!#$%&'*+-.^_`|~", rune(c)) {
continue
}
return false
}
return true
}
func validHeaderValue(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] < ' ' && s[i] != '\t' || s[i] == 0x7f {
return false
}
}
return true
}
func validAuthority(host string) bool {
if strings.ContainsAny(host, "/\\?#@, \t\r\n") {
return false
}
u, err := url.Parse("http://" + host)
if err != nil || u.Host != host || u.Hostname() == "" || u.User != nil || u.Path != "" || u.RawQuery != "" || u.Fragment != "" {
return false
}
if strings.HasPrefix(host, "[") {
addr, err := netip.ParseAddr(u.Hostname())
return err == nil && addr.Is6()
}
return !strings.ContainsAny(u.Hostname(), ":[]")
}