This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rfc7239.go
187 lines (172 loc) · 4.17 KB
/
rfc7239.go
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
package httpheader
import (
"net"
"net/http"
"strconv"
"strings"
)
// Forwarded parses the Forwarded header from h (RFC 7239).
//
// Do not trust the returned values for sensitive purposes such as access control,
// unless you have a trusted gateway controlling the Forwarded header. This
// header's syntax makes it possible for a malicious client to submit a malformed
// value that will "shadow" further elements appended to the same value.
func Forwarded(h http.Header) []ForwardedElem {
values := h["Forwarded"]
if values == nil {
return nil
}
elems := make([]ForwardedElem, 0, estimateElems(values))
for v, vs := iterElems("", values); v != ""; v, vs = iterElems(v, vs) {
var elem ForwardedElem
ParamsLoop:
for {
var name, value string
name, value, v = consumeParam(v)
switch name {
case "":
break ParamsLoop
case "for":
elem.For = parseNode(value)
case "by":
elem.By = parseNode(value)
case "host":
elem.Host = value
case "proto":
elem.Proto = strings.ToLower(value)
default:
if elem.Ext == nil {
elem.Ext = make(map[string]string)
}
elem.Ext[name] = value
}
}
elems = append(elems, elem)
}
return elems
}
// SetForwarded replaces the Forwarded header in h (RFC 7239).
// See also AddForwarded.
func SetForwarded(h http.Header, elems []ForwardedElem) {
if len(elems) == 0 {
h.Del("Forwarded")
return
}
h.Set("Forwarded", buildForwarded(elems))
}
// AddForwarded is like SetForwarded but appends instead of replacing.
func AddForwarded(h http.Header, elems ...ForwardedElem) {
if len(elems) == 0 {
return
}
h.Add("Forwarded", buildForwarded(elems))
}
func buildForwarded(elems []ForwardedElem) string {
b := &strings.Builder{}
for i, elem := range elems {
if i > 0 {
write(b, ", ")
}
var wrote bool
wrote = writeNode(b, wrote, "for", elem.For)
wrote = writeNode(b, wrote, "by", elem.By)
if elem.Host != "" {
wrote = writeParam(b, wrote, "host", elem.Host)
}
if elem.Proto != "" {
wrote = writeParam(b, wrote, "proto", elem.Proto)
}
for name, value := range elem.Ext {
wrote = writeParam(b, wrote, name, value)
}
if !wrote {
write(b, "for=unknown")
}
}
return b.String()
}
// A ForwardedElem represents one element of the Forwarded header (RFC 7239).
// Standard parameters are stored in the corresponding fields;
// any extension parameters are stored in Ext.
type ForwardedElem struct {
By Node
For Node
Host string
Proto string
Ext map[string]string
}
// A Node represents a node identifier in a Forwarded header (RFC 7239 Section 6).
// Either IP or ObfuscatedNode may be non-zero, but not both.
// Similarly for Port and ObfuscatedPort.
type Node struct {
IP net.IP
Port int
ObfuscatedNode string
ObfuscatedPort string
}
func parseNode(s string) Node {
var node Node
rawIP, rawPort := s, ""
portPos := strings.LastIndexByte(s, ':')
if portPos != -1 && portPos < strings.IndexByte(s, ']') {
// That's not a port, that's part of the IPv6 address.
portPos = -1
}
if portPos != -1 {
rawIP, rawPort = s[:portPos], s[portPos+1:]
}
rawIP = strings.TrimPrefix(rawIP, "[")
rawIP = strings.TrimSuffix(rawIP, "]")
node.IP = net.ParseIP(rawIP)
if node.IP == nil && strings.ToLower(rawIP) != "unknown" {
node.ObfuscatedNode = rawIP
}
node.Port, _ = strconv.Atoi(rawPort)
if node.Port == 0 && rawPort != "" {
node.ObfuscatedPort = rawPort
}
return node
}
func writeNode(b *strings.Builder, wrote bool, name string, node Node) bool {
var rawIP, rawPort string
switch {
case node.Port != 0:
rawPort = strconv.Itoa(node.Port)
case node.ObfuscatedPort != "":
rawPort = node.ObfuscatedPort
}
switch {
case node.IP != nil:
rawIP = node.IP.String()
case node.ObfuscatedNode != "":
rawIP = node.ObfuscatedNode
case rawPort != "":
rawIP = "unknown"
}
if rawIP == "" && rawPort == "" {
return wrote
}
ipv6 := strings.IndexByte(rawIP, ':') != -1
quoted := ipv6 || rawPort != ""
if wrote {
write(b, ";")
}
write(b, name, "=")
if quoted {
write(b, `"`)
}
if ipv6 {
write(b, "[")
}
write(b, rawIP)
if ipv6 {
write(b, "]")
}
if rawPort != "" {
write(b, ":", rawPort)
}
if quoted {
write(b, `"`)
}
return true
}