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

Parse array slices without eval(<user_input>) #752

Merged
merged 8 commits into from
Jun 18, 2024

Conversation

hyperrealist
Copy link
Contributor

This PR addresses issue #751

@danielballan
Copy link
Member

Echoing comments from #751 for visiblity:

Thanks for working on this @hyperrealist.

I want to be clear for anyone reading that there are no known exploitable vulnerabilities in the current codebase. This regex input validation:

slice: str = Query(None, pattern="^[-0-9,:]*$"),

rejects malicious input. For example:

$ http "https://tiled-demo.blueskyproject.io/api/v1/array/full/generated/small_image?slice=import os;os.system('rm -rf /')"
HTTP/1.1 422 Unprocessable Entity
Connection: keep-alive
Content-Length: 261
Content-Type: application/json
Date: Fri, 31 May 2024 20:02:40 GMT
Server: nginx/1.18.0 (Ubuntu)
Set-Cookie: tiled_csrf=GKH9tESfusL742-9G0XY5Rn8TZvqxLQyKg1gka7-42o; HttpOnly; Path=/; SameSite=lax
server-timing: app;dur=8.2
set-cookie: tiled_csrf=GKH9tESfusL742-9G0XY5Rn8TZvqxLQyKg1gka7-42o; HttpOnly; Path=/; SameSite=lax
x-tiled-request-id: b558539f3ba5d4fa

{
    "detail": [
        {
            "ctx": {
                "pattern": "^[-0-9,:]*$"
            },
            "input": "import os;os.system('rm -rf /')",
            "loc": [
                "query",
                "slice"
            ],
            "msg": "String should match pattern '^[-0-9,:]*$'",
            "type": "string_pattern_mismatch",
            "url": "https://errors.pydantic.dev/2.7/v/string_pattern_mismatch"
        }
    ]
}

However, I very much support refactoring to remove eval for defense in depth.

@@ -160,19 +163,16 @@ def expected_shape(


def slice_(
slice: str = Query(None, pattern="^[-0-9,:]*$"),
slice: Optional[str] = None,
Copy link
Member

@danielballan danielballan Jun 4, 2024

Choose a reason for hiding this comment

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

I would like to retain a regex pattern. It no longer has a security function, with eval removed, but it does have a role in both documenting the acceptable inputs in the OpenAPI spec and providing a detailed error message. (See my example in PR Conversation tab.)

Additionally, it would be useful to test that all the failing examples (typo and malicious) will not match the regex, and therefore should get bounced by FastAPI before they parsed by our code. That is, the ValueError or TypeError should never actually get raised when used in integration with FastAPI.

Finally, the regex pattern I've used here is a little under-specified. It is sufficient to block malicious examples but not sufficient to block malformed (e.g. typo-ed) examples. A more precise regex was drafted in these lines of a dormant PR. Perhaps that would be reused here (with the mean aspect removed for now).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the feedback! I'll write a commit with updates.

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree. The rest looks good to me.

Copy link
Contributor

@padraic-shafer padraic-shafer left a comment

Choose a reason for hiding this comment

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

Looks good. A couple of minor things are noted in the comments.

tiled/_tests/test_slicer.py Outdated Show resolved Hide resolved
tiled/server/dependencies.py Outdated Show resolved Hide resolved
@danielballan
Copy link
Member

I rebased on main to move this along.

While resolving conflicts in CHANGELOG.md, I refined the language to clarity that there was no known exploitable vulnerability, but that this change improves our defense in depth.

@danielballan danielballan merged commit cd03fc5 into bluesky:main Jun 18, 2024
9 checks passed
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