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

Shared: Add library for filepath normalization #14500

Merged
merged 4 commits into from
Oct 23, 2023

Conversation

joefarebrother
Copy link
Contributor

Adds a library FilePath exposing an abstract class NormalizableFilepath, to be extended to provide strings that should be normalized as filepaths (i.e. transforming a/b/../c to a/c) through the getNormalizedPath member.

@joefarebrother joefarebrother requested a review from a team October 13, 2023 16:22
@joefarebrother joefarebrother requested a review from a team as a code owner October 13, 2023 16:22

private int getNumComponents() { result = strictcount(int i | exists(this.getComponent(i))) }

/** count .. segments in prefix in normalization from index i */
Copy link
Contributor

Choose a reason for hiding this comment

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

I see you simply copied my algorithm suggestion verbatim, but these qldocs could be a bit nicer now that the code is moving from a slack thread and into actual main.

aschackmull
aschackmull previously approved these changes Oct 16, 2023
Copy link
Contributor

@aschackmull aschackmull left a comment

Choose a reason for hiding this comment

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

LGTM

aschackmull
aschackmull previously approved these changes Oct 17, 2023
shared/util/codeql/util/FilePath.qll Outdated Show resolved Hide resolved
shared/util/codeql/util/FilePath.qll Outdated Show resolved Hide resolved
/**
* Gets the normalized filepath for this string.
*
* Normalizes `..` paths, `.` paths, and multiple `/`s as much as possible, but does not normalize case, resolve symlinks, or make relative paths absolute.
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this also note only local paths are handled (correctly)? E.g.: the UNC path //server/public/../private should resolve to //server/public/private (you can't .. out of a share for security reasons), but AFAICT this would resolve it to /server/private.

*
* The normalized path will not have a trailing `/`.
*
* Only `/` is treated as a path separator.
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this mean something e.g. the absolute path C:\foo\bar\baz\../quux would get normalised to quux?

Copy link
Contributor

Choose a reason for hiding this comment

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

Or maybe this warrants a stronger caution that DOS/Windows paths in general are unsupported as AFAICT C:/.. and even C:foo/.. incorrectly resolve to . despite using / separators.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems reasonable to address these cases in the documentation. However as this work was semi-blocking for #14343, and these cases are not relevant for this use case, I have decided to merge anyway. I will address these doc improvements in a follow-up PR.

| a/b/../c | a/c |
| a/b//////c/./d/../e//d// | a/b/c/e/d |
| a/b/c | a/b/c |
| a/b/c/../../../../d/e/../f | ../d/f |
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be good to add a similar test but with an absolute path (e.g. /a/b/c/../../../../d/e/../f should resolve to /d/f.

Copy link
Contributor

@michaelnebel michaelnebel left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@joefarebrother joefarebrother merged commit 3f11d83 into github:main Oct 23, 2023
@aschackmull
Copy link
Contributor

Seems like @sashabu had some reasonable review questions. Shouldn't they at least have been answered before merging?

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

Successfully merging this pull request may close these issues.

6 participants