Repository navigation
Reject retired credentials in CLI transports #397
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
Open
John-David Dalton (jdalton)
wants to merge
1
commit into
main
Choose a base branch
from
jdalton/retired-credential-recovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| __author__ = 'socket.dev' | ||
| __version__ = '2.10.11' | ||
| __version__ = '2.10.12' | ||
| USER_AGENT = f'SocketPythonCLI/{__version__}' |
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
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,28 @@ | ||
| import hashlib | ||
| import hmac | ||
| from typing import Optional | ||
|
|
||
| RETIRED_API_TOKEN_SHA256 = ( | ||
| "9cab58b24140ee12c08e265729d246f9200f8ef9b1d884f90c06c4e6b69adf18" | ||
| ) | ||
| RETIRED_API_TOKEN_ERROR = ( | ||
| "This API token has been retired. Create a new API token in Socket " | ||
| "and update your configuration." | ||
| ) | ||
|
|
||
|
|
||
| # Fingerprint identifies a retired credential; it is not a password verifier. | ||
| def _matches_retired_digest(token: Optional[str], digest: str) -> bool: | ||
| if token is None: | ||
| return False | ||
| token_digest = hashlib.sha256(token.strip().encode("utf-8")).hexdigest() | ||
|
jdalton marked this conversation as resolved.
Dismissed
|
||
| return hmac.compare_digest(token_digest, digest) | ||
|
|
||
|
|
||
| def _reject_retired_api_token(token: Optional[str], digest: str) -> None: | ||
| if _matches_retired_digest(token, digest): | ||
| raise ValueError(RETIRED_API_TOKEN_ERROR) | ||
|
|
||
|
|
||
| def reject_retired_api_token(token: Optional[str]) -> None: | ||
| _reject_retired_api_token(token, RETIRED_API_TOKEN_SHA256) | ||
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,73 @@ | ||
| import hashlib | ||
|
|
||
| import pytest | ||
| from socketdev import socketdev | ||
|
|
||
| from socketsecurity.config import ( | ||
| API_TOKEN_ENVIRONMENT_VARIABLES, | ||
| CliConfig, | ||
| select_api_token, | ||
| ) | ||
| from socketsecurity.core.cli_client import CliClient | ||
| from socketsecurity.core.socket_config import SocketConfig | ||
| from socketsecurity.core.tools.reachability import ReachabilityAnalyzer | ||
| from socketsecurity.retired_api_token import ( | ||
| RETIRED_API_TOKEN_ERROR, | ||
| _matches_retired_digest, | ||
| _reject_retired_api_token, | ||
| ) | ||
|
|
||
|
|
||
| def test_retired_api_token_rejection_redacts_token_and_digest(): | ||
| token = "synthetic-retired-token" | ||
| digest = hashlib.sha256(token.encode()).hexdigest() | ||
|
|
||
| with pytest.raises(ValueError) as exc_info: | ||
| _reject_retired_api_token(token, digest) | ||
|
|
||
| assert str(exc_info.value) == RETIRED_API_TOKEN_ERROR | ||
| assert token not in str(exc_info.value) | ||
| assert digest not in str(exc_info.value) | ||
| assert _matches_retired_digest(f" {token} ", digest) | ||
|
|
||
|
|
||
| def test_cli_config_preserves_token_environment_precedence(monkeypatch): | ||
| for name in API_TOKEN_ENVIRONMENT_VARIABLES: | ||
| monkeypatch.delenv(name, raising=False) | ||
| monkeypatch.setenv(API_TOKEN_ENVIRONMENT_VARIABLES[0], "first-env-token") | ||
| monkeypatch.setenv(API_TOKEN_ENVIRONMENT_VARIABLES[1], "second-env-token") | ||
|
|
||
| config = CliConfig.from_args(["--api-token", "argument-token"]) | ||
|
|
||
| assert config.api_token == "first-env-token" | ||
|
|
||
|
|
||
| def test_cli_config_uses_argument_when_token_environment_is_empty(monkeypatch): | ||
| for name in API_TOKEN_ENVIRONMENT_VARIABLES: | ||
| monkeypatch.delenv(name, raising=False) | ||
|
|
||
| config = CliConfig.from_args(["--api-token", "argument-token"]) | ||
|
|
||
| assert config.api_token == "argument-token" | ||
|
|
||
|
|
||
| def test_cli_http_and_reachability_construction_accept_safe_token_without_network(): | ||
| token = "synthetic-safe-token" | ||
| config = SocketConfig(api_key=token) | ||
| client = CliClient(config) | ||
| sdk = socketdev(token=token) | ||
| analyzer = ReachabilityAnalyzer(sdk, token) | ||
|
|
||
| assert client.config.api_key == token | ||
| assert analyzer.api_token == token | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("token", [None, "different-token"]) | ||
| def test_nonretired_api_token_is_accepted(token): | ||
| digest = hashlib.sha256(b"synthetic-retired-token").hexdigest() | ||
| _reject_retired_api_token(token, digest) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("name", API_TOKEN_ENVIRONMENT_VARIABLES) | ||
| def test_each_cli_environment_source_is_supported(name): | ||
| assert select_api_token({name: "selected-token"}, "argument-token") == "selected-token" |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Retired-token error escapes CLI handler
Medium Severity
reject_retired_api_tokenraisesValueErrorfromCliConfig.from_args.clicatches that and callsfrom_argsagain to read--disable-blockingand--exit-code-on-api-error. The second call raises the same error, so_emit_infrastructure_errornever runs and those flags are ignored. Operators get an uncaught traceback instead of the CLI's formatted infrastructure error path.Reviewed by Cursor Bugbot for commit cfada31. Configure here.