Skip to content

Commit

Permalink
fix(tcp): fix potential nil reference in TCPProxy
Browse files Browse the repository at this point in the history
  • Loading branch information
mefellows committed May 19, 2017
1 parent 40d054a commit 9ebeee2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 39 deletions.
2 changes: 1 addition & 1 deletion protocol/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (p *proxy) pipe(src io.Reader, dst io.Writer) {
log.Trace("TCP Proxy overwriting bytes sent to target: %s", ctx.Bytes)
} else {
middleware.HandleEvent(muxy.EventPostDispatch, ctx)
log.Trace("TCP Proxy overwriting bytes sent to client: %s", ctx.Bytes)
log.Trace("TCP Proxy overwriting bytes sent back to originating client: %s", ctx.Bytes)
}
b = ctx.Bytes
}
Expand Down
77 changes: 39 additions & 38 deletions symptom/http_tamperer.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,48 +141,49 @@ func (m *HTTPTampererSymptom) MuckRequest(ctx *muxy.Context) {

// MuckResponse adds chaos to the response
func (m *HTTPTampererSymptom) MuckResponse(ctx *muxy.Context) {

// Body
if m.Response.Body != "" {
cl := ioutil.NopCloser(bytes.NewReader([]byte(m.Response.Body)))
r := &http.Response{
Request: ctx.Request,
Header: ctx.Response.Header,
Close: ctx.Response.Close,
ContentLength: ctx.Response.ContentLength,
Trailer: ctx.Response.Trailer,
TLS: ctx.Response.TLS,
TransferEncoding: ctx.Response.TransferEncoding,
Status: ctx.Response.Status,
StatusCode: ctx.Response.StatusCode,
Proto: ctx.Response.Proto,
ProtoMajor: ctx.Response.ProtoMajor,
ProtoMinor: ctx.Response.ProtoMinor,
Body: cl,
if ctx.Response != nil {
// Body
if m.Response.Body != "" {
cl := ioutil.NopCloser(bytes.NewReader([]byte(m.Response.Body)))
r := &http.Response{
Request: ctx.Request,
Header: ctx.Response.Header,
Close: ctx.Response.Close,
ContentLength: ctx.Response.ContentLength,
Trailer: ctx.Response.Trailer,
TLS: ctx.Response.TLS,
TransferEncoding: ctx.Response.TransferEncoding,
Status: ctx.Response.Status,
StatusCode: ctx.Response.StatusCode,
Proto: ctx.Response.Proto,
ProtoMajor: ctx.Response.ProtoMajor,
ProtoMinor: ctx.Response.ProtoMinor,
Body: cl,
}
log.Debug("HTTP Tamperer Injecting HTTP Response Body with [%s]", log.Colorize(log.BLUE, m.Response.Body))
*ctx.Response = *r
}
log.Debug("HTTP Tamperer Injecting HTTP Response Body with [%s]", log.Colorize(log.BLUE, m.Response.Body))
*ctx.Response = *r
}

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

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

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

Expand Down

0 comments on commit 9ebeee2

Please sign in to comment.