Skip to content

Commit dbde435

Browse files
rupertnashmr-c
authored andcommitted
remove relaxed/strict path checking global state to make in-process tests work consistently
1 parent 9c702a9 commit dbde435

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

cwltool/command_line_tool.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Implementation of CommandLineTool."""
22

33
import copy
4+
from enum import Enum
45
import hashlib
56
import json
67
import locale
@@ -22,6 +23,7 @@
2223
MutableMapping,
2324
MutableSequence,
2425
Optional,
26+
Pattern,
2527
Set,
2628
TextIO,
2729
Union,
@@ -84,11 +86,19 @@
8486
if TYPE_CHECKING:
8587
from .provenance_profile import ProvenanceProfile # pylint: disable=unused-import
8688

87-
ACCEPTLIST_EN_STRICT_RE = re.compile(
88-
r"^[\w.+\-\u2600-\u26FF\U0001f600-\U0001f64f]+$"
89-
) # accept unicode word characters and emojis
90-
ACCEPTLIST_EN_RELAXED_RE = re.compile(r".*") # Accept anything
91-
ACCEPTLIST_RE = ACCEPTLIST_EN_STRICT_RE
89+
90+
class PathCheckingMode(Enum):
91+
"""What characters are allowed in path names.
92+
93+
We have the strict, default mode and the relaxed mode.
94+
"""
95+
96+
STRICT = re.compile(
97+
r"^[\w.+\-\u2600-\u26FF\U0001f600-\U0001f64f]+$"
98+
) # accept unicode word characters and emojis
99+
RELAXED = re.compile(r".*") # Accept anything
100+
101+
92102
DEFAULT_CONTAINER_MSG = """
93103
We are on Microsoft Windows and not all components of this CWL description have a
94104
container specified. This means that these steps will be executed in the default container,
@@ -301,7 +311,9 @@ def run(
301311
)
302312

303313

304-
def check_adjust(builder: Builder, file_o: CWLObjectType) -> CWLObjectType:
314+
def check_adjust(
315+
accept_re: Pattern[str], builder: Builder, file_o: CWLObjectType
316+
) -> CWLObjectType:
305317
"""
306318
Map files to assigned path inside a container.
307319
@@ -327,7 +339,7 @@ def check_adjust(builder: Builder, file_o: CWLObjectType) -> CWLObjectType:
327339
file_o["nameroot"] = str(nr)
328340
if file_o.get("nameext") != ne:
329341
file_o["nameext"] = str(ne)
330-
if not ACCEPTLIST_RE.match(basename):
342+
if not accept_re.match(basename):
331343
raise WorkflowException(
332344
"Invalid filename: '{}' contains illegal characters".format(
333345
file_o["basename"]
@@ -368,6 +380,11 @@ def __init__(
368380
"""Initialize this CommandLineTool."""
369381
super().__init__(toolpath_object, loadingContext)
370382
self.prov_obj = loadingContext.prov_obj
383+
self.path_check_mode = (
384+
PathCheckingMode.RELAXED
385+
if loadingContext.relax_path_checks
386+
else PathCheckingMode.STRICT
387+
) # type: PathCheckingMode
371388

372389
def make_job_runner(self, runtimeContext: RuntimeContext) -> Type[JobBase]:
373390
dockerReq, dockerRequired = self.get_requirement("DockerRequirement")
@@ -685,7 +702,7 @@ def remove_dirname(d: CWLObjectType) -> None:
685702
visit_class(
686703
[builder.files, builder.bindings],
687704
("File", "Directory"),
688-
partial(check_adjust, builder),
705+
partial(check_adjust, self.path_check_mode.value, builder),
689706
)
690707

691708
def job(
@@ -713,7 +730,9 @@ def job(
713730
cachebuilder.stagedir,
714731
separateDirs=False,
715732
)
716-
_check_adjust = partial(check_adjust, cachebuilder)
733+
_check_adjust = partial(
734+
check_adjust, self.path_check_mode.value, cachebuilder
735+
)
717736
visit_class(
718737
[cachebuilder.files, cachebuilder.bindings],
719738
("File", "Directory"),
@@ -887,7 +906,7 @@ def update_status_output_callback(
887906
)
888907
builder.requirements = j.requirements
889908

890-
_check_adjust = partial(check_adjust, builder)
909+
_check_adjust = partial(check_adjust, self.path_check_mode.value, builder)
891910

892911
visit_class(
893912
[builder.files, builder.bindings], ("File", "Directory"), _check_adjust

cwltool/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def __init__(self, kwargs: Optional[Dict[str, Any]] = None) -> None:
7474
self.do_update = None # type: Optional[bool]
7575
self.jobdefaults = None # type: Optional[CommentedMap]
7676
self.doc_cache = True # type: bool
77+
self.relax_path_checks = False # type: bool
7778

7879
super().__init__(kwargs)
7980

cwltool/main.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
from schema_salad.sourceline import strip_dup_lineno
4242
from schema_salad.utils import ContextType, FetcherCallableType, json_dumps
4343

44-
from . import CWL_CONTENT_TYPES, command_line_tool, workflow
44+
from . import CWL_CONTENT_TYPES, workflow
4545
from .argparser import arg_parser, generate_parser, get_default_args
4646
from .builder import HasReqsHints
4747
from .context import LoadingContext, RuntimeContext, getdefault
@@ -949,8 +949,6 @@ def main(
949949
_logger.error("CWL document required, no input file was provided")
950950
parser.print_help()
951951
return 1
952-
if args.relax_path_checks:
953-
command_line_tool.ACCEPTLIST_RE = command_line_tool.ACCEPTLIST_EN_RELAXED_RE
954952

955953
if args.ga4gh_tool_registries:
956954
ga4gh_tool_registries[:] = args.ga4gh_tool_registries

0 commit comments

Comments
 (0)