Ruby: support sprintf formatted string with modulo operator
- 主要语言
- CodeQL
- 星标
- 10.1k
- 派生
- 2.1k
- 平均合并
- 2 天 15 小时
- 30 天内合并 PR
- 141
描述
I noticed that dataflow in Ruby isn't propagated to [Kernel.sprintf](https://ruby-doc.org/3.2.2/String.html#method-i-25) formatted strings, e.g. the stored xss query should flag this code in an ERB template:
```rb
<%# BAD: Kernel.sprintf modulo operator syntax %>
<%= "Welcome %{user}".html_safe % { user: @user.handle } %>
```
The string literal is parsed as a a single `Ast::StringTextComponent`, where it should probably also contain a `Ast::StringInterpolationComponent`. I tried to work around this problem using an additional taint step:
```ql
predicate isAdditionalSprintfTaintStep(DataFlow::Node node1, DataFlow::Node node2) {
exists(ModuloExpr expr, HashLiteral hash, StringLiteral str |
hash.getParent*() = expr.getRightOperand() and
str.getParent*() = expr.getLeftOperand() and
hash.getAKeyValuePair().getValue() = node1.asExpr().getExpr() and
str = node2.asExpr().getExpr()
)
}
```
which works for the code snippet above, but doesn't work when the dataflow gets a bit more complex:
```rb
<% sink = "Welcome %{user}".html_safe %>
<%= sink % { user: @user.handle } %>
```
I tried the following, but it doesn't work:
```ql
predicate isAdditionalSprintfTaintStep(DataFlow::Node node1, DataFlow::Node node2) {
exists(ModuloExpr expr, HashLiteral hash, StringLiteral str |
DataFlow::localExprFlow(hash.getAControlFlowNode(), expr.getRightOperand().getAControlFlowNode()) and
DataFlow::localExprFlow(str.getAControlFlowNode(), expr.getLeftOperand().getAControlFlowNode()) and
hash.getAKeyValuePair().getValue() = node1.asExpr().getExpr() and
str = node2.asExpr().getExpr()
)
}
```
How do I catch the insecure code snippet above using local dataflow?
贡献指南
调研方向
从 Ruby 对 Kernel.sprintf 模运算语法的处理,以及 issue 中描述的 Ast::StringTextComponent 与 Ast::StringInterpolationComponent 行为入手。将两个 ERB 示例与尝试过的 isAdditionalSprintfTaintStep 谓词和本地数据流调用进行比较;当 stored-XSS 示例能够通过本地数据流被捕获时即表示完成,包括格式化字符串先赋值给 sink 的情况。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- ruby
- 领域
- security
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100