8000 add our audit-middleware and check that it throws when we try to add-… by ajmers · Pull Request #1 · ajmers/nrepl · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add our audit-middleware and check that it throws when we try to add-… #1

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/clojure/nrepl/middleware/session.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@

(def ^{:private true} sessions (atom {}))

(defn audit-activity
"Middleware constructor which wraps any eval calls to watch local activity."
[handler]
(fn wrapper
[msg]
(println "[NREPL-AUDIT] Op:" (:op msg))
(let [disallowed-ops #{"add-middleware" "swap-middleware"}
log-ops #{"eval" "load-file" "stdin" "ls-middleware"}
op (:op msg)]
(when (contains? disallowed-ops (:op msg))
(throw (ex-info "Changing middleware in a service nREPL is not allowed"
{:op (:op msg)
:msg msg})))
(when (contains? log-ops (:op msg))
(case op
"eval" (println "[NREPL-AUDIT]" (pr-str op (:code msg)))
"load-file" (println "[NREPL-AUDIT]" (pr-str op (:file msg)))
"ls-middleware" (println "[NREPL-AUDIT]" (pr-str op msg))
(println "[NREPL-AUDIT]" (pr-str msg)))))
(handler msg)))

(defn close-all-sessions!
"Use this fn to manually shut down all sessions. Since each new session spanws
a new thread, and sessions need to be otherwise explicitly closed, we can
Expand Down
30 changes: 30 additions & 0 deletions test/clojure/nrepl/middleware/dynamic_loader_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,36 @@
(combine-responses @resps#)))]
~@body)))



(deftest wrap-dynamic-loader-test-with-audit
(testing-dynamic "Audit middleware prevents add-middleware from working (throws)"
(handle {:op "add-middleware"
:middleware ["nrepl.middleware.session/audit-activity"]})
(is (thrown? Exception (handle {:op "add-middleware"
:middleware ["nrepl.middleware.session/audit-activity"]})))
(is (= ["#'nrepl.middleware.dynamic-loader/wrap-dynamic-loader"
"#'nrepl.middleware.session/session"
"#'nrepl.middleware.session/audit-activity"]
(:middleware (handle {:op "ls-middleware"})))))
(testing-dynamic "Audit middleware prevents Swap-middleware from working (throws)"
(handle {:op "add-middleware"
:middleware ["#'nrepl.middleware.session/audit-activity"]})
(is (= 3 ;; now we have all these: session eval print caught dynamic
(count (:middleware (handle {:op "ls-middleware"})))))
(is (thrown? Exception
(handle {:op "swap-middleware"
:middleware ["nrepl.middleware.dynamic-loader/wrap-dynamic-loader"]}) ))

(let [ls-result (:middleware (handle {:op "ls-middleware"}))]
(is (= 3 (count ls-result)))
;; wrap-dynamic-loader *requires* session, that's why session is here even though we didn't add it specifically
(is (= (set ls-result)
(set ["#'nrepl.middleware.session/audit-activity"
"#'nrepl.middleware.dynamic-loader/wrap-dynamic-loader"
"#'nrepl.middleware.session/session"
]))))))

(deftest wrap-dynamic-loader-test
(testing-dynamic "Booting with no middleware"
(is (= ["#'nrepl.middleware.dynamic-loader/wrap-dynamic-loader"]
Expand Down
0