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

sanitize possible html value injections in all value renders #89

Merged
merged 10 commits into from
Feb 16, 2024
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
tailrecursion/cljs-priority-map {:mvn/version "1.2.1"
:exclusions [org.clojure/clojure
org.clojure/clojurescript]}
hiccup/hiccup {:mvn/version "1.0.5"}
hiccup/hiccup {:mvn/version "2.0.0-RC3"}
hiccups/hiccups {:mvn/version "0.3.0"}}
:aliases
{:test-cljs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(ns com.yetanalytics.lrs.pedestal.routes.statements.html
(:require #?@(:clj [[hiccup.core :as html]
(:require #?@(:clj [[hiccup2.core :as html]
[clojure.java.io :as io]]
:cljs [[hiccups.runtime :as hic]
[goog.string :refer [format]]
Expand Down Expand Up @@ -118,9 +118,10 @@
(cs/join "\n"
(map
(fn [hvec]
(#?(:clj html/html
:cljs hic/render-html)
hvec))
(str
(#?(:clj html/html
:cljs hic/render-html)
hvec)))
hvecs))))

(defn actor-pred
Expand Down Expand Up @@ -351,7 +352,7 @@
:truncate-after-mod -9
:url-params params)]
(if (unwrap? ctx)
#?(:clj (html/html statement-rendered)
#?(:clj (str (html/html statement-rendered))
:cljs (hic/render-html statement-rendered))
(page head
[:body
Expand Down Expand Up @@ -419,7 +420,7 @@
(inject-ascending
path-prefix params))]
(if (unwrap? ctx)
#?(:clj (html/html statement-response-rendered)
#?(:clj (str (html/html statement-response-rendered))
:cljs (hic/render-html statement-response-rendered))
(page
head
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
(web-link? href)
(assoc :target "_blank")))1

(defn escaped-html-str
"Change special characters into HTML character entities."
[text]
(.. (str text)
(replace "&" "&")
(replace "<" "&lt;")
(replace ">" "&gt;")
(replace "\"" "&quot;")
(replace "'" "&#39;")))

(defn a
[link text]
[:a
Expand All @@ -65,7 +75,8 @@
(reduce-kv
(fn [m k v]
(let [kw (keyword nil (format "data-%s" (name k)))]
(assoc m kw v)))
(assoc m kw #?(:clj v
:cljs (escaped-html-str v)))))
(empty data)
data))

Expand Down Expand Up @@ -153,7 +164,8 @@
(sort-by #(get key-weights (first %) 0) >)
(map-indexed
(fn coerce-kv [idx [k v]]
(let [kn (name k)
(let [kn #?(:clj (name k)
:cljs (escaped-html-str (name k)))
scalar? (and (not (rendered? v))
(or (link-tuple? v)
(not (coll? v))))
Expand Down Expand Up @@ -266,4 +278,5 @@
(if (and (string? json)
(linky? json))
(a json json)
(str json))])))))
#?(:clj (str json)
:cljs (escaped-html-str json)))])))))
Loading
Loading