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

fix: dedupe auth logic & patch potential timing attack #31

Merged
merged 11 commits into from
Dec 31, 2023

Conversation

sambarnes
Copy link
Collaborator

Details

'splorin the codebase a bit more, and saw a simple refactor to share that auth logic across endpoints

haven't used fastapi in a little while, so when i went to check latest syntax on dependencies/middleware, i saw they recommend the new Annotated[...] style in python 3.10+

you can find the rationale behind a lot of the structuring in the docs here -- including their notes on the possible timing attack

Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I agree to license this contribution under the MIT LICENSE
  • I checked the current PR for duplication.

@sambarnes sambarnes marked this pull request as ready for review December 27, 2023 17:00
@louisgv
Copy link
Contributor

louisgv commented Dec 29, 2023

@sambarnes Thanks for the PR! - Had a tons of family stuff yesterday as well haha. Reviewing shortly!

ci: start hooking up unit test suite
pyproject.toml Outdated
@@ -16,11 +16,13 @@ accelerate = "^0.23.0"
datasets = "^2.14.5"
scipy = "^1.11.3"
wandb = "^0.15.12"
httpx = "^0.26.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

seems like we don't need this anymore? Based on your comment, I think this was needed for the e2e test.

For e2e, I wonder if we can do a modal deply runner to a e2e workspace, tail the modal URL, then call it with our JS scripts?

Also we should prob add a js script to test auth as well :-?

Copy link
Collaborator Author

@sambarnes sambarnes Dec 29, 2023

Choose a reason for hiding this comment

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

ah good catch! I think I just accidentally forgot to put this in the dev deps group. IIRC it was required by the fastapi test client, and shortcircuited the unit test until I had added it

(edit: done here 1d9e5c1)


I'll try digging into the E2E + js stuff you mentioned tomorrow :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

started the js test by simply putting a bad api key request inside the existing test-simple.ts (375823e)

but for the sake of separation & clutter, idk if it makes sense to take it much further inside this PR -- perhaps a new dedicated issue is warranted, to convert these manual scripts to a test suite with assertions?


i'm about to get on a flight, so i'll have some time to try out my own deployment & run the manual scripts to ensure what i've pushed so far works. after that i should have a better sense of how an automated CI/CD for E2E testing could look

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@louisgv aight yeh the test works as expected now, gonna re-req review

i was able to deploy & do a pnpm x scripts/test-simple.ts that resulted in two successful requests & then hit the expected failure of {"detail":"Incorrect bearer token"}

Copy link
Contributor

@louisgv louisgv left a comment

Choose a reason for hiding this comment

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

Great works 👍

@sambarnes
Copy link
Collaborator Author

sambarnes commented Dec 29, 2023

surprisingly airplane wifi been solid enough for my first attempt at the deployment workflow lol

was able to verify that my changes indeed do not work at the moment 🙃 appears that _token is being implicitly required as a body param rather than a dependency

{"detail":[{"loc":["body","payload"],"msg":"field required","type":"value_error.missing"},{"loc":["body","_token"],"msg":"field required","type":"value_error.missing"}]}
Error: Status: 422

gonna check if modal is on an older version of fastapi, or im missing some small thing elsewhere -- worstcase, i'll switch back to the older syntax


edit: aye there it is, released in fastapi 0.105.0 -- while we're locked at 0.104 rn
https://github.com/OpenRouterTeam/openrouter-runner/blob/main/poetry.lock#L533

just bumped to latest in 911e73b and gonna test again


editedit: looks like modal doesn't respect the new fastapi version i locked, so i reverted that change & will be changing to the older = Depends(...) format. possible that i just didnt see where to do a container rebuild, but oh well not gonna waste more time on it since the planes landing soon lol

@sambarnes sambarnes requested a review from louisgv December 29, 2023 18:10
@louisgv
Copy link
Contributor

louisgv commented Dec 31, 2023

@sambarnes yeah one of the constraint with the modal runner is that the modal's stub abstraction depends on a specific fastapi version (as well as pydantic)

Comment on lines +20 to +28
let gotExpectedError = false;
try {
await completion(prompt, {model, apiKey: "BADKEY"});
} catch (e: any) {
gotExpectedError = e.message == "Status: 401";
}
if (!gotExpectedError) {
throw new Error("Unauthorized request returned unexpected response")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: we can just re-throw the error in the try if not 401, no need to do it separately IMO. Also, we should either use startsWith or strict equal.

Copy link
Contributor

@louisgv louisgv left a comment

Choose a reason for hiding this comment

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

Overall LGTM - added some nits! Merging for now and will fix the nits in a hot-patch later.

@louisgv louisgv merged commit 9261a6e into OpenRouterTeam:main Dec 31, 2023
3 checks passed
@sambarnes sambarnes deleted the auth-refactor branch December 31, 2023 19:04
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