Common Lisp define is a directory that is a subdirectory or other directory
I am using hunchentoot to create a simple web application to open a directory tree on the internet. The problem I am facing is finding a reliable and secure way to determine if the requested directory is actually a child directory *share-root*
, which is /srv/share
.
I spent time cl-fad, but it's not exactly what I need (or I am not using it in a way that solves my problem).
My goal is to be able to get a path like: /srv/share/media/../../../
and understand that the request should be ignored because it is requesting something outside of the share.
source to share
I suggest enough-namestring
in combination with truename
: if
(enough-namestring foo bar)
is a relative path, then foo
is under bar
. In other words:
(defun pathname-under-p (under top)
(case (car (pathname-directory (enough-namestring (truename under)
(truename top))))
((nil :relative) t)
(t nil)))
or simply
(defun pathname-under-p (under top)
(not (eq :absolute (car (pathname-directory (enough-namestring (truename under)
(truename top)))))))
If your implementation does not support truename
in the directory, you will have to use the implementation-defined function or directory
.
Many thanks to @Svante for debugging.
source to share