Repository navigation
Clamp attacker-controlled event fields instead of rejecting the batch. Closes #1812 - #1834
Conversation
regulartim
left a comment
There was a problem hiding this comment.
Hey @dev-aditya-hub ! Nice fix, thank you. You tend to write very verbose comments. Can you strip them down a bit? Make sure they are concise and correct.
| """URL field that drops an unusable value instead of failing the batch. | ||
|
|
||
| The honeypot records whichever URL the attacker asked it to fetch, so the | ||
| value is attacker-controlled and may be malformed or wider than the column. | ||
| ``_process_related_urls`` already drops an over long URL rather than storing | ||
| it, and ``_normalize_raw_event_to_hit`` re-checks ``is_valid_url`` before the | ||
| value is used, so dropping it here loses nothing that would have been kept. | ||
|
|
||
| ``serializers.URLField`` cannot be subclassed for this: its ``URLValidator`` | ||
| lives in ``self.validators`` and runs after ``to_internal_value``, so a | ||
| dropped value would then fail validation as an empty string. | ||
| """ | ||
|
|
||
| def to_internal_value(self, data): | ||
| value = super().to_internal_value(data) | ||
| if not value or not fits_field(RawEvent, "related_url", value): | ||
| return "" | ||
| try: | ||
| URLValidator()(value) | ||
| except DjangoValidationError: | ||
| return "" | ||
| return value | ||
|
|
||
|
|
There was a problem hiding this comment.
Your comment says that unusable values are dropped, but I think the class lets some of them still pass. Can you add test cases for these URLs, please:
- http://testi.test/a\x00b (NUL)
- http://testi.test/a\ud800b (lone surrogate)
There was a problem hiding this comment.
thanks for the proper review you're right Those two are not dropped URLValidator accepts them so to_internal_value returns the value as is and then the null/surrogate validators reject the field which fails the whole batch exactly what this PR is meant to prevent I'll have LenientURLField drop URLs containing a NUL or a lone surrogate to as well and add both as test cases
| def _post_mixed(self, dirty_event, extra_clean=2): | ||
| events = [valid_event(self.sensor.id) for _ in range(extra_clean)] | ||
| events.append(dirty_event) | ||
| return self.client.post(EVENTS_URL, {"events": events}, format="json") | ||
|
|
||
| def _assert_batch_saved(self, res, expected_count): | ||
| self.assertEqual(res.status_code, status.HTTP_202_ACCEPTED) | ||
| task_id = res.data["task_id"] | ||
| self.assertEqual(RawEvent.objects.filter(batch__task_id=task_id).count(), expected_count) | ||
| batch = EventStatus.objects.get(task_id=task_id) | ||
| self.assertNotEqual(batch.status, EventStatusType.FAILED) | ||
|
|
There was a problem hiding this comment.
These are exact copies of methods in TestEventSanitizationKeepsBatch, right? If so, please inherit from that class or create a common base class.
There was a problem hiding this comment.
Good point pulled _post_mixed and _assert_batch_saved into a BatchSanitizationTestCase base and both classes inherit it now, no more duplication. Added the NUL and lone-surrogate URL cases there too.
Thanks for the review ! :)
|
Ah, also @dev-aditya-hub , could you add the "AI disclosure" section? The PR template already contains it. |
…Closes GreedyBear-Project#1812 InjectionSerializer validates a batch as one unit, so a single event that fails validation rejects the whole request and nothing is staged. Several EventSerializer fields hold strings the attacker types at the honeypot rather than values the operator's agent picks, so one over long username or one malformed download URL discarded up to 10,000 valid events. Clamp username, password, session_id and protocol to the width of their column, the way normalize_credential_field already does on the T-Pot path, and drop an unusable related_url to an empty string. _process_related_urls already drops an over long URL rather than storing it, and _normalize_raw_event_to_hit re-checks is_valid_url before the value is used, so dropping it here loses nothing that would have been kept. LenientURLField subclasses CharField rather than URLField because URLField's URLValidator lives in self.validators and runs after to_internal_value, so a dropped value would fail as an empty string. This reverses a deliberate decision from GreedyBear-Project#1707: sanitize_text returned a clean over long value whole so that it still failed validation. Its docstring and the test that pinned it are updated accordingly.
…#1812 A related_url with a NUL or a lone surrogate passes URLValidator, so LenientURLField returned it unchanged. The model's validators then reject it and the whole batch fails - the same thing the clamp was meant to avoid. Screen for those characters in to_internal_value and drop the URL to an empty string, like any other unusable value, and cover both through the batch tests. Lift the shared _post_mixed/_assert_batch_saved helpers into a BatchSanitizationTestCase base so the two batch test classes don't repeat them.
Added the AI disclosure section to the description :) |
regulartim
left a comment
There was a problem hiding this comment.
Hey @dev-aditya-hub ! Still, the doc strings are a bit too verbose for my taste, but that's subjective. Thanks, for your work, we can merge thins.
@dev-aditya-hub I have clear evidence that not only the code, but also the description was written by Claude Code. The agent itself raised the PR originally and wrote an honest AI disclosure. You deleted it with the intent to deceive me. I am very sorry, but this is unacceptable for me. I don't want to spent my time with people who play such games. |
Description
InjectionSerializervalidates a batch as one unit, so a single event that fails validation rejects the whole request and nothing is staged. SeveralEventSerializerfields hold strings the attacker types at the honeypot rather than values the operator's agent picks, so one over-long username or one malformed download URL discarded up to 10,000 valid events.This clamps
username,password,session_idandprotocolto the width of their column, the waynormalize_credential_fieldalready does on the T-Pot path, and drops an unusablerelated_urlto an empty string._process_related_urlsalready drops an over-long URL rather than storing it, and_normalize_raw_event_to_hitre-checksis_valid_urlbefore the value is used, so dropping it here loses nothing that would have been kept.LenientURLFieldsubclassesCharFieldrather thanURLFieldbecauseURLField'sURLValidatorlives inself.validatorsand runs afterto_internal_value, so a dropped value would fail as an empty string.This reverses a deliberate decision from #1707:
sanitize_textreturned a clean over-long value whole so that it still failed validation. Its docstring and the test that pinned it are updated accordingly.Related issues
Closes #1812. Context: #1707 (the decision this reverses).
AI disclosure
The bug fix code was written with LLM assistance and fully reviewed and verified by me. All written content this description, the PR text, and the GitHub comments is my own.
Type of change
Checklist
Formalities
<feature name>. Closes #999develop.develop.Docs and tests
Ruff) gave 0 errors. If you have correctly installed pre-commit, it does these checks and adjustments on your behalf.GUI changes