Repository navigation
fix(auth): validate credential formats in kaggle.json and access_token - #1241
Merged
Merged
Conversation
stevemessick
force-pushed
the
fix/validate-auth-key-format
branch
from
November 4, 2026 22:35
9f388eb to
8bbc867
Compare
rosbo
approved these changes
Nov 4, 2026
| 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): |
Contributor
There was a problem hiding this comment.
Do we already have tests that check the happy path? (i.e. valid token or kaggle.json format).
Contributor
Author
There was a problem hiding this comment.
Yes! We have:
test_valid_kgat_token_in_access_token_succeeds(for validKGAT_<32-hex>tokens)test_oauth_bearer_token_in_access_token_succeeds(for valid OAuth bearer tokens)test_environment_variables(for legacyKAGGLE_USERNAME/KAGGLE_KEYenv vars)- Added
test_valid_kaggle_json_file_succeedsin the latest commit to also cover reading a validkaggle.jsonfile from disk.
Contributor
Author
|
Thanks for the quick review! |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Validates credential formats upfront during authentication and prints actionable error messages when new
KGAT_API access tokens and legacykaggle.jsoncredentials are mixed up or malformed:KGAT_token placed inkaggle.jsonorKAGGLE_KEY:KaggleApi.read_config_file()detects rawKGAT_tokens or malformed JSON in~/.kaggle/kaggle.jsoninstead of silently ignoring parse errors, and guides the user/agent to~/.kaggle/access_tokenorKAGGLE_API_TOKEN.KaggleApi._authenticate_with_legacy_apikey()rejectsKGAT_/KGRT_tokens configured inKAGGLE_KEYorkaggle.json(key,token,api_token,apiToken), removes the invalid key fromconfig_values, and prints clear instructions.~/.kaggle/access_tokenorKAGGLE_API_TOKEN:KaggleApi._validate_access_token_format()fails fast before calling_introspect_token()whenaccess_tokenorKAGGLE_API_TOKENcontains legacy JSON credentials ({"username": "...", "key": "..."}), a JSON-wrappedKGAT_token, a bare 32-character hex legacy API key withoutKGAT_, an OAuth refresh token (KGRT_...), quotes/KEY=VALUEsyntax, or a truncated/malformedKGAT_token.KaggleApi._introspect_token()also catches HTTP401responses alongside(400, 403, 404)so invalid or expired bearer tokens do not raise an unhandledHTTPError.Test Plan
tests/unit/test_authenticate.pycovering all credential format mismatches as well as validKGAT_and OAuth bearer tokens.1450 passed) andblack/mypy.