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

fix: masking on macos #42

Closed
wants to merge 2 commits into from
Closed

fix: masking on macos #42

wants to merge 2 commits into from

Conversation

ajbt200128
Copy link

MacOS ridiculously symlinks their $TMPDIR = /var/folders/lz/<somestring>/T/
to /private/var/folders/.... This means that if for whatever reason someone prints the real path of a tmp folder instead of the symlinked path, TestO wouldnt mask it.

Also, $TMPDIR on MacOS has a trailing slash for some reason, when on Linux Filename.get_temp_dir_name does not which also broke some things.

Note that I didn't test this on linux, and am hoping there are some CI tests there, but if not will need someone to test it for me

MacOS ridiculously symlinks their `$TMPDIR =
/var/folders/lz/<somestring>/T/`
to `/private/var/folders/...`. This means that if for whatever reason
someone prints the real path of a tmp folder instead of the symlinked
path, TestO wouldnt mask it.

Also, $TMPDIR on MacOS has a trailing slash for some reason, when on
Linux `Filename.get_temp_dir_name` does not which also broke some things
@ajbt200128
Copy link
Author

Apologies for the extra diffs, my ocaml formatter does that for some reason in this repo

Copy link

@kopecs kopecs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these tests in this repo? Would probably best to add some for this if we have them set up.

let real_tmpdir = Unix.realpath tmpdir in
let tmpdir =
if String.ends_with ~suffix:Filename.dir_sep tmpdir then
String.sub tmpdir 0 (String.length tmpdir - 1)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String.sub tmpdir 0 (String.length tmpdir - 1)
String.sub tmpdir 0 (String.length tmpdir - String.length Filename.dir_sep)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably slightly more communicative

Comment on lines +238 to +241
(* Some OS's *cough cough MacOS* symlink their $TMPDIR to a different path *)
(* This means that if an ocmal program ever uses realpath, they may print a valid *)
(* But different temp dir that's not equal to $TMPDIR *)
(* So let's include that in the pattern *)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just 1 comment?

let tmpdir_pat = Re.Pcre.quote tmpdir in
let real_tmpdir_pat = Re.Pcre.quote real_tmpdir in
let tmpdir_pat = Printf.sprintf "(?:%s|%s)" tmpdir_pat real_tmpdir_pat in
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. One issue is that Unix.realpath requires OCaml >= 4.13 which is now 3 years old. It's fine if we really have to but not ideal. String.ends_with also requires OCaml >= 4.13 but is easier to reimplement ourselves.

If I understand correctly, there are two issues:

  1. The trailing slash
  2. Multiple temp folders

For (1), we need tests. In particular, we need to check that "/tmp//" works.

For (2), we also need tests. If depending on OCaml 4.13 is an issue, we could leave this responsibility to the user by letting them specify multiple temp folders. They can actually already do this today by applying two filters: one with the original path obtained with Filename.get_temp_dir_name () and another that is Filename.get_temp_dir_name () |> Unix.realpath.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another issue: String.sub tmpdir 0 (String.length tmpdir - 1) assumes the length of Filename.dir_sep is 1 which is not guaranteed. Better use String.length tmpdir - String.length Filename.dir_sep.

mjambon added a commit to semgrep/semgrep that referenced this pull request Mar 27, 2024
mask temporary paths obtained after applying Unix.realpath which is not
available until ocaml 4.13.
See semgrep/testo#42
mjambon added a commit to semgrep/semgrep that referenced this pull request Mar 27, 2024
)

This is a workaround to the issue of temporary folders being symlinks as
described in
semgrep/testo#42.

This is not done in Testo because I find it too restrictive for Testo to
require OCaml >= 4.13 at this time.

The other bug addressed by semgrep/testo#42
(trailing slashes in temp folder path) will be fixed in Testo.

Test plan: CI checks must pass but symlinked temp folders still fail for
other reasons:
```
$ ln -s /tmp link-to-tmp
$ TMPDIR=$(pwd)/link-to-tmp make test
```
1. If the temp folder is relative, the tests fail because of a chdir.
It's understandable.
2. With an absolute path such as `/home/martin/link-to-tmp`, 12 tests
fail. Here's the error we get for test 1d84c9d09005 (`Language Server
(unit) > Session Targets > Test multiple workspaces with some dirty
(only_git_dirty: false)`):
```
 FAIL: The test raised an exception: Failure: cannot make path "/home/martin/semgrep2/link-to-tmp/test_workspace_9c93dc2d-def6-48f2-84cc-65eeade3e0d2" relative to project root "/tmp/test_workspace_9c93dc2d-def6-48f2-84cc-65eeade3e0d2".
 cwd: /home/martin/semgrep2
 realpath for .: { Rfpath.fpath = .; rpath = (Rpath.Rpath /home/martin/semgrep2);
   cwd = (Rpath.Rpath /home/martin/semgrep2) }
 Sys.argv: ./test
```
It is a known bug that is not simple to fix. See
#10003

Some other language server tests fail with 30-second timeouts. This
should be fixed such that exceptions cause an immediate failure.
@mjambon
Copy link
Member

mjambon commented Mar 28, 2024

I redid this for compatibility with OCaml 4.08 and left the call to Unix.realpath to be the user's responsibility (see semgrep/semgrep#10018) since it requires OCaml >= 4.13 and it's hard to reimplement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants