Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 0.1.9

### Security

- **Avoid reflecting invalid API keys**: authentication failures now return a generic error message instead of including the submitted credential in the response and request logs.

## 0.1.8

### Features
Expand Down
2 changes: 1 addition & 1 deletion prepline_general/api/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.8" # pragma: no cover
__version__ = "0.1.9" # pragma: no cover
2 changes: 1 addition & 1 deletion prepline_general/api/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ def general_partition(
api_key = request.headers.get("unstructured-api-key")
if api_key != api_key_env:
raise HTTPException(
detail=f"API key {api_key} is invalid", status_code=status.HTTP_401_UNAUTHORIZED
detail="API key is invalid", status_code=status.HTTP_401_UNAUTHORIZED

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nit: you could just slice the first 4 chars if you wanted. but i dont think that's actually very beneficial.
we know pretty well what the error is from this already.

)

accept_type = request.headers.get("Accept")
Expand Down
18 changes: 10 additions & 8 deletions test_general/api/test_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import logging
import os
import tempfile
import uuid
Expand Down Expand Up @@ -578,7 +579,7 @@ def test_general_api_returns_503(monkeypatch):
assert response.status_code == 503


def test_general_api_returns_401(monkeypatch):
def test_general_api_returns_401(monkeypatch, caplog):
"""
When UNSTRUCTURED_API_KEY is set, return a 401 if the unstructured-api-key header does not match
"""
Expand All @@ -594,15 +595,16 @@ def test_general_api_returns_401(monkeypatch):

assert response.status_code == 200

client = TestClient(app)
test_file = Path("sample-docs") / "fake-xml.xml"
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
headers={"unstructured-api-key": "helloworld"},
)
with caplog.at_level(logging.ERROR, logger="unstructured_api"):
response = client.post(
MAIN_API_ROUTE,
files=[("files", (str(test_file), open(test_file, "rb")))],
headers={"unstructured-api-key": "helloworld"},
)

assert response.status_code == 401
assert response.json() == {"detail": "API key is invalid"}
assert "helloworld" not in caplog.text


class MockResponse:
Expand Down
Loading