Directory traversal check with Lisp
I want to write code to check directory traversal. In other words, replace ".." with other values if strings includes "..". For example, the string "/aaa/../index.html" should be replaced with "/index.html".
And I want to write this code as a recursive function. Here is a base.
(defun replace-double-dott (x r) (if (null x) r (let ((n (first x)) (o (rest x))) (if (equal n "..") (setf r (tail-remove r)) (setf r (append r (list n)))) (replace-double-dott o r))))
Here is a result.
? (replace-double-dott (split "/aaa/bbb/../../index.html" "/") nil) ("index.html") ? (replace-double-dott (split "/aaa/bbb/../../../index.html" "/") nil) ("index.html") ? (replace-double-dott (split "/aaa/../bbb/../ccc/index.html" "/") nil) ("ccc" "index.html") ?