-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from sambarnes/auth-refactor
fix: dedupe auth logic & patch potential timing attack
- Loading branch information
Showing
13 changed files
with
250 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
|
||
jobs: | ||
unit: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
python-version: [ "3.10", "3.11" ] | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- uses: abatilo/actions-poetry@v2 | ||
|
||
- name: Install dependencies | ||
run: poetry install --no-root | ||
|
||
- name: Run tests | ||
run: poetry run pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,37 @@ | ||
import os | ||
import secrets | ||
|
||
from fastapi import Depends, HTTPException, status | ||
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer | ||
from pydantic import BaseModel | ||
|
||
_auth = HTTPBearer() | ||
|
||
|
||
class Config(BaseModel): | ||
name: str | ||
api_key_id: str | ||
|
||
def auth( | ||
self, token: HTTPAuthorizationCredentials = Depends(_auth) | ||
) -> HTTPAuthorizationCredentials: | ||
""" | ||
API Authentication dependency for protected endpoints. Checks that the request's bearer token | ||
matches the server's configured API key. | ||
Raises: | ||
* HTTPException(403) if no token is provided. | ||
* HTTPException(401) if the token is invalid. | ||
""" | ||
|
||
# Timing attacks possible through direct comparison. Prevent it with a constant time comparison here. | ||
got_credential = token.credentials.encode() | ||
want_credential = os.environ[self.api_key_id].encode() | ||
if not secrets.compare_digest(got_credential, want_credential): | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Incorrect bearer token", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
|
||
return token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import os | ||
|
||
from fastapi import Depends, FastAPI, testclient | ||
from fastapi.security import HTTPAuthorizationCredentials | ||
|
||
from shared.config import Config | ||
|
||
|
||
def test_auth(): | ||
"""The API auth dependency should prevent unauthorized requests.""" | ||
|
||
app = FastAPI() | ||
config = Config(name="test", api_key_id="RUNNER_API_KEY") | ||
os.environ["RUNNER_API_KEY"] = "abc123" | ||
|
||
@app.get("/test") | ||
def test(_token: HTTPAuthorizationCredentials = Depends(config.auth)): | ||
return "OK" | ||
|
||
with testclient.TestClient(app) as client: | ||
response = client.get("/test") | ||
assert response.status_code == 403 | ||
|
||
response = client.get( | ||
"/test", headers={"Authorization": "Bearer invalid"} | ||
) | ||
assert response.status_code == 401 | ||
|
||
response = client.get( | ||
"/test", headers={"Authorization": "Bearer abc123"} | ||
) | ||
assert response.status_code == 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.