Skip to content

Commit

Permalink
Added protection against malformed file paths as reported by Meredydd
Browse files Browse the repository at this point in the history
Luff
  • Loading branch information
weavejester committed Sep 13, 2009
1 parent fda9824 commit 81fae95
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/compojure/http/helpers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
#(.startsWith (.toLowerCase (.getName %)) "index.")
(.listFiles dir))))

(defn safe-path?
"Is a filepath safe for a particular root?"
[root path]
(.startsWith (.getCanonicalPath (File. root path))
(.getCanonicalPath (File. root))))

(defn serve-file
"Attempts to serve up a static file from a directory, which defaults to
'./public'. Nil is returned if the file does not exist. If the file is a
Expand All @@ -62,8 +68,9 @@
(serve-file "public" path))
([root path]
(let [filepath (File. root path)]
(cond
(.isFile filepath)
filepath
(.isDirectory filepath)
(find-index-file filepath)))))
(if (safe-path? root path)
(cond
(.isFile filepath)
filepath
(.isDirectory filepath)
(find-index-file filepath))))))
4 changes: 4 additions & 0 deletions test/compojure/http/helpers.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
(deftest test-content-type
(is (= (content-type "text/html")
{:headers {"Content-Type" "text/html"}})))

(deftest test-safe-path
(is (not (safe-path? "/home/compojure" "../private/secret.txt")))
(is (safe-path? "/home/compojure" "public/index.html")))

0 comments on commit 81fae95

Please sign in to comment.