-
Notifications
You must be signed in to change notification settings - Fork 9
Fix IDOR in GET /api/users/{user_id} — email leaked to any authenticated user , Closes #25 #40
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b724b6f
fix: gate email/verification fields on GET /api/users/{user_id} behin…
SankeerthNara 1178e58
fix: satisfy mypy strict typing in shares_workspace
SankeerthNara c85d63c
fix: prevent email leak via display_name fallback in PublicUserResponse
SankeerthNara d6e3f1d
test: add regression tests for workspace-gated user profile access (#25)
SankeerthNara a83a444
ci: add Postgres service and test DB migrations to API CI workflow
SankeerthNara 1216b73
ci: restrict GITHUB_TOKEN to read-only permissions
SankeerthNara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,38 +1,51 @@ | ||
| name: API CI | ||
|
|
||
| on: | ||
| pull_request: | ||
| branches: [main] | ||
| paths: | ||
| - "api/app/**" | ||
| - ".github/workflows/api-ci.yml" | ||
|
|
||
| jobs: | ||
| lint-and-test: | ||
| runs-on: ubuntu-latest | ||
| permissions: | ||
| contents: read | ||
| defaults: | ||
| run: | ||
| working-directory: api/app | ||
|
|
||
| services: | ||
| postgres: | ||
| image: postgres:17-alpine | ||
| env: | ||
| POSTGRES_USER: postgres | ||
| POSTGRES_PASSWORD: postgres | ||
| POSTGRES_DB: loomy_test | ||
| ports: | ||
| - 5432:5432 | ||
| options: >- | ||
| --health-cmd pg_isready | ||
| --health-interval 10s | ||
| --health-timeout 5s | ||
| --health-retries 5 | ||
| env: | ||
| TEST_DATABASE_URL: postgresql://postgres:postgres@localhost:5432/loomy_test | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - name: Install uv | ||
| uses: astral-sh/setup-uv@v4 | ||
| with: | ||
| version: "latest" | ||
|
|
||
| - name: Set up Python | ||
| run: uv python install 3.12 | ||
|
|
||
| - name: Install dependencies | ||
| run: uv sync --all-extras | ||
|
|
||
| - name: Run migrations (test db) | ||
| run: uv run alembic upgrade head | ||
| env: | ||
| DATABASE_URL: postgresql://postgres:postgres@localhost:5432/loomy_test | ||
| - name: Ruff | ||
| run: uv run ruff check . | ||
|
|
||
| - name: Mypy | ||
| run: uv run mypy . | ||
|
|
||
| - name: Pytest | ||
| run: uv run pytest --tb=short -q | ||
| run: uv run pytest --tb=short -q |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,9 +1,101 @@ | ||
| import uuid | ||
| import os | ||
| from collections.abc import Generator | ||
|
|
||
| import pytest | ||
| from fastapi.testclient import TestClient | ||
| from sqlalchemy import create_engine | ||
| from sqlalchemy.orm import Session, sessionmaker | ||
|
|
||
| from app.api.deps import get_current_user | ||
| from app.db.session import get_db | ||
| from app.main import app | ||
| from app.modules.users.model import User | ||
| from app.modules.workspaces.model import Workspace, WorkspaceMember | ||
|
|
||
| TEST_DATABASE_URL = os.environ.get( | ||
| "TEST_DATABASE_URL", | ||
| "postgresql://postgres:postgres@localhost:15432/loomy_test", | ||
| ) | ||
|
|
||
| engine = create_engine(TEST_DATABASE_URL) | ||
| TestingSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def db() -> Generator[Session, None, None]: | ||
| """Each test runs inside a transaction that is rolled back afterward, | ||
| so tests never leave residue in loomy_test and can run in any order.""" | ||
| connection = engine.connect() | ||
| transaction = connection.begin() | ||
| session = TestingSessionLocal(bind=connection) | ||
|
|
||
| try: | ||
| yield session | ||
| finally: | ||
| session.close() | ||
| transaction.rollback() | ||
| connection.close() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def client() -> TestClient: | ||
| return TestClient(app) | ||
| def client(db: Session) -> Generator[TestClient, None, None]: | ||
| def _get_db_override() -> Generator[Session, None, None]: | ||
| yield db | ||
|
|
||
| app.dependency_overrides[get_db] = _get_db_override | ||
| with TestClient(app) as test_client: | ||
| yield test_client | ||
| app.dependency_overrides.clear() | ||
|
|
||
|
|
||
| def make_user( | ||
| db: Session, | ||
| *, | ||
| email: str | None = None, | ||
| username: str | None = None, | ||
| first_name: str | None = "Test", | ||
| last_name: str | None = "User", | ||
| ) -> User: | ||
| """Create and persist a real User row for use in tests.""" | ||
| unique = uuid.uuid4().hex[:8] | ||
| user = User( | ||
| email=email or f"user-{unique}@test.com", | ||
| username=username or f"user{unique}", | ||
| # Password hash is irrelevant here since tests authenticate via | ||
| # the get_current_user override, not a real login flow. | ||
| hashed_password="not-a-real-hash", | ||
| first_name=first_name, | ||
| last_name=last_name, | ||
| email_verified=False, | ||
| ) | ||
| db.add(user) | ||
| db.flush() | ||
| db.refresh(user) | ||
| return user | ||
|
|
||
|
|
||
| def make_workspace(db: Session, *, owner: User, name: str = "Test Workspace") -> Workspace: | ||
| """Create a workspace owned by `owner`, and add the owner as a member | ||
| (mirrors workspaces/repository.py's create()).""" | ||
| unique = uuid.uuid4().hex[:8] | ||
| workspace = Workspace(name=name, slug=f"test-workspace-{unique}", owner_id=owner.id) | ||
| db.add(workspace) | ||
| db.flush() | ||
| db.refresh(workspace) | ||
| member = WorkspaceMember(workspace_id=workspace.id, user_id=owner.id, role="owner") | ||
| db.add(member) | ||
| db.flush() | ||
| return workspace | ||
|
|
||
|
|
||
| def add_member(db: Session, *, workspace: Workspace, user: User, role: str = "member") -> None: | ||
| member = WorkspaceMember(workspace_id=workspace.id, user_id=user.id, role=role) | ||
| db.add(member) | ||
| db.flush() | ||
|
|
||
|
|
||
| def auth_as(client: TestClient, user: User) -> None: | ||
| """Override get_current_user so requests through `client` are | ||
| authenticated as `user`, without going through a real JWT login.""" | ||
| app.dependency_overrides[get_current_user] = lambda: user |
This file contains hidden or 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,89 @@ | ||
| from fastapi.testclient import TestClient | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from tests.conftest import add_member, auth_as, make_user, make_workspace | ||
|
|
||
|
|
||
| def test_get_user_no_shared_workspace_hides_email( | ||
| client: TestClient, db: Session | ||
| ) -> None: | ||
| """Core regression test for issue #25: a user with no shared workspace | ||
| must not be able to retrieve another user's email via this endpoint.""" | ||
| requester = make_user(db) | ||
| target = make_user(db) | ||
| auth_as(client, requester) | ||
|
|
||
| response = client.get(f"/api/users/{target.id}") | ||
|
|
||
| assert response.status_code == 200 | ||
| body = response.json() | ||
| assert set(body.keys()) == {"id", "username", "display_name", "avatar_url"} | ||
| assert "email" not in body | ||
| assert "email_verified" not in body | ||
| assert "first_name" not in body | ||
| assert "last_name" not in body | ||
|
|
||
|
|
||
| def test_get_user_self_lookup_returns_full_profile( | ||
| client: TestClient, db: Session | ||
| ) -> None: | ||
| user = make_user(db) | ||
| auth_as(client, user) | ||
|
|
||
| response = client.get(f"/api/users/{user.id}") | ||
|
|
||
| assert response.status_code == 200 | ||
| body = response.json() | ||
| assert body["email"] == user.email | ||
| assert body["email_verified"] == user.email_verified | ||
|
|
||
|
|
||
| def test_get_user_shared_workspace_returns_full_profile( | ||
| client: TestClient, db: Session | ||
| ) -> None: | ||
| owner = make_user(db) | ||
| member = make_user(db) | ||
| workspace = make_workspace(db, owner=owner) | ||
| add_member(db, workspace=workspace, user=member) | ||
| auth_as(client, owner) | ||
|
|
||
| response = client.get(f"/api/users/{member.id}") | ||
|
|
||
| assert response.status_code == 200 | ||
| body = response.json() | ||
| assert body["email"] == member.email | ||
| assert body["email_verified"] == member.email_verified | ||
|
|
||
|
|
||
| def test_get_user_display_name_never_leaks_email_local_part( | ||
| client: TestClient, db: Session | ||
| ) -> None: | ||
| """Regression test for the coderabbitai finding: when a user has no | ||
| first_name/last_name set, the public response's display_name must fall | ||
| back to username, never to the email local part.""" | ||
| requester = make_user(db) | ||
| target = make_user( | ||
| db, | ||
| email="secretlocalpart@test.com", | ||
| first_name=None, | ||
| last_name=None, | ||
| ) | ||
| auth_as(client, requester) | ||
|
|
||
| response = client.get(f"/api/users/{target.id}") | ||
|
|
||
| assert response.status_code == 200 | ||
| body = response.json() | ||
| assert body["display_name"] == target.username | ||
| assert "secretlocalpart" not in body["display_name"] | ||
|
|
||
|
|
||
| def test_get_user_not_found_returns_404(client: TestClient, db: Session) -> None: | ||
| import uuid | ||
|
|
||
| requester = make_user(db) | ||
| auth_as(client, requester) | ||
|
|
||
| response = client.get(f"/api/users/{uuid.uuid4()}") | ||
|
|
||
| assert response.status_code == 404 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.