Skip to content

Commit ab62e52

Browse files
fix: None not defaulted to []
Looks like .get() does not set challenge_files to the default empty list when it is None, causing ctfcli to throw. A second potential error case happens when directly using dict["files"] without checking for a None value. Using the OR operator fixes this. Signed-off-by: AlexNg <[email protected]>
1 parent 763c2de commit ab62e52

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

ctfcli/core/challenge.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ def _load_challenge_id(self):
252252

253253
def _validate_files(self):
254254
# if the challenge defines files, make sure they exist before making any changes to the challenge
255-
for challenge_file in self.get("files", []):
255+
for challenge_file in self.get("files") or []:
256256
if not (self.challenge_directory / challenge_file).exists():
257257
raise InvalidChallengeFile(f"File {challenge_file} could not be loaded")
258258

@@ -364,7 +364,7 @@ def _create_file(self, local_path: Path):
364364

365365
def _create_all_files(self):
366366
new_files = []
367-
for challenge_file in self["files"]:
367+
for challenge_file in self.get("files") or []:
368368
new_files.append(("file", open(self.challenge_directory / challenge_file, mode="rb")))
369369

370370
files_payload = {"challenge_id": self.challenge_id, "type": "challenge"}
@@ -588,8 +588,8 @@ def sync(self, ignore: Tuple[str] = ()) -> None:
588588
# Create / Upload files
589589
if "files" not in ignore:
590590
# Get basenames of local files to compare against remote files
591-
local_files = {f.split("/")[-1]: f for f in self.get("files", [])}
592-
remote_files = self._normalize_remote_files(remote_challenge.get("files", []))
591+
local_files = {f.split("/")[-1]: f for f in self.get("files") or []}
592+
remote_files = self._normalize_remote_files(remote_challenge.get("files") or [])
593593

594594
# Delete remote files which are no longer defined locally
595595
for remote_file in remote_files:
@@ -761,7 +761,7 @@ def lint(self, skip_hadolint=False, flag_format="flag{") -> bool:
761761
click.secho("Skipping Hadolint", fg="yellow")
762762

763763
# Check that all files exist
764-
challenge_files = challenge.get("files", [])
764+
challenge_files = challenge.get("files") or []
765765
for challenge_file in challenge_files:
766766
challenge_file_path = self.challenge_directory / challenge_file
767767

@@ -794,9 +794,12 @@ def mirror(self, files_directory_name: str = "dist", ignore: Tuple[str] = ()) ->
794794
remote_challenge = self.load_installed_challenge(self.challenge_id)
795795
challenge = self._normalize_challenge(remote_challenge)
796796

797+
remote_challenge["files"] = remote_challenge["files"] or []
798+
challenge["files"] = challenge["files"] or []
799+
797800
# Add files which are not handled in _normalize_challenge
798801
if "files" not in ignore:
799-
local_files = {Path(f).name: f for f in challenge.get("files", [])}
802+
local_files = {Path(f).name: f for f in challenge["files"]}
800803

801804
# Update files
802805
for remote_file in remote_challenge["files"]:
@@ -813,9 +816,6 @@ def mirror(self, files_directory_name: str = "dist", ignore: Tuple[str] = ()) ->
813816
challenge_files_directory.mkdir(parents=True, exist_ok=True)
814817

815818
(challenge_files_directory / remote_file_name).write_bytes(r.content)
816-
if "files" not in challenge:
817-
challenge["files"] = []
818-
819819
challenge["files"].append(f"{files_directory_name}/{remote_file_name}")
820820

821821
# The file is already present in the challenge.yml - we know the desired path
@@ -827,7 +827,7 @@ def mirror(self, files_directory_name: str = "dist", ignore: Tuple[str] = ()) ->
827827
# Soft-Delete files that are not present on the remote
828828
# Remove them from challenge.yml but do not delete them from disk
829829
remote_file_names = [f.split("/")[-1].split("?token=")[0] for f in remote_challenge["files"]]
830-
challenge["files"] = [f for f in challenge.get("files", []) if Path(f).name in remote_file_names]
830+
challenge["files"] = [f for f in challenge["files"] if Path(f).name in remote_file_names]
831831

832832
for key in challenge.keys():
833833
if key not in ignore:
@@ -841,6 +841,9 @@ def verify(self, ignore: Tuple[str] = ()) -> bool:
841841
remote_challenge = self.load_installed_challenge(self.challenge_id)
842842
normalized_challenge = self._normalize_challenge(remote_challenge)
843843

844+
remote_challenge["files"] = remote_challenge["files"] or []
845+
challenge["files"] = challenge["files"] or []
846+
844847
for key in normalized_challenge:
845848
if key in ignore:
846849
continue
@@ -865,7 +868,7 @@ def verify(self, ignore: Tuple[str] = ()) -> bool:
865868
# Check if files defined in challenge.yml are present
866869
try:
867870
self._validate_files()
868-
local_files = {Path(f).name: f for f in challenge.get("files", [])}
871+
local_files = {Path(f).name: f for f in challenge["files"]}
869872
except InvalidChallengeFile:
870873
return False
871874

0 commit comments

Comments
 (0)