Skip to content

Clamp attacker-controlled event fields instead of rejecting the batch. Closes #1812 - #1834

Merged
regulartim merged 2 commits into
GreedyBear-Project:developfrom
dev-aditya-hub:fix/1812-clamp-attacker-controlled-event-fields
Nov 16, 2026
Merged

regulartim merged 2 commits into
GreedyBear-Project:developfrom
dev-aditya-hub:fix/1812-clamp-attacker-controlled-event-fields

Conversation

@dev-aditya-hub

@dev-aditya-hub dev-aditya-hub commented Nov 4, 2026 •

Copy link
Copy Markdown

Description

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.

This clamps 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 drops 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 #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.

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

  • Bug fix (non-breaking change which fixes an issue).
  • New feature (non-breaking change which adds functionality).
  • Breaking change (fix or feature that would cause existing functionality to not work as expected).
  • Chore (refactoring, dependency updates, CI/CD changes, code cleanup, docs-only changes).

Checklist

Formalities

  • I have read and understood the rules about how to Contribute to this project.
  • I have read and understood the AI policy.
  • I chose an appropriate title for the pull request in the form: <feature name>. Closes #999
  • My branch is based on develop.
  • The pull request is for the branch develop.
  • I have reviewed and verified any LLM-generated code included in this PR.
  • I am interested in participating in GSoC.

Docs and tests

  • I documented my code changes with docstrings and/or comments.
  • I have checked if my changes affect user-facing behavior that is described in the wiki. If so, I also included an update to the wiki in the description of this PR.
  • Linter (Ruff) gave 0 errors. If you have correctly installed pre-commit, it does these checks and adjustments on your behalf.
  • I have added tests for the feature/bug I solved.
  • All the tests gave 0 errors.

GUI changes

  • I have provided a screenshot of the result in the PR.
  • I have created new frontend tests for the new component or updated existing ones.

@regulartim regulartim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread api/serializers/events.py
Comment on lines +156 to +179
"""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


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread tests/api/views/test_event.py Outdated
Comment on lines +613 to +624
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are exact copies of methods in TestEventSanitizationKeepsBatch, right? If so, please inherit from that class or create a common base class.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ! :)

@regulartim

Copy link
Copy Markdown
Member

Ah, also @dev-aditya-hub , could you add the "AI disclosure" section? The PR template already contains it.

dev-aditya-hub added 2 commits November 12, 2026 16:06
…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.
@dev-aditya-hub

Copy link
Copy Markdown
Author

Ah, also @dev-aditya-hub , could you add the "AI disclosure" section? The PR template already contains it.

Added the AI disclosure section to the description :)

@regulartim regulartim left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@regulartim
regulartim merged commit fa1cf67 into GreedyBear-Project:develop Nov 16, 2026
7 checks passed
@regulartim

Copy link
Copy Markdown
Member

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.

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants