diff --git a/mkconcore.py b/mkconcore.py index 3b20f8e4..8999b8fd 100644 --- a/mkconcore.py +++ b/mkconcore.py @@ -75,40 +75,45 @@ import shlex # Added for POSIX shell escaping # input validation helper -def safe_name(value, context, allow_path=False): - """ - Validates that the input string does not contain characters dangerous - for filesystem paths or shell command injection. - """ - if not value: - raise ValueError(f"{context} cannot be empty") - # blocks control characters and shell metacharacters - # allow path separators and drive colons for full paths when needed - if allow_path: - pattern = r'[\x00-\x1F\x7F*?"<>|;&`$\'()]' - else: - # blocks path traversal (/, \, :) in addition to shell metacharacters - pattern = r'[\x00-\x1F\x7F\\/:*?"<>|;&`$\'()]' - if re.search(pattern, value): - raise ValueError(f"Unsafe {context}: '{value}' contains illegal characters.") - return value - -MKCONCORE_VER = "22-09-18" - -SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) - -def _resolve_concore_path(): - script_concore = os.path.join(SCRIPT_DIR, "concore.py") - if os.path.exists(script_concore): - return SCRIPT_DIR - cwd_concore = os.path.join(os.getcwd(), "concore.py") - if os.path.exists(cwd_concore): - return os.getcwd() - return SCRIPT_DIR - -GRAPHML_FILE = sys.argv[1] -TRIMMED_LOGS = True -CONCOREPATH = _resolve_concore_path() +def safe_name(value, context, allow_path=False): + """ + Validates that the input string does not contain characters dangerous + for filesystem paths or shell command injection. + """ + if not value: + raise ValueError(f"{context} cannot be empty") + # blocks control characters and shell metacharacters + # allow path separators and drive colons for full paths when needed + if allow_path: + pattern = r'[\x00-\x1F\x7F*?"<>|;&`$\'()]' + else: + # blocks path traversal (/, \, :) in addition to shell metacharacters + pattern = r'[\x00-\x1F\x7F\\/:*?"<>|;&`$\'()]' + if re.search(pattern, value): + raise ValueError(f"Unsafe {context}: '{value}' contains illegal characters.") + return value + +MKCONCORE_VER = "22-09-18" + +SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) + +def _resolve_concore_path(): + script_concore = os.path.join(SCRIPT_DIR, "concore.py") + if os.path.exists(script_concore): + return SCRIPT_DIR + cwd_concore = os.path.join(os.getcwd(), "concore.py") + if os.path.exists(cwd_concore): + return os.getcwd() + return SCRIPT_DIR + +if len(sys.argv) < 4: + logging.error("Usage: python mkconcore.py [type]") + logging.error(" type must be posix (macos or ubuntu), windows, or docker") + quit() + +GRAPHML_FILE = safe_name(sys.argv[1], "GRAPHML file argument", allow_path=True) +TRIMMED_LOGS = True +CONCOREPATH = _resolve_concore_path() CPPWIN = "g++" #Windows C++ 6/22/21 CPPEXE = "g++" #Ubuntu/macOS C++ 6/22/21 VWIN = "iverilog" #Windows verilog 6/25/21 @@ -152,18 +157,15 @@ def _resolve_concore_path(): sourcedir = sys.argv[2] outdir = sys.argv[3] -# Validate outdir argument (allow full paths) -safe_name(outdir, "Output directory argument", allow_path=True) +# Validate arguments (allow full paths) +safe_name(sourcedir, "Source directory argument", allow_path=True) +safe_name(outdir, "Output directory argument", allow_path=True) if not os.path.isdir(sourcedir): logging.error(f"{sourcedir} does not exist") quit() -if len(sys.argv) < 4: - logging.error("usage: py mkconcore.py file.graphml sourcedir outdir [type]") - logging.error(" type must be posix (macos or ubuntu), windows, or docker") - quit() -elif len(sys.argv) == 4: +if len(sys.argv) == 4: prefixedgenode = outdir+"_" #nodes and edges prefixed with outdir_ only in case no type specified 3/24/21 concoretype = "docker" else: @@ -1227,4 +1229,4 @@ def cleanup_script_files(): os.chmod(outdir+"/clear",stat.S_IRWXU) os.chmod(outdir+"/maxtime",stat.S_IRWXU) os.chmod(outdir+"/params",stat.S_IRWXU) - os.chmod(outdir+"/unlock",stat.S_IRWXU) + os.chmod(outdir+"/unlock",stat.S_IRWXU)