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

Implement restrictions #16

Open
1 of 2 tasks
oxalorg opened this issue Jul 10, 2021 · 15 comments
Open
1 of 2 tasks

Implement restrictions #16

oxalorg opened this issue Jul 10, 2021 · 15 comments
Labels
enhancement New feature or request

Comments

@oxalorg
Copy link
Owner

oxalorg commented Jul 10, 2021

@pradeepbishnoi
Copy link
Contributor

pradeepbishnoi commented Jul 11, 2021

To :deny use below example as reference
(eval-string "(first '(1 2 3 4))" {:deny '[inc dec first]})

@pradeepbishnoi
Copy link
Contributor

@borkdude Any suggestion how to convert this into a quote for
Tried few things however not able to convert it.
(first `(~(mapv symbol ["inc" "dec" "last"])))

above expression give a vector with symbol. however want to convert this into a quoted collection for input to :deny.

image

@borkdude
Copy link
Collaborator

Just (mapv symbol ...) should do the trick? Why are you using the backtick?

@pradeepbishnoi
Copy link
Contributor

pradeepbishnoi commented Jul 11, 2021

@borkdude Yes, it does. I over thought on this one and didn't realise that :deny is just needing a vector. Using the backpack I was trying to create a "quoted" statement to pass 🤦

@oxalorg oxalorg added the enhancement New feature or request label Jul 11, 2021
oxalorg added a commit that referenced this issue Jul 19, 2021
@Invertisment
Copy link
Collaborator

Invertisment commented Jan 28, 2023

You're overcomplicating things.
The thing that the user enters is an EDN string.

I wrote this function by trying to solve this problem and as I currently don't yet have a proper branch to merge it from so I post it here:

(defn find-restricted-symbols [restricted-symbol-strings user-input]
  (let [restricted-symbols (set (map symbol restricted-symbol-strings))]
    (->> user-input
         clojure.edn/read-string
         vector
         (clojure.walk/prewalk (fn [form]
                                 (cond (map? form) (concat (keys form) (vals form))
                                       (set? form) (seq form)
                                       :else form)))
         flatten
         set
         (clojure.set/intersection restricted-symbols)
         seq)))

#_(find-restricted-symbols ["flatten" "into"] "(flatten (into [] [[1]]))")
#_(find-restricted-symbols ["flatten" "into"] "#{:my-set-item flatten}")
#_(find-restricted-symbols ["flatten" "into"] "{}")
#_(find-restricted-symbols ["flatten" "into"] "{flatten []}")
#_(find-restricted-symbols ["flatten" "into"] "{flatten {:my-map-key into} }")
#_(find-restricted-symbols ["flatten" "into"] "{flatten {:my-map-key #{[into]}} }")
#_(find-restricted-symbols ["flatten" "into"] "flatten")
#_(find-restricted-symbols ["max" "max-key"] "[max \"max-key\"]")

@borkdude
Copy link
Collaborator

What about just using str/includes? on the input string? ;)

@Invertisment
Copy link
Collaborator

Invertisment commented Jan 28, 2023

What about just using str/includes? on the input string? ;)

Yes it would work but my function distinguishes between "max-key" and max-key

@borkdude
Copy link
Collaborator

No, that would give false positives on locals with that name. The :deny option in SCI was built to do this correctly, only on var names.

@Invertisment
Copy link
Collaborator

My function would fail on (fn max-key []...), true. And NS resolution wouldn't help.

Would SCI matching recover from this correctly?

@borkdude
Copy link
Collaborator

yes

@borkdude
Copy link
Collaborator

user=> (sci/eval-string "((fn max-key [] 1))" {:deny '[max-key]})
1
user=> (sci/eval-string "(max-key (fn max-key [] 1))" {:deny '[max-key]})
Execution error (ExceptionInfo) at sci.impl.utils/throw-error-with-location (utils.cljc:41).
max-key is not allowed!

@Invertisment
Copy link
Collaborator

Should it still evaluate and show the results together with restriction error?

@borkdude
Copy link
Collaborator

@Invertisment Maybe we can make restricting a setting like you did for the editor settings. In case an error happens with restrictions, we want to offer the ability to turn that off.

@Invertisment
Copy link
Collaborator

Invertisment commented Jan 29, 2023

Do you mean that you would expect to accept range as solution to "implement range" if this kind of option would be false?
I was thinking about simply showing additional message.

Also if restriction check wouldn't be part of evaluator then we could do this check elsewhere.
But yes. If it would be part of evaluator then we would either need to evaluate it twice or only show either an error or result but nothing else.

@borkdude
Copy link
Collaborator

Evaluating twice also seems fine, in case the restriction is hit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants