Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add File.target validation and escaping #222

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions lib/files.ncl
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
let nix = import "./nix-interop/nix.ncl" in
let RelativePath =
std.contract.from_predicate
(
fun x =>
x
|> std.string.characters
|> std.array.first != "/"
)
in
let NoParentTraversal =
std.contract.from_predicate
(
fun x =>
x
|> std.string.split "/"
|> std.array.all ((!=) "..")
)
in
let File = {
target
| doc m%"
The file to write to.
If null, defaults to the attribute name of the file.
"%
| String
| std.string.NonEmpty # avoids ""
| RelativePath # avoids "/etc/passwd"
| NoParentTraversal # avoids "../../../../../../../etc/passwd"
| optional,
content
| doc m%"
Expand Down Expand Up @@ -72,7 +92,12 @@ let regenerate_files | Files -> nix.derivation.Derivation
}
file_descr.materialisation_method
in
nix-s%"regenerate_function "%{copy_command}" "%{file_descr.file}" "%{file_descr.target}""%
let shell_escape = fun path =>
path
|> std.string.replace "\\" "\\\\"
|> std.string.replace m%"'"% m%"\'"%
in
nix-s%"regenerate_function '%{copy_command}' '%{file_descr.file}' $'%{shell_escape file_descr.target}'"%
in
let regenerate_files = nix-s%"
%{regenerate_function}
Expand Down
Loading