-
Notifications
You must be signed in to change notification settings - Fork 4
ROX-32841: Quote args #295
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import os | ||
| import re | ||
| from re import Pattern | ||
| import string | ||
| from enum import Enum | ||
|
|
@@ -25,6 +26,19 @@ def extract_container_id(cgroup: str) -> str: | |
| else: | ||
| return '' | ||
|
|
||
| def rust_style_quote(s): | ||
| if not s: | ||
| return "''" | ||
| if re.search(r'[^a-zA-Z0-9_./-]', s): | ||
| # Try to match the behavior of shlex.try_join() | ||
| if '\'' in s and not '"' in s: | ||
| return f'"{s}"' | ||
| escaped = s.replace("'", "\\'") | ||
| return f"'{escaped}'" | ||
| return s | ||
|
|
||
| def rust_style_join(args): | ||
| return ' '.join(rust_style_quote(arg) for arg in args) | ||
|
Comment on lines
+29
to
+41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tried using Python's shlex and see if it behaves the same way as the Rust implementation? It would be a lot easier for tests.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is what I used initially, and their quoting strategy is vastly different, unfortunately.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, "vastly" was maybe a bit too strong : the "safe" characters list differ, in particular rust considers '=' and ':' unsafe. This behavior is not tunable AFAICT, so I resorted to mimic things on the testing side.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you put a comment detailing this so the next person doesn't try to use Python's shlex? |
||
|
|
||
| class EventType(Enum): | ||
| """Enumeration for different types of file activity events.""" | ||
|
|
@@ -85,7 +99,7 @@ def get_id(line: str, wanted_id: str) -> int | None: | |
| content = f.read(4096) | ||
| args = [arg.decode('utf-8') | ||
| for arg in content.split(b'\x00') if arg] | ||
| args = ' '.join(args) | ||
| args = rust_style_join(args) | ||
|
|
||
| with open(os.path.join(proc_dir, 'comm'), 'r') as f: | ||
| name = f.read().strip() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the parsing of arguments received from the kernel is done by searching for null characters, I don't think it is possible for
shlex::try_jointo fail here, because we've already removed them all. If that's the case, this might be better:Probably also want to update the comment to reflect this.