fix: tolerate unknown SocketCategory values in SocketAlert.from_dict#79
Open
David Larsen (dc-larsen) wants to merge 2 commits intomainfrom
Open
fix: tolerate unknown SocketCategory values in SocketAlert.from_dict#79David Larsen (dc-larsen) wants to merge 2 commits intomainfrom
David Larsen (dc-larsen) wants to merge 2 commits intomainfrom
Conversation
The Socket API can emit category values the SDK does not yet know about (e.g. "other"). Strict enum construction in SocketAlert.from_dict turned that into a hard failure that propagated up through stream_diff and crashed any consumer that happened to receive such an alert. Fall back to SocketCategory.MISCELLANEOUS and log a warning when the value is unrecognized, so the SDK stays forward-compatible with new server-side categories without needing a coordinated release. Fixes #78.
|
🚀 Preview package published! Install with: pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple socketdev==3.0.33.dev1 |
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
Fall back to
SocketCategory.MISCELLANEOUSwith a warning whenSocketAlert.from_dictencounters an unknowncategoryvalue instead of raisingValueError. Adds regression tests covering the known-value round trip, the unknown-value fallback, the warning log, and every currently defined category.Fixes #78.
Problem
SocketAlert.from_dictcallsSocketCategory(data["category"])directly. If the API emits any value outside the current enum (supplyChainRisk | quality | maintenance | vulnerability | license | miscellaneous), the constructor raisesValueErrorand takes down the entire deserialization chain for the diff response:Stack trace from the original report (issue #78):
The reported trigger was the API returning
"other"as an alert category. Because the error happens during response parsing, any consumer that callsstream_diffwithuse_types=Truefails hard whenever a diff contains one of these alerts.socketsecurityhardcodesuse_types=Trueat its single call site, so it has no toggle to work around this.Fix
Catch
ValueError, log a warning with the unrecognized value, and fall back toSocketCategory.MISCELLANEOUS:MISCELLANEOUSis already the "doesn't fit the other buckets" member of the enum, so reusing it matches the semantic intent without adding a new value to the public contract. The warning surfaces the gap in logs without blocking deserialization.Why this over the alternatives
OTHER = "other"explicitly. Fixes the current symptom but leaves the SDK brittle to any future server-side category addition. The next new value would cause the same hard failure.UNKNOWN = "unknown"enum value. Explicit, but introduces a member the server never emits, and every downstream consumer would need to learn about it.MISCELLANEOUSis already in the contract.Option 1 ("lenient deserialization") is the approach requested in the issue body.
Test plan
python -m pytest tests/unit/test_socket_alert_category.py -v(5 tests, 6 subtests pass):MISCELLANEOUSValueErrorraised for unknown valuesSocketCategoryround trips throughfrom_dictpython -m pytest tests/unit/ -v(99 passed, 1 pre-existing skip, no regressions)SocketAlert.from_dict({"key": "k", "type": "t", "severity": "low", "category": "other"})now returns an alert withcategory == SocketCategory.MISCELLANEOUSinstead of raising.Release note
socketdev.fullscans.SocketAlert.from_dictnow accepts unknowncategoryvalues, bucketing them asMISCELLANEOUSand emitting a warning log. Parsing behavior for known values is unchanged.Version bumped to 3.0.33 to satisfy the
check_versionCI step.