Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions go/ql/lib/change-notes/2024-02-14-range-map-read.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: fix
---
* Fixed dataflow out of a `map` using a `range` statement.
16 changes: 8 additions & 8 deletions go/ql/lib/semmle/go/dataflow/internal/ContainerFlow.qll
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ predicate containerStoreStep(Node node1, Node node2, Content c) {
or
c instanceof MapKeyContent and
node2.getType() instanceof MapType and
exists(Write w | w.writesElement(node2, node1, _))
exists(Write w | w.writesElement(node2.(PostUpdateNode).getPreUpdateNode(), node1, _))
or
c instanceof MapValueContent and
node2.getType() instanceof MapType and
exists(Write w | w.writesElement(node2, _, node1))
exists(Write w | w.writesElement(node2.(PostUpdateNode).getPreUpdateNode(), _, node1))
}

/**
Expand All @@ -57,11 +57,11 @@ predicate containerStoreStep(Node node1, Node node2, Content c) {
predicate containerReadStep(Node node1, Node node2, Content c) {
c instanceof ArrayContent and
(
node2.(Read).readsElement(node1, _) and
(
node1.getType() instanceof ArrayType or
node1.getType() instanceof SliceType
)
node1.getType() instanceof ArrayType or
node1.getType() instanceof SliceType
) and
(
node2.(Read).readsElement(node1, _)
or
node2.(RangeElementNode).getBase() = node1
or
Expand All @@ -85,5 +85,5 @@ predicate containerReadStep(Node node1, Node node2, Content c) {
or
c instanceof MapValueContent and
node1.getType() instanceof MapType and
node2.(Read).readsElement(node1, _)
(node2.(Read).readsElement(node1, _) or node2.(RangeElementNode).getBase() = node1)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import go
import TestUtilities.InlineFlowTest
import DefaultFlowTest
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

func source() string {
return "untrusted data"
}

func sink(any) {
}

func main() {
var someMap map[string]string = map[string]string{}
someMap["someKey"] = source()

for _, val := range someMap {
sink(val) // $ hasValueFlow="val"
}
}

func testLiteral() {
someMap := map[string]string{"someKey": source()}

for _, val := range someMap {
sink(val) // $ hasValueFlow="val"
}
}