Skip to content

switch backend to listen on free port and require auth#35

Merged
Ofir Gluzman (ofirgluzman) merged 1 commit intomainfrom
feature/fix-port-conflict
Mar 9, 2026
Merged

switch backend to listen on free port and require auth#35
Ofir Gluzman (ofirgluzman) merged 1 commit intomainfrom
feature/fix-port-conflict

Conversation

@alexger
Copy link
Member

  • instead of hard-coded port we dynamically pick a free one
  • backend<->frontend channel is guarded by a random auth token

auth_header = request.headers.get("authorization", "")
if auth_header.startswith("Bearer ") and _token_matches(auth_header[7:]):
return await call_next(request)
if auth_header.startswith("Basic "):

Choose a reason for hiding this comment

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

why needed in addition to bearer token?

Copy link
Member Author

Choose a reason for hiding this comment

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

it isn't but curl is slightly more convenient to use with basic: curl -u ':<token'> ...

) -> StarletteResponse:
if not auth_token:
return await call_next(request)
if request.method == "OPTIONS":

Choose a reason for hiding this comment

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

Is this under assumption the options requests get swallowed by the cors middleware? If so, worth adding a comment

Copy link
Member Author

Choose a reason for hiding this comment

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

OPTIONS mustn't require auth, so the code lets it pass. There is no assumption cors middleware exists at all...

if os.environ.get("LTX_NO_AUTH", "").lower() in ("1", "true", "yes"):
auth_token = ""
else:
auth_token = os.environ.get("LTX_AUTH_TOKEN", "") or secrets.token_urlsafe(32)
Copy link
Member

@ofirgluzman Ofir Gluzman (ofirgluzman) Mar 8, 2026

Choose a reason for hiding this comment

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

Suggestion:

  • Server just gets LTX_AUTH_TOKEN as optional token, null means no token
  • Electron main is the one generating the token and starts the backend with LTX_AUTH_TOKEN populated to it
  • no LTX_NO_AUTH, this is covered by LTX_AUTH_TOKEN=ll
  • No self generation of the token by the backend (secrets.token_urlsafe(32)) and picking it from the log by Eelectron

Pros

  • Simpler: just one token source
  • More secured: token is not printed in the std logs (which I also just started to route to the log file before reading this PR at 90259a9)
  • No log regex parsing communication

- instead of hard-coded port we dynamically pick a free one
- backend<->frontend channel is guarded by a random auth token
@alexger
Copy link
Member Author

done, i've moved the token selection into electron as you suggested

@ofirgluzman Ofir Gluzman (ofirgluzman) merged commit 6987944 into main Mar 9, 2026
4 checks passed
@ofirgluzman Ofir Gluzman (ofirgluzman) deleted the feature/fix-port-conflict branch March 9, 2026 11:00
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.

2 participants