Skip to content

Commit 3a2970c

Browse files
fix: validate arguments before usage in mkconcore.py (Issue #267)
1 parent 2c47e5e commit 3a2970c

1 file changed

Lines changed: 43 additions & 42 deletions

File tree

mkconcore.py

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -75,40 +75,45 @@
7575
import shlex # Added for POSIX shell escaping
7676

7777
# input validation helper
78-
def safe_name(value, context, allow_path=False):
79-
"""
80-
Validates that the input string does not contain characters dangerous
81-
for filesystem paths or shell command injection.
82-
"""
83-
if not value:
84-
raise ValueError(f"{context} cannot be empty")
85-
# blocks control characters and shell metacharacters
86-
# allow path separators and drive colons for full paths when needed
87-
if allow_path:
88-
pattern = r'[\x00-\x1F\x7F*?"<>|;&`$\'()]'
89-
else:
90-
# blocks path traversal (/, \, :) in addition to shell metacharacters
91-
pattern = r'[\x00-\x1F\x7F\\/:*?"<>|;&`$\'()]'
92-
if re.search(pattern, value):
93-
raise ValueError(f"Unsafe {context}: '{value}' contains illegal characters.")
94-
return value
95-
96-
MKCONCORE_VER = "22-09-18"
97-
98-
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
99-
100-
def _resolve_concore_path():
101-
script_concore = os.path.join(SCRIPT_DIR, "concore.py")
102-
if os.path.exists(script_concore):
103-
return SCRIPT_DIR
104-
cwd_concore = os.path.join(os.getcwd(), "concore.py")
105-
if os.path.exists(cwd_concore):
106-
return os.getcwd()
107-
return SCRIPT_DIR
108-
109-
GRAPHML_FILE = sys.argv[1]
110-
TRIMMED_LOGS = True
111-
CONCOREPATH = _resolve_concore_path()
78+
def safe_name(value, context, allow_path=False):
79+
"""
80+
Validates that the input string does not contain characters dangerous
81+
for filesystem paths or shell command injection.
82+
"""
83+
if not value:
84+
raise ValueError(f"{context} cannot be empty")
85+
# blocks control characters and shell metacharacters
86+
# allow path separators and drive colons for full paths when needed
87+
if allow_path:
88+
pattern = r'[\x00-\x1F\x7F*?"<>|;&`$\'()]'
89+
else:
90+
# blocks path traversal (/, \, :) in addition to shell metacharacters
91+
pattern = r'[\x00-\x1F\x7F\\/:*?"<>|;&`$\'()]'
92+
if re.search(pattern, value):
93+
raise ValueError(f"Unsafe {context}: '{value}' contains illegal characters.")
94+
return value
95+
96+
MKCONCORE_VER = "22-09-18"
97+
98+
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
99+
100+
def _resolve_concore_path():
101+
script_concore = os.path.join(SCRIPT_DIR, "concore.py")
102+
if os.path.exists(script_concore):
103+
return SCRIPT_DIR
104+
cwd_concore = os.path.join(os.getcwd(), "concore.py")
105+
if os.path.exists(cwd_concore):
106+
return os.getcwd()
107+
return SCRIPT_DIR
108+
109+
if len(sys.argv) < 4:
110+
print("Usage: python mkconcore.py <GRAPHML_FILE> <sourcedir> <outdir> [type]")
111+
print(" type must be posix (macos or ubuntu), windows, or docker")
112+
sys.exit(1)
113+
114+
GRAPHML_FILE = sys.argv[1]
115+
TRIMMED_LOGS = True
116+
CONCOREPATH = _resolve_concore_path()
112117
CPPWIN = "g++" #Windows C++ 6/22/21
113118
CPPEXE = "g++" #Ubuntu/macOS C++ 6/22/21
114119
VWIN = "iverilog" #Windows verilog 6/25/21
@@ -152,18 +157,14 @@ def _resolve_concore_path():
152157
sourcedir = sys.argv[2]
153158
outdir = sys.argv[3]
154159

155-
# Validate outdir argument (allow full paths)
156-
safe_name(outdir, "Output directory argument", allow_path=True)
160+
# Validate outdir argument (allow full paths)
161+
safe_name(outdir, "Output directory argument", allow_path=True)
157162

158163
if not os.path.isdir(sourcedir):
159164
logging.error(f"{sourcedir} does not exist")
160165
quit()
161166

162-
if len(sys.argv) < 4:
163-
logging.error("usage: py mkconcore.py file.graphml sourcedir outdir [type]")
164-
logging.error(" type must be posix (macos or ubuntu), windows, or docker")
165-
quit()
166-
elif len(sys.argv) == 4:
167+
if len(sys.argv) == 4:
167168
prefixedgenode = outdir+"_" #nodes and edges prefixed with outdir_ only in case no type specified 3/24/21
168169
concoretype = "docker"
169170
else:
@@ -1227,4 +1228,4 @@ def cleanup_script_files():
12271228
os.chmod(outdir+"/clear",stat.S_IRWXU)
12281229
os.chmod(outdir+"/maxtime",stat.S_IRWXU)
12291230
os.chmod(outdir+"/params",stat.S_IRWXU)
1230-
os.chmod(outdir+"/unlock",stat.S_IRWXU)
1231+
os.chmod(outdir+"/unlock",stat.S_IRWXU)

0 commit comments

Comments
 (0)