Skip to content

fix: pass reason= keyword arg to web.HTTPBadRequest in emu_svc#51

Open
deacon-mp wants to merge 2 commits intomasterfrom
fix/emu-http-bad-request-positional-arg
Open

fix: pass reason= keyword arg to web.HTTPBadRequest in emu_svc#51
deacon-mp wants to merge 2 commits intomasterfrom
fix/emu-http-bad-request-positional-arg

Conversation

@deacon-mp
Copy link
Copy Markdown
Contributor

Summary

handle_forwarded_beacon in emu_svc.py was calling web.HTTPBadRequest(error_msg) with a positional argument. In aiohttp >= 3.x the exception constructor does not accept positional message arguments — the message must be passed as reason=<str>.

Before:

raise web.HTTPBadRequest(error_msg)

After:

raise web.HTTPBadRequest(reason=error_msg)

Impact

When the forwarded beacon handler catches any exception (e.g. invalid JSON from the forwarded agent, network error, unexpected server error), the HTTPBadRequest constructor itself raises TypeError: __init__() takes 1 positional argument but 2 were given. This causes an unhandled exception in the aiohttp request handler, resulting in a 500 Internal Server Error instead of the intended 400 Bad Request response.

Test plan

  • Send a malformed forwarded beacon request and confirm a 400 response is returned (not 500)
  • Confirm existing emu plugin tests still pass

aiohttp >= 3.x raises TypeError when web.HTTPBadRequest() is called with
a positional argument. The error message must be passed as reason=<str>.

Affected: handle_forwarded_beacon exception handler in emu_svc.py
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an aiohttp exception-construction bug in the emu forwarded beacon HTTP handler so error paths correctly return a 400 response instead of failing with a TypeError and producing a 500.

Changes:

  • Update handle_forwarded_beacon to raise web.HTTPBadRequest using the reason= keyword argument (aiohttp 3.x+ compatible).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 59 to +62
except Exception as e:
error_msg = 'Server error when processing forwarded beacon: %s' % e
self.log.error(error_msg)
raise web.HTTPBadRequest(error_msg)
raise web.HTTPBadRequest(reason=error_msg)
Adds a test verifying that handle_forwarded_beacon raises web.HTTPBadRequest
with a non-empty reason= kwarg when the request body contains invalid JSON,
covering the runtime failure path fixed in the previous commit.
@deacon-mp deacon-mp requested a review from Copilot March 16, 2026 04:25
@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
C Reliability Rating on New Code (required ≥ A)

See analysis details on SonarQube Cloud

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes handle_forwarded_beacon to raise aiohttp.web.HTTPBadRequest using the supported reason= keyword argument (avoiding a TypeError that previously caused 500s instead of 400s).

Changes:

  • Update emu_svc.handle_forwarded_beacon to call web.HTTPBadRequest(reason=...).
  • Add a regression test covering malformed JSON causing HTTPBadRequest to be raised.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
app/emu_svc.py Fixes aiohttp exception construction to avoid TypeError and return intended 400 responses.
tests/test_emu_svc.py Adds regression coverage to ensure malformed forwarded beacon bodies result in HTTPBadRequest.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

error_msg = 'Server error when processing forwarded beacon: %s' % e
self.log.error(error_msg)
raise web.HTTPBadRequest(error_msg)
raise web.HTTPBadRequest(reason=error_msg)
Comment on lines +257 to +258
assert exc_info.value.reason is not None
assert len(exc_info.value.reason) > 0
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an aiohttp compatibility issue in the forwarded beacon handler so it reliably returns a 400 Bad Request instead of erroring while constructing the exception (500).

Changes:

  • Update handle_forwarded_beacon to pass the error message via reason= to web.HTTPBadRequest (aiohttp 3.x compatible).
  • Add a regression test ensuring malformed JSON in the forwarded beacon request results in HTTPBadRequest.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
app/emu_svc.py Fixes HTTPBadRequest construction to avoid TypeError and restore intended 400 behavior.
tests/test_emu_svc.py Adds regression coverage for malformed JSON triggering HTTPBadRequest.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

error_msg = 'Server error when processing forwarded beacon: %s' % e
self.log.error(error_msg)
raise web.HTTPBadRequest(error_msg)
raise web.HTTPBadRequest(reason=error_msg)
Comment on lines +257 to +258
assert exc_info.value.reason is not None
assert len(exc_info.value.reason) > 0
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