Skip to content

Migrate labs json file to dedicated labs model#535

Open
samyak003 wants to merge 1 commit into
adeyosemanputra:v3.0.0-prefrom
samyak003:labs-model
Open

Migrate labs json file to dedicated labs model#535
samyak003 wants to merge 1 commit into
adeyosemanputra:v3.0.0-prefrom
samyak003:labs-model

Conversation

@samyak003

Copy link
Copy Markdown
Contributor

Migrate labs JSON config to Django database & enable Custom Labs CRUD

Overview

This PR migrates the static list of labs from labs.json to a database-backed Django model Lab. This enables dynamic custom lab management (CRUD) directly from the user interface.

Key Changes

  • Database Migration: Added the Lab model and migrations to populate default built-in labs.
  • CRUD Views & Endpoints: Implemented views and endpoints in challenge/views.py and challenge/urls.py to dynamically list, create, update, and delete custom labs.
  • Frontend Integration: Added a "Custom Labs" management modal and a dynamic collapsible sidebar inside base.html along with supporting CSS.
  • Startup Command & Configs: Refactored the startuplabs management command to retrieve labs from the database, registered the model in Django Admin, and deleted the obsolete labs.json file.
  • Cleanups: Commented out the HEALTHCHECK directive in the Dockerfile, removed obsolete MITRE endpoints/views, and updated the setup instructions in README.md.

{"name": "xxe_lab", "build_location": "dockerized_labs/xxe_lab", "port": 5010},
{"name": "dependency_attack_lab", "build_location": "dockerized_labs/dependency_attack_lab", "port": 5020},
{"name": "package_injection_lab", "build_location": "dockerized_labs/package_injection_lab", "port": 5021},
{"name": "open_source_library_attack_lab", "build_location": "dockerized_labs/open_source_library_attack_lab", "port": 5022}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This list will be updated in future, so user have to update previous migration, which is not recomanded
better solution is keep a seed script or seed command ready for populating this table. Then in future user have to update that seed script, not previous migration file

const btn = document.createElement('button');
btn.type = 'button';
btn.className = 'sidebar-list-items lab-button';
btn.setAttribute('onclick', `launchLab('${lab.name}')`);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

lab.name comes from the create/update endpoint without validation, and this builds an inline JS handler. A name like x\x27);alert(1);// becomes executable code when the sidebar is rendered and clicked. Use addEventListener("click", () => launchLab(lab.name)) and validate names server-side against the slug format used by the URL.

Comment thread introduction/mitre.py
@@ -1,277 +0,0 @@
import datetime

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This deletion breaks URLConf import: introduction/urls.py still has from . import mitre, views. With this module removed, importing the URLConf raises ImportError/ModuleNotFoundError before any route is served. Please either keep the module or remove the import and all remaining /mitre/... references in the template/sidebar.

Comment thread challenge/views.py

try:
try:
lab = Lab.objects.get(id=lab_id)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This lookup is not scoped to custom labs, so any authenticated user who can guess or enumerate a built-in Lab id can POST to /challenge/custom-labs/update/<id>/ and overwrite its name, build_location, and port. The delete endpoint has the same issue. Please fetch with id=lab_id, is_custom=True and return 404/403 for built-ins, or restrict these endpoints to staff/admin.

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