Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c63d598

Browse files
committed
Sanitize StringBuilder appends that follow directly from a constructor.
Note that some of this logic ought to be incorporated into StringBuilderVar once that code can be reviewed.
1 parent b5a450b commit c63d598

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

java/ql/src/Security/CWE/CWE-918/RequestForgery.qll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,11 +229,37 @@ private MethodAccess getNextAppend(MethodAccess append) {
229229
result = any(StringBuilderVar sbv).getNextAppend(append)
230230
}
231231

232+
private Expr getQualifier(MethodAccess e) { result = e.getQualifier() }
233+
234+
private MethodAccess getAChainedAppend(Expr e) {
235+
(
236+
result.getQualifier() = e
237+
or
238+
result.getQualifier() = getAChainedAppend(e)
239+
) and
240+
result.getCallee().getDeclaringType() instanceof StringBuildingType and
241+
result.getCallee().getName() = "append"
242+
}
243+
232244
class HostnameSanitizedExpr extends Expr {
233245
HostnameSanitizedExpr() {
234246
// Sanitize expressions that come after a sanitizing prefix in a tree of string additions:
235247
this = getASanitizedAddOperand()
236248
or
249+
// Sanitize all appends to a StringBuilder that is initialized with a sanitizing prefix:
250+
// (note imprecision: if the same StringBuilder/StringBuffer has more than one constructor call,
251+
// this sanitizes all of its append calls, not just those that may follow the constructor).
252+
exists(StringBuilderVar sbv, ConstructorCall constructor, Expr initializer |
253+
initializer = sbv.getAnAssignedValue() and
254+
constructor = getQualifier*(initializer) and
255+
constructor.getArgument(0) instanceof HostnameSanitzingPrefix and
256+
(
257+
this = sbv.getAnAppend().getArgument(0)
258+
or
259+
this = getAChainedAppend(constructor).getArgument(0)
260+
)
261+
)
262+
or
237263
// Sanitize expressions that come after a sanitizing prefix in a sequence of StringBuilder operations:
238264
exists(MethodAccess appendSanitizingConstant, MethodAccess subsequentAppend |
239265
appendSanitizingConstant.getArgument(0) instanceof HostnameSanitzingPrefix and

0 commit comments

Comments
 (0)