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

Conversation

Chan-Siu-Man
Copy link
Contributor

@Chan-Siu-Man Chan-Siu-Man commented Oct 25, 2024

This change adds these in files.ncl:

  • File.target validation with std.string.NonEmpty, RelativePath, NoParentTraversal,
  • File.target shell-escaping in regenerate_one.

Before this change, the files field has File with possibly pathological target | String, e.g.:

  • Empty target, i.e., "",
  • Absolute target, e.g., "/etc/passwd",
  • Parent traversals, e.g,. "../../../../../../../etc/passwd".

This change adds schemas to eliminate such cases.

Also, in regenerate_one, we add shell_escape to file_descr.target:

nix-s%"regenerate_function '%{copy_command}' '%{file_descr.file}' $'%{shell_escape file_descr.target}'"%

By quoting with $'…' (dollar-sign and single-quotes) instead of "…" (double-quotes), this correctly handles paths with characters such as ", ', *, \, \n (newline), \t (tabs), $.

This change adds these in `files.ncl`:
- `File.target` validation with `ValidTarget`,
- `File.target` shell-escaping in `regenerate_one`.

Before this change, the `files` field has `File` with possibly
pathological `target | String`, e.g.:
- Empty target, i.e., `""`,
- Absolute target, e.g., `"/etc/passwd"`,
- Parent traversals, e.g,. `"../../../../../../../etc/passwd"`.

This change adds `ValidTarget`, such that `File.target | ValidTarget`
has no such cases.

Also, in `regenerate_one`, we add `shell_escape` to `file_descr.target`:
```
nix-s%"regenerate_function "%{copy_command}" "%{file_descr.file}" "%{shell_escape file_descr.target}""%
```

This correctly handles paths with characters such as `"`, `'`, `*`, `\`,
`\n` (newline), `\t` (tabs).
lib/files.ncl Outdated Show resolved Hide resolved
lib/files.ncl Outdated Show resolved Hide resolved
lib/files.ncl Outdated Show resolved Hide resolved
@yannham yannham requested a review from YorikSar October 25, 2024 15:37
@Chan-Siu-Man Chan-Siu-Man force-pushed the add-file-target-validation branch 2 times, most recently from f21c7e7 to 2c6314d Compare October 26, 2024 01:14
This change addresses [the code review](https://github.com/nickel-lang/organist/pull/222/files/c4cfe488040f5630dfe78104f5648d506e9d334b):
- Use `$'…'` (dollar-sign with *single*-quotes) instead of
  `"…"` (*double*-quotes) for escape shell argument (`file.target`) in
  `regenerate_one`, to handle paths with characters such as `"`, `'`,
  `*`, `\`, `\n` (newline), `\t` (tab), `$`.
- Rename `NoTraveral` to `NoParentTraveral` for more accurate naming.
- Rewrite implementations of contracts `RelativePath` and
  `NoParentTraveral` in idiomatic nickel.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants