Skip to content

Commit cc54164

Browse files
committed
added more sinks related to io.Writer of BodyWriter
1 parent b6aaff2 commit cc54164

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

go/ql/lib/semmle/go/frameworks/Fasthttp.qll

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,43 @@ module Fasthttp {
9696
)
9797
or
9898
exists(DataFlow::CallNode writerCall |
99-
writerCall = any(Function fprintf | fprintf.hasQualifiedName("fmt", "Fprintf")).getACall() and
99+
writerCall =
100+
any(Function fprintf | fprintf.hasQualifiedName("fmt", ["Fprint", "Fprintf", "Fprintln"]))
101+
.getACall() and
100102
sink = writerCall.getArgument(0) and
101103
body = writerCall.getSyntacticArgument(any(int i | i > 1))
102104
)
105+
or
106+
exists(DataFlow::CallNode writerCall |
107+
writerCall =
108+
any(Function ioCopy |
109+
ioCopy.hasQualifiedName("io", ["copy", "CopyBuffer", "CopyN", "WriteString"])
110+
).getACall() and
111+
sink = writerCall.getArgument(0) and
112+
body = writerCall.getArgument(1)
113+
)
114+
or
115+
exists(DataFlow::CallNode writerCall |
116+
writerCall =
117+
any(Function ioTeeReader | ioTeeReader.hasQualifiedName("io", "TeeReader")).getACall() and
118+
sink = writerCall.getArgument(1) and
119+
body = writerCall.getArgument(0)
120+
)
121+
or
122+
exists(DataFlow::CallNode writerCall |
123+
writerCall =
124+
any(Method bufioWriteTo | bufioWriteTo.hasQualifiedName("bufio", "Reader", "WriteTo"))
125+
.getACall() and
126+
sink = writerCall.getArgument(0) and
127+
body = writerCall.getReceiver()
128+
)
129+
or
130+
exists(DataFlow::CallNode writerCall |
131+
writerCall =
132+
any(Method bytes | bytes.hasQualifiedName("bytes", "Buffer", "WriteTo")).getACall() and
133+
sink = writerCall.getArgument(0) and
134+
body = writerCall.getReceiver()
135+
)
103136
}
104137

105138
private predicate writerSink(DataFlow::Node sink) { writerSinkAndBody(sink, _) }

go/ql/test/library-tests/semmle/go/frameworks/Fasthttp/fasthttp.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ package main
44

55
import (
66
"bufio"
7+
"bytes"
78
"fmt"
9+
"io"
810
"net"
911
"time"
1012

@@ -176,11 +178,19 @@ func fasthttpServer() {
176178
userInput := "user Controlled input"
177179
requestCtx.SetContentType("text/html")
178180
userInputByte := []byte("user Controlled input")
181+
userInputReader := bytes.NewReader(userInputByte)
179182
requestCtx.Response.AppendBody(userInputByte) // $ XssSink=userInputByte
180183
requestCtx.Response.AppendBodyString(userInput) // $ XssSink=userInput
181184
rspWriter := requestCtx.Response.BodyWriter()
182-
rspWriter.Write(userInputByte) // $ XssSink=userInputByte
183-
fmt.Fprintf(rspWriter, "%s", userInputByte) // $ XssSink=userInputByte
185+
rspWriter.Write(userInputByte) // $ XssSink=userInputByte
186+
fmt.Fprintf(rspWriter, "%s", userInputByte) // $ XssSink=userInputByte
187+
io.WriteString(rspWriter, userInput) // $ XssSink=userInput
188+
io.TeeReader(userInputReader, rspWriter) // $ XssSink=userInputReader
189+
io.TeeReader(userInputReader, rspWriter) // $ XssSink=userInputReader
190+
bufioReader := bufio.NewReader(dstReader)
191+
bufioReader.WriteTo(rspWriter) // $ XssSink=bufioReader
192+
bytesUserInput := bytes.NewBuffer(userInputByte)
193+
bytesUserInput.WriteTo(rspWriter) // $ XssSink=bytesUserInput
184194
requestCtx.Response.SetBody(userInputByte) // $ XssSink=userInputByte
185195
requestCtx.Response.SetBodyString(userInput) // $ XssSink=userInput
186196
requestCtx.Response.SetBodyRaw(userInputByte) // $ XssSink=userInputByte

0 commit comments

Comments
 (0)