Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PrepareForEval panics on empty input #2054

Closed
omri86 opened this issue Feb 3, 2020 · 1 comment · Fixed by #2060
Closed

PrepareForEval panics on empty input #2054

omri86 opened this issue Feb 3, 2020 · 1 comment · Fixed by #2060
Assignees
Labels

Comments

@omri86
Copy link

omri86 commented Feb 3, 2020

Expected Behavior

Running the following code in order to pre-compile the rego policy should return an error (in my opinion):

	res, err := rego.New(
		rego.Query("d"),
		rego.Module("example.rego", ""),
	).PrepareForEval(context.Background())

This happens when the input is empty or contains only comments
e.g., this input will also fail:

input := `# comment
# another comment`

Actual Behavior

Ran it as a test, and got a panic: runtime error: invalid memory address or nil pointer dereference

=== RUN   TestRego
--- FAIL: TestRego (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0xb83327]

goroutine 39 [running]:
testing.tRunner.func1(0xc00014c200)
	/usr/local/go/src/testing/testing.go:874 +0x3a3
panic(0x1df4360, 0x3cc5d50)
	/usr/local/go/src/runtime/panic.go:679 +0x1b2
/vendor/github.com/open-policy-agent/opa/ast.(*Module).Copy(0x0, 0xc0005f7438)
	/.../vendor/github.com/open-policy-agent/opa/ast/policy.go:227 +0x47
/vendor/github.com/open-policy-agent/opa/ast.(*Compiler).Compile(0xc0001b65b0, 0xc0005f7438)
	/.../vendor/github.com/open-policy-agent/opa/ast/compile.go:339 +0x107
/vendor/github.com/open-policy-agent/opa/bundle.writeModules(0x2a69f00, 0xc00005a178, 0x2a9ae40, 0xc0002f2000, 0x2a0f840, 0xc00043bf80, 0xc0001b65b0, 0x2a85060, 0xc0001d1fc0, 0xc00043bf20, ...)
	/.../vendor/github.com/open-policy-agent/opa/bundle/store.go:397 +0x598
/vendor/github.com/open-policy-agent/opa/bundle.activateBundles(0xc0005f7ac0, 0xc00005a178, 0x2a9ae40)
	/.../vendor/github.com/open-policy-agent/opa/bundle/store.go:243 +0x817
/vendor/github.com/open-policy-agent/opa/bundle.Activate(...)
	/.../vendor/github.com/open-policy-agent/opa/bundle/store.go:161
/vendor/github.com/open-policy-agent/opa/rego.(*Rego).compileModules(0xc000116000, 0x2a69f00, 0xc00005a178, 0x2a0f840, 0xc00043bf80, 0x2a85060, 0xc0001d1fc0, 0x0, 0x0)
	/.../vendor/github.com/open-policy-agent/opa/rego/rego.go:1430 +0x1b5
/vendor/github.com/open-policy-agent/opa/rego.(*Rego).prepare(0xc000116000, 0x2a69f00, 0xc00005a178, 0x0, 0xc0005f7c80, 0x1, 0x1, 0x0, 0x1842eea3b5116228)
	/.../vendor/github.com/open-policy-agent/opa/rego/rego.go:1259 +0x1f4
/vendor/github.com/open-policy-agent/opa/rego.(*Rego).PrepareForEval(0xc000116000, 0x2a69f00, 0xc00005a178, 0x0, 0x0, 0x0, 0x0, 0x1, 0xc00044c020, 0x453d28)
	/...vendor/github.com/open-policy-agent/opa/rego/rego.go:1174 +0x4aa
/.../TestRego(0xc00014c200)
	/.../some_test.go... +0xf7
testing.tRunner(0xc00014c200, 0x21ffbf8)
	/usr/local/go/src/testing/testing.go:909 +0xc9
created by testing.(*T).Run
	/usr/local/go/src/testing/testing.go:960 +0x350

Process finished with exit code 1

Steps to Reproduce the Problem

  1. Integrating with OPA Go API @ version v0.15.1
  2. Create and run the following test:
	res, err := rego.New(
		rego.Query("d"),
		rego.Module("example.rego", ""),
	).PrepareForEval(context.Background())

Additional Info

  • Did not find any duplicate issue
  • I know that the query doesn't really make sense, but still - in my opinion - this shouldn't panic
@omri86 omri86 changed the title PrepareForEval panics on empty script PrepareForEval panics on empty input Feb 3, 2020
@tsandall tsandall added the bug label Feb 3, 2020
@tsandall
Copy link
Member

tsandall commented Feb 3, 2020

You're correct that OPA should not panic. In this case, I'd expect an error since the parser should reject an empty module/file.

@patrick-east patrick-east self-assigned this Feb 5, 2020
patrick-east added a commit to patrick-east/opa that referenced this issue Feb 21, 2020
The documentation is pretty clear that a module must at least contain
a package, so it is safe to say that an empty file isn't valid.

Previously the helper would just return a nil module and nil error, it
will now return an error.

Fixes: open-policy-agent#2054
Signed-off-by: Patrick East <east.patrick@gmail.com>
patrick-east added a commit that referenced this issue Feb 21, 2020
The documentation is pretty clear that a module must at least contain
a package, so it is safe to say that an empty file isn't valid.

Previously the helper would just return a nil module and nil error, it
will now return an error.

Fixes: #2054
Signed-off-by: Patrick East <east.patrick@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants