Skip to content

Commit 602495d

Browse files
committed
Replace cases accidentally handled by CompareExprSanitizer with ReturnedAlongsideErrorSanitizerGuard
1 parent b48a07e commit 602495d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

go/ql/src/experimental/CWE-321/HardcodedKeysLib.qll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,26 @@ module HardcodedKeys {
188188
}
189189
}
190190

191+
/**
192+
* Marks anything returned alongside an error-value that is known
193+
* to be non-nil by virtue of a guarding check as harmless.
194+
*
195+
* For example, `if err != nil { return "", err }` is unlikely to be
196+
* contributing a dangerous hardcoded key.
197+
*/
198+
private class ReturnedAlongsideErrorSanitizerGuard extends Sanitizer {
199+
200+
ReturnedAlongsideErrorSanitizerGuard() {
201+
exists(ControlFlow::ConditionGuardNode guard, SsaWithFields errorVar, ReturnStmt r |
202+
guard.ensuresNeq(errorVar.getAUse(), Builtin::nil().getARead()) and
203+
guard.dominates(this.getBasicBlock()) and
204+
r.getExpr(1) = errorVar.getAUse().asExpr() and
205+
this.asExpr() = r.getExpr(0)
206+
)
207+
}
208+
209+
}
210+
191211
/** Mark any formatting string call as a sanitizer */
192212
private class FormattingSanitizer extends Sanitizer {
193213
FormattingSanitizer() { exists(Formatting::StringFormatCall s | s.getAResult() = this) }

0 commit comments

Comments
 (0)