Improved massage-params

This commit is contained in:
Simon Brooke 2018-07-24 17:53:58 +01:00
parent 710bfbef81
commit e17a79e7c7
2 changed files with 36 additions and 21 deletions

View file

@ -5,6 +5,7 @@
:url "https://opensource.org/licenses/MIT"} :url "https://opensource.org/licenses/MIT"}
:dependencies [[org.clojure/clojure "1.8.0"] :dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/core.memoize "0.7.1"]
[org.clojure/math.numeric-tower "0.0.4"] [org.clojure/math.numeric-tower "0.0.4"]
[org.clojure/tools.logging "0.3.1"] [org.clojure/tools.logging "0.3.1"]
[selmer "1.10.6"]] [selmer "1.10.6"]]

View file

@ -1,5 +1,6 @@
(ns adl-support.core (ns adl-support.core
(:require [clojure.java.io :as io] (:require [clojure.core.memoize :as memo]
[clojure.java.io :as io]
[clojure.string :refer [split]])) [clojure.string :refer [split]]))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -66,13 +67,13 @@
{(keyword k) v}))) {(keyword k) v})))
(defn massage-params (defn raw-massage-params
"Sending empty strings, or numbers as strings, to the database often isn't "Sending empty strings, or numbers as strings, to the database often isn't
helpful. Massage these `params` and `form-params` to eliminate these problems. helpful. Massage these `params` and `form-params` to eliminate these problems.
We must take key field values out of just params, but we should take all other We must take key field values out of just params, but we should take all other
values out of form-params - because we need the key to load the form in values out of form-params - because we need the key to load the form in
the first place, but just accepting values of other params would allow spoofing." the first place, but just accepting values of other params would allow spoofing."
[params form-params key-fields] ([params form-params key-fields]
(let (let
[ks (set (map keyword key-fields))] [ks (set (map keyword key-fields))]
(reduce (reduce
@ -91,6 +92,19 @@
(map (map
#(massage-value % form-params) #(massage-value % form-params)
(keys form-params))))) (keys form-params)))))
([request key-fields]
(raw-massage-params (:params request) (:form-params request) key-fields))
([request]
(raw-massage-params (:params request) (:form-params request) #{})))
(def massage-params
"Sending empty strings, or numbers as strings, to the database often isn't
helpful. Massage these `params` and `form-params` to eliminate these problems.
We must take key field values out of just params, but we should take all other
values out of form-params - because we need the key to load the form in
the first place, but just accepting values of other params would allow spoofing."
(memo/ttl raw-massage-params {} :ttl/threshold 5000))
(defn (defn