Skip to content

Commit 3464a19

Browse files
authored
Add more lint checks (#57)
* Lint that Dockerfiles EXPOSE a port * Lint that if your challenge spec doesn't provide an image that you don't provide a Dockerfile * Lint that all files being searched for are provided * Closes #40
1 parent 8d06a73 commit 3464a19

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

ctfcli/utils/challenge.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,42 @@ def lint_challenge(path):
287287
required_fields = ["name", "author", "category", "description", "value"]
288288
errors = []
289289
for field in required_fields:
290-
if challenge.get(field) is None:
291-
errors.append(field)
290+
if field == "value" and challenge.get("type") == "dynamic":
291+
pass
292+
else:
293+
if challenge.get(field) is None:
294+
errors.append(field)
292295

293296
if len(errors) > 0:
294297
print("Missing fields: ", ", ".join(errors))
295298
exit(1)
296299

300+
# Check that the image field and Dockerfile match
301+
if (Path(path).parent / "Dockerfile").is_file() and challenge.get("image") != ".":
302+
print("Dockerfile exists but image field does not point to it")
303+
exit(1)
304+
305+
# Check that Dockerfile exists and is EXPOSE'ing a port
306+
if challenge.get("image") == ".":
307+
try:
308+
dockerfile = (Path(path).parent / "Dockerfile").open().read()
309+
except FileNotFoundError:
310+
print("Dockerfile specified in 'image' field but no Dockerfile found")
311+
exit(1)
312+
313+
if "EXPOSE" not in dockerfile:
314+
print("Dockerfile missing EXPOSE")
315+
exit(1)
316+
317+
# Check that all files exists
318+
files = challenge.get("files")
319+
errored = False
320+
for f in files:
321+
fpath = Path(path).parent / f
322+
if fpath.is_file() is False:
323+
print(f"File {f} specified but not found at {fpath.absolute()}")
324+
errored = True
325+
if errored:
326+
exit(1)
327+
297328
exit(0)

0 commit comments

Comments
 (0)