Skip to content

Commit

Permalink
404 if container not ready
Browse files Browse the repository at this point in the history
  • Loading branch information
ConnorNelson committed Jun 17, 2024
1 parent 8561f97 commit bdaafac
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
14 changes: 9 additions & 5 deletions dojo_plugin/pages/workspace.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import hmac

import docker
from flask import request, Blueprint, render_template, redirect, url_for, abort
from CTFd.models import Users
from CTFd.utils.user import get_current_user, is_admin
Expand Down Expand Up @@ -110,11 +111,14 @@ def forward_workspace(service, service_path=""):
abort(404)

if service in ondemand_services:
exec_run(
f"/opt/pwn.college/services.d/{service}",
workspace_user="hacker", user_id=user.id, shell=True,
assert_success=True
)
try:
exec_run(
f"/opt/pwn.college/services.d/{service}",
workspace_user="hacker", user_id=user.id, shell=True,
assert_success=True
)
except (docker.errors.NotFound, docker.errors.APIError):
abort(404)

elif service.count("~") == 1:
port, user_id = service.split("~", 1)
Expand Down
9 changes: 4 additions & 5 deletions dojo_plugin/utils/workspace.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import shlex

import docker

docker_client = docker.from_env()

def exec_run(cmd, *, shell=False, assert_success=True, workspace_user="root", user_id=None, container=None, **kwargs):

if shell:
cmd = f"""/bin/sh -c \"
{cmd}
\""""

cmd = f'/bin/sh -c {shlex.quote(cmd)}'
if not container:
container = docker_client.containers.get(f"user_{user_id}")
exit_code, output = container.exec_run(cmd, user=workspace_user, **kwargs)
Expand Down

0 comments on commit bdaafac

Please sign in to comment.