web-path-handling.scm 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. (define-module (web-path-handling)
  2. #:export (static-assets-dir-name
  3. static-assets-dir-path
  4. safe-path?
  5. static-asset-path?
  6. safe/static-asset-path?
  7. safe/existing/static-asset-path?))
  8. (use-modules
  9. ((srfi srfi-1) #:prefix srfi-1:)
  10. ((logging) #:prefix log:)
  11. (path-handling))
  12. ;; Define some bindings, which are required for security
  13. ;; reasons.
  14. (define static-assets-dir-name "static")
  15. (define static-assets-dir-path (canonicalize-path static-assets-dir-name))
  16. (define safe-path?
  17. (λ (path required-parent-path)
  18. (and
  19. ;; outright refuse to work with a complex path
  20. (not (complex-path? path))
  21. ;; must be inside the static assets directory
  22. (log:debug "subpath? of" (absolute-path path) "and" required-parent-path)
  23. (subpath? (absolute-path path) required-parent-path))))
  24. (define static-asset-path?
  25. (λ (path)
  26. "Check if the path's first part is the static
  27. directory. Warning: This predicate does not guarantee it to
  28. be a safe path."
  29. (let ([path-parts (path-split path)])
  30. (string=? (srfi-1:first path-parts) static-assets-dir-name))))
  31. (define safe/static-asset-path?
  32. (λ (path)
  33. (log:debug "checking whether path is static asset path")
  34. (log:debug "checking for safety of path")
  35. (and (static-asset-path? path)
  36. (safe-path? path static-assets-dir-path))))
  37. (define safe/existing/static-asset-path?
  38. (λ (path)
  39. (log:debug "checking for safety and existence of path")
  40. (and (file-exists? path)
  41. (safe/static-asset-path? path))))