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

var x is unsafe #34

Closed
kirk-patton opened this issue Nov 20, 2020 · 1 comment
Closed

var x is unsafe #34

kirk-patton opened this issue Nov 20, 2020 · 1 comment

Comments

@kirk-patton
Copy link

I am finding that I can examine some variables and not others when I used the key binding OPA: Evaluate Selection.

example[t] {
	x := {"a":"b"}
	t := x
}

if x := {"a":"b"} is selected and OPA: Evaluate Selection is run, I get

// Found 1 result in 20.247µs using input.json as input.
[
  {
    "x": {
      "a": "b"
    }
  }
]

If t := x is selected and OPA: Evaluate Selection is run, I get
: rego_unsafe_var_error: var x is unsafe

If I select example[t], and OPA: Evaluate Selection is run, I get

[
  [
    [
      {
        "a": "b"
      }
    ]
  ]
]

I don't understand why I get the var is unsafe message. Is this a bug?

@tsandall
Copy link
Member

When you select expressions inside of VS Code and run OPA: Evaluate Selection, the VS Code plugin is running a query against the policy. For example, if you select x := {"a": "b"} and evaluate it, the plugin essentially runs...

opa eval -b <workspace-dir> 'x := {"a": "b"}'

And then it prints the result.

In this case, the query is x := {"a": "b"}. That query is syntactically and semantically valid. On the other hand, if you only select t := x while syntactically valid, it's not semantically valid as there's no assignment to the variable x (which makes it unsafe). See https://www.openpolicyagent.org/docs/latest/faq/#safety for more info on the safety concept.

As you discovered you can select individual expressions as well as rule names. You can also select multiple expressions. If you select both lines in the rule body, the query should evaluate. In that case, the equivalent opa eval invocation would be (essentially):

opa eval -b <workspace-dir> 'x := {"a": "b"}; t := x'

Hope this helps!

@tsandall tsandall added question Further information is requested and removed question Further information is requested labels Nov 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants