Panic: assignment to entry in nil map in NewRegoPolicyInterpreter (copyObject)
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 58/100
Research direction
Start in internal/regopolicyinterpreter/regopolicyinterpreter.go at the copyObject implementation and the NewRegoPolicyInterpreter path around line 85. Reproduce the empty-map case described in the issue, then run go test -fuzz=FuzzInterpreterLogic -fuzztime=15m; done means empty input no longer causes an assignment-to-nil-map panic.
Written by the indexing model from the issue text.
Description
Bug Description
Invoking NewRegoPolicyInterpreter with an empty map as initial data can panic with assignment to entry in nil map due to how the internal copyObject function handles empty input.
Steps to Reproduce
- Call
NewRegoPolicyInterpreterand passmake(map[string]interface{})(an empty map) for the data parameter. - The function panics when attempting
data["metadata"] = ...inside the constructor.
Root Cause
- In
copyObject, unmarshaling JSON into a nil map returns a nil map, not an initialized map. - Later code assumes the map is always initialized and safe to assign fields to.
Impact
- Triggers a runtime panic with valid (but empty) input.
Recommendation
- Modify
copyObjectto add check for nil or empty input:
func copyObject(data map[string]interface{}) (map[string]interface{}, error) {
// Handle nil or empty input
if data == nil {
return make(map[string]interface{}), nil
}
objJSON, err := json.Marshal(data)
if err != nil {
return nil, err
}
objCopy := make(map[string]interface{}) // Initialize before unmarshaling!
err = json.Unmarshal(objJSON, &objCopy)
if err != nil {
return nil, err
}
return objCopy, nil
}
Fuzzer Identification
- I discovered this issue while fuzzing the regopolicyinterpreter. Here's the test case:
package regopolicyinterpreter
import (
"testing"
)
func FuzzInterpreterLogic(f *testing.F) {
// Seed with a basic Rego package and a query
f.Add("package test\nallow = true", "data.test.allow")
f.Fuzz(func(t *testing.T, moduleCode string, queryStr string) {
// 1. Initialize the interpreter with fuzzed code
rpi, err := NewRegoPolicyInterpreter(moduleCode, nil)
if err != nil {
t.Skip() // Ignore invalid Rego syntax
}
// 2. Try to add a fuzzed module (AddModule returns nothing)
rpi.AddModule("fuzzed.rego", &RegoModule{
Namespace: "fuzzed",
Code: moduleCode,
})
// 3. Attempt a raw query with an empty input map
// want (string, map[string]interface{})
input := make(map[string]interface{})
_, _ = rpi.RawQuery(queryStr, input)
})
}
- Running with:
go test -fuzz=FuzzInterpreterLogic -fuzztime=15m
- Dominant language
- Go
- Stars
- 694
- Forks
- 304
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 28
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoft/hcsshim
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
-
Difficulty 4/5 3-5 days Newbie friendliness 52/100
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
-
Difficulty 4/5 3-5 days Newbie friendliness 62/100
-
Difficulty 4/5 3-5 days Newbie friendliness 58/100
All issues in microsoft/hcsshim
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
bazel-contrib/rules_go#4726 · 1 comment ·
-
area/auto-scaling area/monitoring area/ops-productivity kind/enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Type/Improvement
Difficulty 1/5 1-3 hours Newbie friendliness 90/100
OpenNSW/nsw-srilanka#522 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 92/100