-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify search for iptables executable (#382)
* fix safe exec * fix black format * iptables simplify * iptables search * Remove unused import
- Loading branch information
1 parent
2509734
commit fd3c748
Showing
5 changed files
with
19 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,5 @@ | ||
import os | ||
import subprocess | ||
|
||
__version__ = "0.9.5" | ||
|
||
from os import PathLike | ||
from typing import Union | ||
|
||
BIN_LOCATIONS = ["/usr/bin", "/bin", "/usr/sbin", "/sbin"] | ||
|
||
|
||
def _check_file_exists_and_executable(path: Union[PathLike, str]) -> bool: | ||
if not os.path.isfile(path): | ||
return False | ||
else: | ||
return os.access(path, os.X_OK) | ||
|
||
|
||
def safe_exec(binary_name: str, args: list) -> bytes: | ||
""" | ||
Executes the given binary with the given arguments as a subprocess. What makes this safe is that the binary name | ||
is not executed as an alias, and only binaries that live in trusted system locations are executed. This means that | ||
only system-wide binaries are executable. | ||
""" | ||
exec_path = None | ||
for prefix in BIN_LOCATIONS: | ||
bin_path = os.path.join(prefix, binary_name) | ||
if _check_file_exists_and_executable(os.path.join(prefix, binary_name)): | ||
exec_path = bin_path | ||
break | ||
if exec_path is None: | ||
raise Exception(f"Could not find executable ${binary_name}") | ||
else: | ||
return subprocess.check_output(args, shell=True, executable=exec_path) | ||
STDPATH = os.pathsep.join(["/usr/bin", "/bin", "/usr/sbin", "/sbin"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters