forked from cli/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsanitize_ascii.go
More file actions
193 lines (178 loc) · 4.47 KB
/
sanitize_ascii.go
File metadata and controls
193 lines (178 loc) · 4.47 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
package api
import (
"bytes"
"errors"
"io"
"net/http"
"regexp"
"strings"
)
var jsonTypeRE = regexp.MustCompile(`[/+]json($|;)`)
// GitHub servers return non-printable characters as their unicode code point values.
// The values of \u0000 to \u001F represent C0 ASCII control characters and
// the values of \u0080 to \u009F represent C1 ASCII control characters. These control
// characters will be interpreted by the terminal, this behaviour can be used maliciously
// as an attack vector, especially the control character \u001B. This function wraps
// JSON response bodies in a ReadCloser that transforms C0 and C1 control characters
// to their caret and hex notations respectively so that the terminal will not interpret them.
func AddASCIISanitizer(rt http.RoundTripper) http.RoundTripper {
return &funcTripper{roundTrip: func(req *http.Request) (*http.Response, error) {
res, err := rt.RoundTrip(req)
if err != nil || !jsonTypeRE.MatchString(res.Header.Get("Content-Type")) {
return res, err
}
res.Body = &sanitizeASCIIReadCloser{ReadCloser: res.Body}
return res, err
}}
}
// sanitizeASCIIReadCloser implements the ReadCloser interface.
type sanitizeASCIIReadCloser struct {
io.ReadCloser
addBackslash bool
previousWindow []byte
}
// Read uses a sliding window alogorithm to detect C0 and C1
// ASCII control sequences as they are read and replaces them
// with equivelent inert characters. Characters that are not part
// of a control sequence not modified.
func (s *sanitizeASCIIReadCloser) Read(out []byte) (int, error) {
var readErr error
var outIndex int
var bufIndex int
var bufLen int
var window []byte
buf := make([]byte, len(out))
bufLen, readErr = s.ReadCloser.Read(buf)
if readErr != nil && !errors.Is(readErr, io.EOF) {
if bufLen > 0 {
// Do not sanitize if there was a read error that is not EOF.
bufLen = copy(out, buf)
}
return bufLen, readErr
}
if s.previousWindow != nil {
buf = append(s.previousWindow, buf...)
bufLen += len(s.previousWindow)
}
for {
remaining := min(6, (bufLen - bufIndex))
window = buf[bufIndex : bufIndex+remaining]
if remaining < 6 {
break
}
if bytes.HasPrefix(window, []byte(`\u00`)) {
repl, _ := mapControlCharacterToCaret(window)
if s.addBackslash {
repl = append([]byte{92}, repl...)
}
l := len(repl)
for j := 0; j < l; j++ {
out[outIndex] = repl[j]
outIndex++
}
bufIndex += 6
s.addBackslash = false
continue
}
if window[0] == '\\' {
s.addBackslash = !s.addBackslash
} else {
s.addBackslash = false
}
out[outIndex] = buf[bufIndex]
outIndex++
bufIndex++
}
if readErr != nil && errors.Is(readErr, io.EOF) {
remaining := bufLen - bufIndex
for j := 0; j < remaining; j++ {
out[outIndex] = window[j]
outIndex++
bufIndex++
}
} else {
s.previousWindow = window
}
return outIndex, readErr
}
// mapControlCharacterToCaret maps C0 control sequences to caret notation
// and C1 control sequences to hex notation. C1 control sequences do not
// have caret notation representation.
func mapControlCharacterToCaret(b []byte) ([]byte, bool) {
m := map[string]string{
`\u0000`: `^@`,
`\u0001`: `^A`,
`\u0002`: `^B`,
`\u0003`: `^C`,
`\u0004`: `^D`,
`\u0005`: `^E`,
`\u0006`: `^F`,
`\u0007`: `^G`,
`\u0008`: `^H`,
`\u0009`: `^I`,
`\u000a`: `^J`,
`\u000b`: `^K`,
`\u000c`: `^L`,
`\u000d`: `^M`,
`\u000e`: `^N`,
`\u000f`: `^O`,
`\u0010`: `^P`,
`\u0011`: `^Q`,
`\u0012`: `^R`,
`\u0013`: `^S`,
`\u0014`: `^T`,
`\u0015`: `^U`,
`\u0016`: `^V`,
`\u0017`: `^W`,
`\u0018`: `^X`,
`\u0019`: `^Y`,
`\u001a`: `^Z`,
`\u001b`: `^[`,
`\u001c`: `^\\`,
`\u001d`: `^]`,
`\u001e`: `^^`,
`\u001f`: `^_`,
`\u0080`: `\\200`,
`\u0081`: `\\201`,
`\u0082`: `\\202`,
`\u0083`: `\\203`,
`\u0084`: `\\204`,
`\u0085`: `\\205`,
`\u0086`: `\\206`,
`\u0087`: `\\207`,
`\u0088`: `\\210`,
`\u0089`: `\\211`,
`\u008a`: `\\212`,
`\u008b`: `\\213`,
`\u008c`: `\\214`,
`\u008d`: `\\215`,
`\u008e`: `\\216`,
`\u008f`: `\\217`,
`\u0090`: `\\220`,
`\u0091`: `\\221`,
`\u0092`: `\\222`,
`\u0093`: `\\223`,
`\u0094`: `\\224`,
`\u0095`: `\\225`,
`\u0096`: `\\226`,
`\u0097`: `\\227`,
`\u0098`: `\\230`,
`\u0099`: `\\231`,
`\u009a`: `\\232`,
`\u009b`: `\\233`,
`\u009c`: `\\234`,
`\u009d`: `\\235`,
`\u009e`: `\\236`,
`\u009f`: `\\237`,
}
if c, ok := m[strings.ToLower(string(b))]; ok {
return []byte(c), true
}
return b, false
}
func min(a, b int) int {
if a < b {
return a
}
return b
}