Skip to content

Commit 1aaf1b6

Browse files
committed
logic
1 parent 1436d99 commit 1aaf1b6

File tree

2 files changed

+38
-0
lines changed
  • search-app

2 files changed

+38
-0
lines changed

search-app/src/cmr/search/services/parameters/converters/shapefile.clj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@
5252
"application/vnd.google-earth.kml+xml" {}
5353
"application/geo+json" {:hole-winding :cw}))
5454

55+
(defn validate-entry-dir
56+
[target-dir entry]
57+
(let [canonical (.getCanonicalPath (File. (.toString target-dir) (str entry)))]
58+
(when-not (.startsWith canonical (.getCanonicalPath (.toFile target-dir)))
59+
(throw (Exception. "Given zip content is not allowed")))))
60+
5561
(defn unzip-file
5662
"Unzip a file (of type File) into a temporary directory and return the directory path as a File"
5763
[source]
@@ -62,6 +68,7 @@
6268
target-file #(File. (.toString target-dir) (str %))]
6369
(doseq [entry entries :when (not (.isDirectory ^java.util.zip.ZipEntry entry))
6470
:let [f (target-file entry)]]
71+
(validate-entry-dir target-dir entry)
6572
(debug (format "Zip file entry: [%s]" (.getName entry)))
6673
(io/copy (.getInputStream zip entry) f))))
6774
(.toFile target-dir)

search-app/test/cmr/search/test/unit/services/parameter_converters/shapefile.clj

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
(java.nio.file.attribute FileAttribute)
1414
(java.net URL)
1515
(java.util HashMap)
16+
(java.util.zip ZipEntry ZipOutputStream ZipInputStream)
1617
(java.util.zip ZipFile ZipInputStream)
1718
(org.apache.commons.io FilenameUtils)
1819
(org.geotools.data DataStoreFinder FileDataStoreFinder Query)
@@ -108,3 +109,33 @@
108109

109110
"UTM ZONE 11N"
110111
"EPSG:2955" false [-121.48866759617566 2.8851809782082726E-4]))))
112+
113+
(defn create-single-entry-zip [filename entry-name content]
114+
(let [file-obj (io/file filename)]
115+
(with-open [zos (ZipOutputStream. (io/output-stream file-obj))]
116+
(let [entry (ZipEntry. entry-name)]
117+
(.putNextEntry zos entry)
118+
(.write zos (.getBytes content "UTF-8"))
119+
(.closeEntry zos)))
120+
file-obj))
121+
122+
(deftest unzip-file-test
123+
(testing "invalid file path in the source throws error"
124+
(let [invalid-zip (create-single-entry-zip "invalid.zip" "../somewhere_else.txt" "something")]
125+
(try
126+
(is (thrown-with-msg? Exception #"Given zip content is not allowed" (shapefile/unzip-file invalid-zip)))
127+
(finally
128+
(io/delete-file invalid-zip true)))))
129+
(testing "invalid file path within the tar dir throws error"
130+
(let [invalid-zip (create-single-entry-zip "wrong-dir.zip" "/BLAH/somewhere_else.txt" "something")]
131+
(try
132+
(is (thrown-with-msg? Exception #"Error while uncompressing zip file.*No such file or directory" (shapefile/unzip-file invalid-zip)))
133+
(finally
134+
(io/delete-file invalid-zip true)))))
135+
(testing "valid zip file returns file obj"
136+
(let [valid-zip (create-single-entry-zip "valid.zip" "somewhere_else.txt" "something")
137+
result (shapefile/unzip-file valid-zip)]
138+
(try
139+
(is (some? result))
140+
(finally
141+
(io/delete-file valid-zip true))))))

0 commit comments

Comments
 (0)