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

Add CSRF Token to Builder #1050

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open

Add CSRF Token to Builder #1050

wants to merge 25 commits into from

Conversation

yeldarby
Copy link
Contributor

@yeldarby yeldarby commented Feb 22, 2025

Description

This passes through a CSRF token to the frontend iframe that it passes back when accessing the API. This will prevent random websites from performing destructive actions on the local server (since they won't know the CSRF token). It also prevents clickjacking attacks because the iframe won't be able to do anything unless is has access to a valid CSRF token (which an attacker won't know so won't be able to iframe the UI in a maliciously constructed page).

Update: this also includes #1056 (which was approved separately) with docs content that depends on this.

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How has this change been tested, please provide a testcase or example of how you tested the change?

Locally & via staging. Only affects the new /build loginless UI.

Any specific deployment considerations

Won't actually work against prod until the frontend ships via the companion PR open on the core app repo.

Docs

  • Docs updated? What were the changes: Yes, the guides that use this have now been merged in.

SAFELISTED_HEADERS = {"Accept", "Accept-Language", "Content-Language", "Content-Type"}


class CORSMiddleware:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd try to avoid these big copy/pastes because you lose any improvement they do to original code.

You can just extend original behavior.

class OurCORSMiddleware(CORSMiddleware):
    def __init__(
        self,
        app: ASGIApp,
        allow_origins: typing.Sequence[str] = (),
        allow_methods: typing.Sequence[str] = ("GET",),
        allow_headers: typing.Sequence[str] = (),
        allow_credentials: bool = False,
        allow_origin_regex: str | None = None,
        expose_headers: typing.Sequence[str] = (),
        max_age: int = 600,
        match_paths: str | None = None,
    ):
        super().__init__(
            app,
            allow_origins=allow_origins,
            allow_methods=allow_methods,
            allow_headers=allow_headers,
            allow_credentials=allow_credentials,
            allow_origin_regex=allow_origin_regex,
            expose_headers=expose_headers,
            max_age=max_age,
        )
        self.match_paths_regex = re.compile(match_paths) if match_paths else None

    async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
        # If match_paths is provided and does not match, just skip to the next handler:
        if self.match_paths_regex is not None:
            path = scope.get("path", "")
            if not self.match_paths_regex.match(path):
                await self.app(scope, receive, send)
                return
        
        return super().__call_(scope, receive, send)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea; will update.

@PawelPeczek-Roboflow
Copy link
Collaborator

letting @iurisilvio to handle this and set approval since he is more familiar with the topic

iurisilvio
iurisilvio previously approved these changes Feb 26, 2025
Copy link
Contributor

@iurisilvio iurisilvio left a comment

Choose a reason for hiding this comment

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

LGTM, I think it is ok to have a simple CSRF implementation here.

I'd prefer to not copy/paste the middleware. I can approve again if you rewrite it with my idea.

@yeldarby yeldarby requested a review from capjamesg as a code owner February 26, 2025 21:55
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