From 87943990005add936f8d2cd0129eea631ff31f0c Mon Sep 17 00:00:00 2001 From: iku000888 Date: Wed, 7 Dec 2016 13:58:02 +0900 Subject: [PATCH 1/6] Replace the form decoder with URLCodec - This addresses the following URLdecoding issue https://github.com/ring-clojure/ring/issues/269 --- src/ring/util/codec.clj | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ring/util/codec.clj b/src/ring/util/codec.clj index de57108..feed5c1 100644 --- a/src/ring/util/codec.clj +++ b/src/ring/util/codec.clj @@ -4,7 +4,8 @@ (:import java.io.File java.util.Map [java.net URLEncoder URLDecoder] - org.apache.commons.codec.binary.Base64)) + org.apache.commons.codec.binary.Base64 + org.apache.commons.codec.net.URLCodec)) (defn assoc-conj "Associate a key with a value in a map. If the key already exists in the map, @@ -112,7 +113,8 @@ or UTF-8 by default." [^String encoded & [encoding]] (try - (URLDecoder/decode encoded (or encoding "UTF-8")) + (let [codec (URLCodec. (or encoding "UTF-8"))] + (.decode codec encoded)) (catch Exception _ nil))) (defn form-decode From 51b52d7a9cf430f50b45f811d2000dbbd25da635 Mon Sep 17 00:00:00 2001 From: iku000888 Date: Sun, 18 Dec 2016 00:49:30 +0900 Subject: [PATCH 2/6] Update the failing test to comply with how commons codec would encode --- test/ring/util/test/codec.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/ring/util/test/codec.clj b/test/ring/util/test/codec.clj index 36e51f6..7063bc6 100644 --- a/test/ring/util/test/codec.clj +++ b/test/ring/util/test/codec.clj @@ -58,5 +58,5 @@ "foo+bar" "foo bar" "a=b+c" {"a" "b c"} "a=b%2Fc" {"a" "b/c"}) - (is (= (form-decode "a=foo%FE%FF%00%2Fbar" "UTF-16") + (is (= (form-decode "%FE%FF%00a=%FE%FF%00f%00o%00o%00%2F%00b%00a%00r" "UTF-16") {"a" "foo/bar"}))) From ad55f5f016a16b713668827d45fa5d12c573494c Mon Sep 17 00:00:00 2001 From: iku000888 Date: Sun, 18 Dec 2016 02:26:38 +0900 Subject: [PATCH 3/6] Replace the implementation of url-decode and url-encode - test data has been updated accordingly --- src/ring/util/codec.clj | 9 ++++----- test/ring/util/test/codec.clj | 29 +++++++++-------------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/ring/util/codec.clj b/src/ring/util/codec.clj index feed5c1..340a9b4 100644 --- a/src/ring/util/codec.clj +++ b/src/ring/util/codec.clj @@ -58,16 +58,15 @@ "Returns the url-encoded version of the given string, using either a specified encoding or UTF-8 by default." [unencoded & [encoding]] - (str/replace - unencoded - #"[^A-Za-z0-9_~.+-]+" - #(double-escape (percent-encode % encoding)))) + (let [codec (URLCodec. (or encoding "UTF-8"))] + (.encode codec unencoded))) (defn ^String url-decode "Returns the url-decoded version of the given string, using either a specified encoding or UTF-8 by default. If the encoding is invalid, nil is returned." [encoded & [encoding]] - (percent-decode encoded encoding)) + (let [codec (URLCodec. (or encoding "UTF-8"))] + (.decode codec encoded))) (defn base64-encode "Encode an array of bytes into a base64 encoded string." diff --git a/test/ring/util/test/codec.clj b/test/ring/util/test/codec.clj index 7063bc6..fb76087 100644 --- a/test/ring/util/test/codec.clj +++ b/test/ring/util/test/codec.clj @@ -1,30 +1,19 @@ (ns ring.util.test.codec - (:use clojure.test - ring.util.codec) - (:import java.util.Arrays)) - -(deftest test-percent-encode - (is (= (percent-encode " ") "%20")) - (is (= (percent-encode "+") "%2B")) - (is (= (percent-encode "foo") "%66%6F%6F"))) - -(deftest test-percent-decode - (is (= (percent-decode "%s/") "%s/")) - (is (= (percent-decode "%20") " ")) - (is (= (percent-decode "foo%20bar") "foo bar")) - (is (= (percent-decode "foo%FE%FF%00%2Fbar" "UTF-16") "foo/bar")) - (is (= (percent-decode "%24") "$"))) + (:require [clojure.test :refer :all] + [ring.util.codec :refer :all]) + (:import java.util.Arrays + org.apache.commons.codec.DecoderException)) (deftest test-url-encode (is (= (url-encode "foo/bar") "foo%2Fbar")) - (is (= (url-encode "foo/bar" "UTF-16") "foo%FE%FF%00%2Fbar")) - (is (= (url-encode "foo+bar") "foo+bar")) - (is (= (url-encode "foo bar") "foo%20bar"))) + (is (= (url-encode "foo/bar" "UTF-16") "%FE%FF%00f%00o%00o%00%2F%00b%00a%00r")) + (is (= (url-encode "foo+bar") "foo%2Bbar")) + (is (= (url-encode "foo bar") "foo+bar"))) (deftest test-url-decode (is (= (url-decode "foo%2Fbar") "foo/bar" )) - (is (= (url-decode "foo%FE%FF%00%2Fbar" "UTF-16") "foo/bar")) - (is (= (url-decode "%") "%"))) + (is (= (url-decode "%FE%FF%00f%00o%00o%00%2F%00b%00a%00r" "UTF-16") "foo/bar")) + (is (thrown? DecoderException (url-decode "%")))) (deftest test-base64-encoding (let [str-bytes (.getBytes "foo?/+" "UTF-8")] From 95844107b736b5cd8728c2d8434b832c9857c1ad Mon Sep 17 00:00:00 2001 From: iku000888 Date: Sun, 18 Dec 2016 02:36:13 +0900 Subject: [PATCH 4/6] Re-run travis From 1785df027cf5e044c855b4c42764316228fedaf2 Mon Sep 17 00:00:00 2001 From: iku000888 Date: Sun, 18 Dec 2016 02:46:35 +0900 Subject: [PATCH 5/6] Remove dev profile in an attempt to fix travis test --- project.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project.clj b/project.clj index 7cd47c7..19c0e10 100644 --- a/project.clj +++ b/project.clj @@ -11,7 +11,7 @@ :source-uri "http://github.com/ring-clojure/ring-codec/blob/{version}/{filepath}#L{line}"} :aliases {"test-all" ["with-profile" "default:+1.4:+1.5:+1.6:+1.7:+1.8" "test"]} :profiles - {:dev {:dependencies [[criterium "0.4.4"]]} + {;;:dev {:dependencies [[criterium "0.4.4"]]} :1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]} :1.5 {:dependencies [[org.clojure/clojure "1.5.1"]]} :1.6 {:dependencies [[org.clojure/clojure "1.6.0"]]} From 614d64bdcac0cbfb8a7753fee33987b35c16a7c1 Mon Sep 17 00:00:00 2001 From: iku000888 Date: Sun, 18 Dec 2016 02:59:06 +0900 Subject: [PATCH 6/6] Do not test with default profile --- project.clj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project.clj b/project.clj index 19c0e10..7d9cd77 100644 --- a/project.clj +++ b/project.clj @@ -9,9 +9,9 @@ :codox {:output-path "codox" :source-uri "http://github.com/ring-clojure/ring-codec/blob/{version}/{filepath}#L{line}"} - :aliases {"test-all" ["with-profile" "default:+1.4:+1.5:+1.6:+1.7:+1.8" "test"]} + :aliases {"test-all" ["with-profile" "+1.4:+1.5:+1.6:+1.7:+1.8" "test"]} :profiles - {;;:dev {:dependencies [[criterium "0.4.4"]]} + {:dev {:dependencies [[criterium "0.4.4"]]} :1.4 {:dependencies [[org.clojure/clojure "1.4.0"]]} :1.5 {:dependencies [[org.clojure/clojure "1.5.1"]]} :1.6 {:dependencies [[org.clojure/clojure "1.6.0"]]}