Skip to content

Commit 9ebeee2

Browse files
committed
fix(tcp): fix potential nil reference in TCPProxy
1 parent 40d054a commit 9ebeee2

2 files changed

Lines changed: 40 additions & 39 deletions

File tree

protocol/tcp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (p *proxy) pipe(src io.Reader, dst io.Writer) {
168168
log.Trace("TCP Proxy overwriting bytes sent to target: %s", ctx.Bytes)
169169
} else {
170170
middleware.HandleEvent(muxy.EventPostDispatch, ctx)
171-
log.Trace("TCP Proxy overwriting bytes sent to client: %s", ctx.Bytes)
171+
log.Trace("TCP Proxy overwriting bytes sent back to originating client: %s", ctx.Bytes)
172172
}
173173
b = ctx.Bytes
174174
}

symptom/http_tamperer.go

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -141,48 +141,49 @@ func (m *HTTPTampererSymptom) MuckRequest(ctx *muxy.Context) {
141141

142142
// MuckResponse adds chaos to the response
143143
func (m *HTTPTampererSymptom) MuckResponse(ctx *muxy.Context) {
144-
145-
// Body
146-
if m.Response.Body != "" {
147-
cl := ioutil.NopCloser(bytes.NewReader([]byte(m.Response.Body)))
148-
r := &http.Response{
149-
Request: ctx.Request,
150-
Header: ctx.Response.Header,
151-
Close: ctx.Response.Close,
152-
ContentLength: ctx.Response.ContentLength,
153-
Trailer: ctx.Response.Trailer,
154-
TLS: ctx.Response.TLS,
155-
TransferEncoding: ctx.Response.TransferEncoding,
156-
Status: ctx.Response.Status,
157-
StatusCode: ctx.Response.StatusCode,
158-
Proto: ctx.Response.Proto,
159-
ProtoMajor: ctx.Response.ProtoMajor,
160-
ProtoMinor: ctx.Response.ProtoMinor,
161-
Body: cl,
144+
if ctx.Response != nil {
145+
// Body
146+
if m.Response.Body != "" {
147+
cl := ioutil.NopCloser(bytes.NewReader([]byte(m.Response.Body)))
148+
r := &http.Response{
149+
Request: ctx.Request,
150+
Header: ctx.Response.Header,
151+
Close: ctx.Response.Close,
152+
ContentLength: ctx.Response.ContentLength,
153+
Trailer: ctx.Response.Trailer,
154+
TLS: ctx.Response.TLS,
155+
TransferEncoding: ctx.Response.TransferEncoding,
156+
Status: ctx.Response.Status,
157+
StatusCode: ctx.Response.StatusCode,
158+
Proto: ctx.Response.Proto,
159+
ProtoMajor: ctx.Response.ProtoMajor,
160+
ProtoMinor: ctx.Response.ProtoMinor,
161+
Body: cl,
162+
}
163+
log.Debug("HTTP Tamperer Injecting HTTP Response Body with [%s]", log.Colorize(log.BLUE, m.Response.Body))
164+
*ctx.Response = *r
162165
}
163-
log.Debug("HTTP Tamperer Injecting HTTP Response Body with [%s]", log.Colorize(log.BLUE, m.Response.Body))
164-
*ctx.Response = *r
165-
}
166166

167-
// Set Cookies
168-
for _, c := range m.Response.Cookies {
169-
c.Expires = stringToDate(c.RawExpires)
170-
log.Debug("HTTP Tamperer Spoofing Response Cookie [%s => %s]", log.Colorize(log.LIGHTMAGENTA, c.Name), c.String())
171-
ctx.Response.Header.Add("Set-Cookie", c.String())
172-
}
167+
// Set Cookies
168+
for _, c := range m.Response.Cookies {
169+
c.Expires = stringToDate(c.RawExpires)
170+
log.Debug("HTTP Tamperer Spoofing Response Cookie [%s => %s]", log.Colorize(log.LIGHTMAGENTA, c.Name), c.String())
171+
ctx.Response.Header.Add("Set-Cookie", c.String())
172+
}
173173

174-
// Set Headers
175-
for k, v := range m.Response.Headers {
176-
key := strings.ToTitle(strings.Replace(k, "_", "-", -1))
177-
log.Debug("HTTP Tamperer Spoofing Response Header [%s => %s]", log.Colorize(log.LIGHTMAGENTA, key), v)
178-
ctx.Response.Header.Add(key, v)
179-
}
174+
// Set Headers
175+
for k, v := range m.Response.Headers {
176+
key := strings.ToTitle(strings.Replace(k, "_", "-", -1))
177+
log.Debug("HTTP Tamperer Spoofing Response Header [%s => %s]", log.Colorize(log.LIGHTMAGENTA, key), v)
178+
ctx.Response.Header.Add(key, v)
179+
}
180180

181-
// This Writes all headers, setting status code - so call this last
182-
if m.Response.Status != 0 {
183-
log.Debug("HTTP Tamperer Spoofing Response Code From [%d] to [%s]", ctx.Response.StatusCode, log.Colorize(log.LIGHTMAGENTA, fmt.Sprintf("%d", m.Response.Status)))
184-
ctx.Response.StatusCode = m.Response.Status
185-
ctx.Response.Status = http.StatusText(m.Response.Status)
181+
// This Writes all headers, setting status code - so call this last
182+
if m.Response.Status != 0 {
183+
log.Debug("HTTP Tamperer Spoofing Response Code From [%d] to [%s]", ctx.Response.StatusCode, log.Colorize(log.LIGHTMAGENTA, fmt.Sprintf("%d", m.Response.Status)))
184+
ctx.Response.StatusCode = m.Response.Status
185+
ctx.Response.Status = http.StatusText(m.Response.Status)
186+
}
186187
}
187188
}
188189

0 commit comments

Comments
 (0)