Skip to content

Commit

Permalink
fixes #537 by sanitizing metadata while expanding support to numbers
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Corfield <sean@corfield.org>
  • Loading branch information
seancorfield committed Aug 29, 2024
1 parent ae62d2b commit bf34a23
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changes

* 2.6.next in progress
* Address [#537](https://github.com/seancorfield/honeysql/issues/537) by ignoring non-scalar values in metadata, and expanding support to numbers, and checking strings for suspicious characters.
* Address [#536](https://github.com/seancorfield/honeysql/issues/536) by noting what will not work with PostgreSQL (but works with other databases).
* Address [#533](https://github.com/seancorfield/honeysql/issues/533) by adding `honey.sql/*escape-?*` which can be bound to `false` to prevent `?` being escaped to `??` when used as an operator or function.
* Address [#526](https://github.com/seancorfield/honeysql/issues/526) by using `format-var` in DDL, instead of `format-entity`.
Expand Down
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
:1.9 {:override-deps {org.clojure/clojure {:mvn/version "1.9.0"}}}
:1.10 {:override-deps {org.clojure/clojure {:mvn/version "1.10.3"}}}
:1.11 {:override-deps {org.clojure/clojure {:mvn/version "1.11.4"}}}
:1.12 {:override-deps {org.clojure/clojure {:mvn/version "1.12.0-rc1"}}}
:1.12 {:override-deps {org.clojure/clojure {:mvn/version "1.12.0-rc2"}}}

:elide ; to test #409 (assertion on helper docstrings)
{:jvm-opts ["-Dclojure.compiler.elide-meta=[:doc]"]}
Expand Down
34 changes: 24 additions & 10 deletions src/honey/sql.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@
;; #533 mostly undocumented dynvar to prevent ? -> ?? escaping:
(def ^:no-doc ^:dynamic *escape-?* true)

;; suspicious entity names:
(def ^:private suspicious #";")
(defn- suspicious? [s] (boolean (re-find suspicious s)))
(defn- suspicious-entity-check [entity]
(when-not *allow-suspicious-entities*
(when (suspicious? entity)
(throw (ex-info (str "suspicious character found in entity: " entity)
{:disallowed suspicious})))))

;; clause helpers

(defn clause-body
Expand Down Expand Up @@ -308,12 +317,8 @@
[%]
(str/split % #"\."))))
parts (parts-fn col-e)
entity (str/join "." (map #(cond-> % (not= "*" %) (quote-fn)) parts))
suspicious #";"]
(when-not *allow-suspicious-entities*
(when (re-find suspicious entity)
(throw (ex-info (str "suspicious character found in entity: " entity)
{:disallowed suspicious}))))
entity (str/join "." (map #(cond-> % (not= "*" %) (quote-fn)) parts))]
(suspicious-entity-check entity)
entity))

(comment
Expand Down Expand Up @@ -562,9 +567,18 @@
[x & [sep]]
(when-let [data (meta x)]
(let [items (reduce-kv (fn [acc k v]
(if (true? v)
(conj acc k)
(conj acc k v)))
(cond (number? v)
(conj acc (str v))
(true? v)
(conj acc k)
(ident? v)
(conj acc k v)
(string? v)
(do
(suspicious-entity-check v)
(conj acc k v))
:else ; quietly ignore other metadata
acc))
[]
(reduce dissoc
data
Expand All @@ -576,7 +590,7 @@
(str/join (str sep " ") (mapv sql-kw items))))))

(comment
(format-meta ^{:foo true :bar :baz} [])
(format-meta ^{:foo true :bar :baz :original {:line 1} :top 10} [])

(binding [*ignored-metadata* [:bar]]
(format-meta ^{:foo true :bar :baz} []))
Expand Down

0 comments on commit bf34a23

Please sign in to comment.