Skip to content

fix(auth): validate credential formats in kaggle.json and access_token - #1241

Merged
stevemessick merged 2 commits into
mainfrom
fix/validate-auth-key-format
Nov 4, 2026
Merged

stevemessick merged 2 commits into
mainfrom
fix/validate-auth-key-format

Conversation

@stevemessick

Copy link
Copy Markdown
Contributor

Summary

Validates credential formats upfront during authentication and prints actionable error messages when new KGAT_ API access tokens and legacy kaggle.json credentials are mixed up or malformed:

  • KGAT_ token placed in kaggle.json or KAGGLE_KEY:
    • KaggleApi.read_config_file() detects raw KGAT_ tokens or malformed JSON in ~/.kaggle/kaggle.json instead of silently ignoring parse errors, and guides the user/agent to ~/.kaggle/access_token or KAGGLE_API_TOKEN.
    • KaggleApi._authenticate_with_legacy_apikey() rejects KGAT_ / KGRT_ tokens configured in KAGGLE_KEY or kaggle.json (key, token, api_token, apiToken), removes the invalid key from config_values, and prints clear instructions.
  • Legacy credentials or malformed tokens placed in ~/.kaggle/access_token or KAGGLE_API_TOKEN:
    • KaggleApi._validate_access_token_format() fails fast before calling _introspect_token() when access_token or KAGGLE_API_TOKEN contains legacy JSON credentials ({"username": "...", "key": "..."}), a JSON-wrapped KGAT_ token, a bare 32-character hex legacy API key without KGAT_, an OAuth refresh token (KGRT_...), quotes/KEY=VALUE syntax, or a truncated/malformed KGAT_ token.
    • KaggleApi._introspect_token() also catches HTTP 401 responses alongside (400, 403, 404) so invalid or expired bearer tokens do not raise an unhandled HTTPError.

Test Plan

  • Added 14 unit tests in tests/unit/test_authenticate.py covering all credential format mismatches as well as valid KGAT_ and OAuth bearer tokens.
  • Verified the full unit test suite (1450 passed) and black / mypy.

@stevemessick
stevemessick force-pushed the fix/validate-auth-key-format branch from 9f388eb to 8bbc867 Compare November 4, 2026 22:35
@stevemessick
stevemessick requested a review from rosbo November 4, 2026 22:37
with patch("sys.argv", ["kaggle", "datasets", "download", "some/dataset"]):
self.assertFalse(api._authenticate_with_legacy_apikey())

def test_kgat_token_in_kaggle_key_env_var_is_rejected(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we already have tests that check the happy path? (i.e. valid token or kaggle.json format).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes! We have:

  • test_valid_kgat_token_in_access_token_succeeds (for valid KGAT_<32-hex> tokens)
  • test_oauth_bearer_token_in_access_token_succeeds (for valid OAuth bearer tokens)
  • test_environment_variables (for legacy KAGGLE_USERNAME / KAGGLE_KEY env vars)
  • Added test_valid_kaggle_json_file_succeeds in the latest commit to also cover reading a valid kaggle.json file from disk.

@stevemessick

Copy link
Copy Markdown
Contributor Author

Thanks for the quick review!

@stevemessick
stevemessick merged commit fdc788f into main Nov 4, 2026
15 checks passed
@stevemessick
stevemessick deleted the fix/validate-auth-key-format branch November 4, 2026 22:51
@sridipbasu sridipbasu added the bug Something isn't working label Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants