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

Don't serve forbidden paths when :paths are closed #572

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/nextjournal/clerk/paths.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
(or (not (string? index)) (not (fs/exists? index))))
{:error "`:index` must be either an instance of java.net.URL or a string and point to an existing file"
:index index}
(cond-> opts
(cond-> (merge (select-keys build-opts [:paths :paths-fn :index]) opts)
(and index (not (contains? (set expanded-paths) index)))
(update :expanded-paths conj index)))))

Expand Down Expand Up @@ -86,7 +86,7 @@
(ensure-not-empty build-opts)))))

#_(expand-paths {:paths ["notebooks/di*.clj"] :index "src/nextjournal/clerk/index.clj"})
#_(expand-paths {:paths ['notebooks/rule_30.clj]})
#_(expand-paths {:paths ['notebooks/rule_30.clj] :index "notebooks/markdown.md"})
#_(expand-paths {:index "book.clj"})
#_(expand-paths {:paths-fn `nextjournal.clerk.builder/clerk-docs})
#_(expand-paths {:paths-fn `clerk-docs-2})
Expand Down Expand Up @@ -129,6 +129,7 @@
(git/read-git-attrs)))

#_(process-paths {:paths ["notebooks/rule_30.clj"]})
#_(process-paths {:paths ["notebooks/rule_30.clj"] :index "notebooks/links.md"})
#_(process-paths {:paths ["notebooks/no_rule_30.clj"]})
#_(v/route-index? (process-paths @!server))
#_(route-index (process-paths @!server) "")
Expand Down
34 changes: 23 additions & 11 deletions src/nextjournal/clerk/webserver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@
(defn get-build-opts []
(paths/process-paths @!server))

#_(get-build-opts)

(defn ->nav-path [file-or-ns]
(cond (or (= 'nextjournal.clerk.index file-or-ns)
(= (:index (get-build-opts)) file-or-ns))
Expand Down Expand Up @@ -214,8 +216,14 @@
(str/starts-with? nav-path "'") (symbol (subs nav-path 1))
(re-find #"\.(cljc?|md)$" nav-path) nav-path))

(defn forbidden-path? [file-or-ns]
(if-let [expanded-paths (:expanded-paths (get-build-opts))]
(not (contains? (conj (set expanded-paths) 'nextjournal.clerk.index) file-or-ns))
false))

(defn show! [opts file-or-ns]
((resolve 'nextjournal.clerk/show!) opts file-or-ns))
(when-not (forbidden-path? file-or-ns)
((resolve 'nextjournal.clerk/show!) opts file-or-ns)))

(defn route-index
"A routing function"
Expand Down Expand Up @@ -248,16 +256,19 @@
:headers {"Location" (or (:nav-path @!doc)
(->nav-path 'nextjournal.clerk.home))}}
:else
(if-let [file-or-ns (->file-or-ns (maybe-add-extension nav-path))]
(do (try (show! (merge {:skip-history? true}
(select-keys opts [:expanded-paths :index :git/sha :git/url]))
file-or-ns)
(catch Exception _))
{:status 200
:headers {"Content-Type" "text/html" "Cache-Control" "no-store"}
:body (view/->html {:doc (view/doc->viewer @!doc)
:resource->url @config/!resource->url
:conn-ws? true})})
(if-let [file-or-ns (let [file-or-ns (->file-or-ns (maybe-add-extension nav-path))]
(when-not (forbidden-path? file-or-ns)
file-or-ns))]
(do
(try (show! (merge {:skip-history? true}
(select-keys opts [:expanded-paths :index :git/sha :git/url]))
file-or-ns)
(catch Exception _))
{:status 200
:headers {"Content-Type" "text/html" "Cache-Control" "no-store"}
:body (view/->html {:doc (view/doc->viewer @!doc)
:resource->url @config/!resource->url
:conn-ws? true})})
{:status 404
:headers {"Content-Type" "text/plain"}
:body (format "Could not find notebook at %s." (pr-str nav-path))}))))
Expand Down Expand Up @@ -327,6 +338,7 @@

#_(serve! {:port 7777})
#_(serve! {:port 7777 :paths ["notebooks/rule_30.clj"]})
#_(serve! {:port 7777 :paths ["notebooks/rule_30.clj"] :index "notebooks/links.md"})
#_(serve! {:port 7777 :paths ["notebooks/rule_30.clj" "book.clj"]})
#_(serve! {:port 7777 :paths ["notebooks/rule_30.clj" "notebooks/links.md" "notebooks/markdown.md" "index.clj"]})
#_(serve! {:port 7777 :host "0.0.0.0"})
Loading