github / github/codeql

Ruby: support sprintf formatted string with modulo operator

Aperta
#15,945 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
question Ruby
Lingua principale
CodeQL
Stelle
10.1k
Fork
2.1k
Merge medio
2g 15h
PR unite (30g)
141

Descrizione

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?

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Inizia dalla gestione da parte di Ruby della sintassi modulo di Kernel.sprintf e dal comportamento di Ast::StringTextComponent rispetto ad Ast::StringInterpolationComponent descritto nell’issue. Confronta i due esempi ERB con i predicati isAdditionalSprintfTaintStep tentati e le chiamate al flusso di dati locale; il lavoro è completato quando l’esempio di stored-XSS viene rilevato tramite il flusso di dati locale, anche quando la stringa formattata viene assegnata prima a sink.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
ruby
Ambito
security
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.