Skip to content

split challenge.py into generic properties#214

Closed
vellvoid wants to merge 0 commit into
CTFd:masterfrom
vellvoid:poc-properties
Closed

split challenge.py into generic properties#214
vellvoid wants to merge 0 commit into
CTFd:masterfrom
vellvoid:poc-properties

Conversation

@vellvoid

Copy link
Copy Markdown
Collaborator

core/challenge.py has become the designated place for most of ctfcli's most-important functionality (managing challenges). Adding a new property to the challenge requires touching multiple places and further stuffing of that file - the architecture is just not scalable.

This PR introduces a concept of properties - each challenge property like flags, topics, files, name etc. is handled within its own class which knows how to install, sync, compare, pull that specific property. The challenge class just calls hooks for all known properties.

Adding a new property boils down to creating a new class (or using a generic one for simple properties like just a string), and registering it with the properties registry.

@vellvoid

Copy link
Copy Markdown
Collaborator Author

this builds on #213 to avoid conflicts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors ctfcli.core.challenge by introducing a property registry (ctfcli.core.properties) where each challenge.yml attribute is managed by a dedicated Property class responsible for create/sync/pull/compare/mirror/verify behavior. This reduces the growth pressure on core/challenge.py and centralizes lifecycle logic per attribute.

Changes:

  • Added a property framework (Property, PropertyContext, registry, and multiple concrete properties) and updated Challenge to orchestrate behavior via the registry.
  • Moved/rewired challenge linting into ctfcli.core.lint.lint_challenge and updated tests to patch the new module locations.
  • Improved file upload handling in CLI media upload to ensure file handles are closed via context managers.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/core/test_challenge.py Updates tests to use property APIs (get_property, PropertyContext) and patch new module locations.
tests/core/deployment/test_ssh_deployment.py Updates mocks to patch Image in the new properties.image module.
tests/core/deployment/test_registry_deployment.py Updates mocks to patch Image in the new properties.image module.
tests/core/deployment/test_cloud_deployment.py Updates mocks to patch Image in the new properties.image module.
Makefile Adjusts phony targets and makes lint errors fail the build (no longer ignored).
ctfcli/core/properties/base.py Introduces Property/PropertyContext base abstractions and sentinel NOT_PULLED.
ctfcli/core/properties/init.py Adds the ordered property registry and helpers (get_property, operation_order).
ctfcli/core/properties/scalars.py Implements scalar-style properties (name/text/value/type/state/etc.), including scheduled_at parsing/normalization.
ctfcli/core/properties/collections.py Implements collection-backed properties (flags/topics/tags/hints) with create/sync/pull behavior.
ctfcli/core/properties/files.py Implements challenge file sync/mirror/verify logic as a property.
ctfcli/core/properties/image.py Implements image resolution logic as a local-only property used by deployments.
ctfcli/core/properties/references.py Implements reference properties (requirements/next/module) with normalization and matching.
ctfcli/core/properties/solution.py Implements solution upsert logic including markdown image uploading and snippet include expansion.
ctfcli/core/lint.py Adds a dedicated lint module and delegates Challenge.lint() to it.
ctfcli/core/challenge.py Refactors Challenge to build payloads and run operations via the property registry.
ctfcli/core/api.py Modernizes imports and super() usage; keeps multipart handling centralized.
ctfcli/cli/media.py Fixes file upload to use a context manager for safe file-handle cleanup.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread ctfcli/core/properties/references.py Outdated
Comment on lines +52 to +60
required_challenge_ids = list(set(required_challenges))

if ctx.challenge_id in required_challenge_ids:
click.secho(
"Challenge cannot require itself. Skipping invalid requirement.",
fg="yellow",
)
required_challenges.remove(ctx.challenge_id)
required_challenges.sort()
Comment thread ctfcli/core/properties/files.py Outdated
Comment on lines +201 to +215
def create_all_files(self, ctx: PropertyContext) -> None:
new_files = []
for challenge_file in ctx.challenge["files"]:
file_path = ctx.challenge_directory / challenge_file
new_files.append(("file", (file_path.name, file_path.open("rb"))))

files_payload = {"challenge_id": ctx.challenge_id, "type": "challenge"}

# Specifically use data= here to send multipart/form-data
r = ctx.api.post("/api/v1/files", files=new_files, data=files_payload)
r.raise_for_status()

# Close the file handles
for file_payload in new_files:
file_payload[1][1].close()
Comment thread ctfcli/core/properties/solution.py Outdated
Comment on lines +123 to +137
for mdx, alt, path in markdown_images:
local_path = solution_path.parent / path
file_payload = {
"type": "solution",
"solution_id": solution_id,
}

with local_path.open(mode="rb") as file_handle:
# Specifically use data= here to send multipart/form-data
r = ctx.api.post("/api/v1/files", files={"file": (local_path.name, file_handle)}, data=file_payload)
r.raise_for_status()
resp = r.json()
server_location = resp["data"][0]["location"]

content = content.replace(mdx, f"![{alt}](/files/{server_location})")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants