github / github/codeql

False positive - Log injection is not mitigated via replace with Regex argument in Kotlin

Aperta
#17,423 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
false-positive Kotlin
Lingua principale
CodeQL
Stelle
10.1k
Fork
2.1k
Merge medio
2g 15h
PR unite (30g)
141

Descrizione

**Description of the false positive**

CodeQL is reporting a log injection vulnerability even though I am deleting the problematic characters with Kotlin's `replace` function call with a Regex as its first parameter.

Reading the query (https://github.com/github/codeql/blob/main/java/ql/lib/semmle/code/java/security/LogInjection.qll) I suspect that's because it searches for uses of `replace` with either Strings or Chars as arguments (in order to check for line break removal), but not uses of `replace` with Regex as its first argument (in Kotlin, there is no `replaceAll` function, there is only a `replace` that can accept either String, Char or Regex).

I have also looked at the tests (https://github.com/github/codeql/blob/main/java/ql/test/query-tests/security/CWE-117/LogInjectionTest.java) and that's why I belive this might be the reason, as the tests always use `replaceAll` when working with regular expressions (as it is a Java file).

**Code samples or links to source code**

```
private fun baseSanitize(param: String) = param.replace(Regex("[^a-zA-Z0-9_-]"), "")
private fun baseSanitize(param: String) = param.replace("[^a-zA-Z0-9_-]".toRegex(), "")
private fun baseSanitize(param: String) = param.replace("\\W".toRegex(), "")
```

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Leggi java/ql/lib/semmle/code/java/security/LogInjection.qll e i test di regressione in java/ql/test/query-tests/security/CWE-117/LogInjectionTest.java, concentrandoti su come vengono gestite le chiamate a replace e replaceAll. Aggiungi la copertura per gli esempi di replace di Kotlin Regex, in modo che la query non li segnali più come log injection, quindi esegui i query test di LogInjectionTest.java.

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

Valutazione

Stack tecnologico
kotlin
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.