gravitino icon indicating copy to clipboard operation
gravitino copied to clipboard

[Subtask] test(mcp-server): unit tests for auth flow and audit behaviour

Open yuqi1129 opened this issue 5 months ago • 3 comments

Describe the subtask

Add unit tests covering the authentication flow and audit logging introduced in the preceding subtasks.

Changes:

  • New tests/unit/test_auth_flow.py: verify token is injected into httpx headers; verify missing token raises when required; verify anonymous fallback when token is empty
  • New tests/unit/test_audit.py: verify AuditLogger writes correct JSON; verify AuditMiddleware records allow on success and deny on GravitinoException with auth error

Acceptance:

  • uv run pytest tests/unit/ passes with all new tests green

Parent issue

#11573

yuqi1129 avatar Jun 10 '26 12:06 yuqi1129

Hi, I’d like to work on this issue. I plan to add the specified auth-flow and audit unit tests and verify the full unit-test suite with uv run pytest tests/unit/. If this is still available, could you please assign it to me?

shubhamshettyy avatar Jul 19 '26 06:07 shubhamshettyy

A test worth adding here, because there is a case the current suite structurally cannot reach.

_extract_principal (mcp-server/mcp_server/core/audit.py:57) returns f"{scheme}:{credential[:8]}" for a bearer token. For a JWT, the first 8 characters are the base64url of the header, not anything caller-specific — so every JWT issued by the same issuer with the same algorithm produces the same principal.

Run against the shipped function, three different subjects:

sub=alice -> 'bearer:eyJhbGci'
sub=bob   -> 'bearer:eyJhbGci'
sub=carol -> 'bearer:eyJhbGci'
distinct principals: 1 of 3

Controls from the same run, to show the function can discriminate and the rig works: Basic for alice and bob give 'alice' and 'bob'; empty gives 'anonymous'; and this file's own "Bearer abcdefghijklmnop" gives 'bearer:abcdefgh'. It generalises past one header shape — {"typ","alg"} collapses to 'bearer:eyJ0eXAi' and a Keycloak-style {"alg","typ","kid"} to 'bearer:eyJhbGci', 4 subjects to 1 principal in each case.

This is not an argument against truncating. Keeping token material out of the log is right, and the docstring says plainly what it does. The point is narrower: the epic's objective is that "every access produces an attributable audit record", and #11572 (per-request token isolation, multi-principal) is closed — so the multi-user configuration this was built for is exactly the one where every JWT caller lands on a single principal. The record attributes to the issuer configuration rather than to the caller.

Why no test catches it. mcp-server/tests/unit/test_audit.py contains four bearer inputs — "Bearer abcdefghijklmnop", "Bearer abc", "Bearer", "Bearer " — all synthetic strings whose leading bytes differ by construction, and zero assertNotEqual against 26 assertEqual. No test compares two tokens, so "different callers produce different principals" is never asserted. The suite pins the shape of one principal, not the discrimination between two.

The two-line version:

def test_two_jwt_callers_get_distinct_principals(self):
    self.assertNotEqual(_extract_principal(f"Bearer {jwt_for('alice')}"),
                        _extract_principal(f"Bearer {jwt_for('bob')}"))

That fails today and is the property the epic actually needs. Whether the fix is a salted hash of the credential, a window that isn't the header, or decoding sub when the token parses as a JWT, is a separate call — but the test states the requirement independently of which one you pick.

Santoshkumarpuppala avatar Sep 03 '26 05:09 Santoshkumarpuppala

@Santoshkumarpuppala Thanks for the detailed analysis and reproducer. Agreed—the current token-prefix approach cannot distinguish these JWT callers, and the tests miss that distinction.

Would you be willing to include the fix in the scope of this issue, along with a regression test using tokens for different users that share the same JWT header? Please keep credentials out of the audit log and avoid treating unverified JWT claims as an authenticated identity. Feel free to propose the approach in your PR.

yuqi1129 avatar Oct 27 '26 07:10 yuqi1129