Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent users from downloading each other's sandboxes #121

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/diracx/routers/job_manager/sandboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def pfn_to_key(pfn: str) -> str:
async def get_sandbox_file(
pfn: Annotated[str, Query(max_length=256, pattern=SANDBOX_PFN_REGEX)],
settings: SandboxStoreSettings,
user_info: Annotated[AuthorizedUserInfo, Depends(verify_dirac_access_token)],
) -> SandboxDownloadResponse:
"""Get a presigned URL to download a sandbox file

Expand All @@ -164,7 +165,24 @@ async def get_sandbox_file(
most storage backends return an error when they receive an authorization
header for a presigned URL.
"""
# TODO: Prevent people from downloading other people's sandboxes?
required_prefix = (
"/"
+ "/".join(
[
"S3",
settings.bucket_name,
user_info.vo,
user_info.dirac_group,
user_info.preferred_username,
]
)
+ "/"
)
if not pfn.startswith(required_prefix):
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f"Invalid PFN. PFN must start with {required_prefix}",
)
# TODO: Support by name and by job id?
presigned_url = settings.s3_client.generate_presigned_url(
ClientMethod="get_object",
Expand Down
21 changes: 20 additions & 1 deletion tests/routers/jobs/test_sandboxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

import hashlib
import secrets
from copy import deepcopy
from io import BytesIO

import requests
from fastapi.testclient import TestClient

from diracx.routers.auth import AuthSettings, create_token

def test_upload_then_download(normal_user_client: TestClient):

def test_upload_then_download(
normal_user_client: TestClient, test_auth_settings: AuthSettings
):
data = secrets.token_bytes(512)
checksum = hashlib.sha256(data).hexdigest()

Expand Down Expand Up @@ -42,6 +47,20 @@ def test_upload_then_download(normal_user_client: TestClient):
assert r.status_code == 200, r.text
assert r.content == data

# Modify the authorization payload to be another user
other_user_payload = deepcopy(normal_user_client.dirac_token_payload)
other_user_payload["preferred_username"] = "other_user"
other_user_token = create_token(other_user_payload, test_auth_settings)

# Make sure another user can't download the sandbox
r = normal_user_client.get(
"/jobs/sandbox",
params={"pfn": sandbox_pfn},
headers={"Authorization": f"Bearer {other_user_token}"},
)
assert r.status_code == 400, r.text
assert "Invalid PFN. PFN must start with" in r.json()["detail"]


def test_upload_oversized(normal_user_client: TestClient):
data = secrets.token_bytes(512)
Expand Down