Repository navigation
Validate effect bytes before native force-feedback upload #262
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
omrzk
wants to merge
1
commit into
gvalkov:main
Choose a base branch
from
omrzk:fix/validate-upload-effect-data-261
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| """Validate the native input API without requiring an input device.""" | ||
|
|
||
| import ctypes | ||
| import errno | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import pytest | ||
|
|
||
| from evdev import _input, device, ff | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("expression", ["None", "0", "'effect'", "[]", "bytearray(64)", "memoryview(bytes(64))"]) | ||
| def test_upload_effect_rejects_non_bytes(expression): | ||
| # A conversion failure used to dereference NULL. Keep a broken extension | ||
| # from crashing the test runner, and do not leave core dumps behind. | ||
| code = """ | ||
| import resource | ||
| import sys | ||
| from evdev import _input | ||
| resource.setrlimit(resource.RLIMIT_CORE, (0, 0)) | ||
| try: | ||
| _input.upload_effect(-1, eval(sys.argv[1])) | ||
| except TypeError: | ||
| pass | ||
| else: | ||
| raise AssertionError('expected TypeError') | ||
| """ | ||
| result = subprocess.run([sys.executable, "-c", code, expression], check=False, capture_output=True, text=True) | ||
| assert result.returncode == 0, result.stderr | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("size", [0, 1, ctypes.sizeof(ff.Effect) - 1]) | ||
| def test_upload_effect_rejects_short_bytes(size): | ||
| with pytest.raises(ValueError, match="effect data"): | ||
| _input.upload_effect(-1, bytes(size)) | ||
|
|
||
|
|
||
| class EffectBytes(bytes): | ||
| pass | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("data", [bytes(ff.Effect()), bytes(ff.Effect()) + b"extra", EffectBytes(bytes(ff.Effect()))]) | ||
| def test_upload_effect_preserves_ioctl_errors(data): | ||
| # A complete effect must reach ioctl, including bytes subclasses and the | ||
| # previously accepted trailing bytes. Invalid descriptors remain EBADF. | ||
| with pytest.raises(OSError) as error: | ||
| _input.upload_effect(-1, data) | ||
| assert error.value.errno == errno.EBADF | ||
|
|
||
|
|
||
| def test_device_upload_effect_uses_complete_structure(): | ||
| input_device = device.InputDevice.__new__(device.InputDevice) | ||
| input_device.fd = -1 | ||
| effect = ff.Effect() | ||
| effect.id = -1 | ||
| with pytest.raises(OSError) as error: | ||
| input_device.upload_effect(effect) | ||
| assert error.value.errno == errno.EBADF | ||
| assert effect.id == -1 | ||
| # No descriptor is owned by this stand-in InputDevice. | ||
| input_device.fd = None | ||
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.
wrapping bytes(ff.Effect()) in EffectBytes seems to be redundant
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.
This prints true:
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.
Or is there something I'm missing?
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.
The subclass case is intentional: equal payloads still reach the extension as different Python types. The first case covers plain
bytes;EffectByteschecks that validation continues to accept bytes subclasses, and the extra-data case preserves trailing-byte acceptance.You are right that the inner
bytes()conversion is redundant:EffectBytes(ff.Effect())produces the same subclass and payload. I checked all three expressions locally; each reaches ioctl and raisesEBADFwith fd -1. The subclass wrapper is what adds coverage here.