Skip to content

feat: add middleware to log client IP for every API request#705

Open
Xaxxoo wants to merge 1 commit into
SoroScan:mainfrom
Xaxxoo:feat/ip-logging-middleware-426
Open

feat: add middleware to log client IP for every API request#705
Xaxxoo wants to merge 1 commit into
SoroScan:mainfrom
Xaxxoo:feat/ip-logging-middleware-426

Conversation

@Xaxxoo
Copy link
Copy Markdown

@Xaxxoo Xaxxoo commented Jun 1, 2026

Closes #426

Summary

  • Adds ClientIPLoggingMiddleware to soroscan/middleware.py that logs the HTTP method, request path, and client IP (from REMOTE_ADDR) for every non-static API request via the soroscan.ip_access logger.
  • Placed in MIDDLEWARE immediately after ReverseProxyFixedIPMiddleware so REMOTE_ADDR already holds the real client IP when resolved through X-Forwarded-For.
  • Static-asset paths (/static/, /media/, /favicon.ico) are skipped to avoid log noise.
  • Registers the soroscan.ip_access logger in settings.py.

Test plan

  • test_logs_method_path_and_ip — confirms method, path, and IP appear in log output
  • test_logs_post_request — verifies POST requests are logged
  • test_static_files_not_logged — static/media/favicon paths produce no log records
  • test_unknown_ip_fallback — missing REMOTE_ADDR logs "unknown"
  • test_extra_fields_attachedclient_ip, method, path are present as log-record extras
  • test_proxy_ip_used_after_remote_addr_override — IP already resolved by proxy middleware is logged correctly

Closes SoroScan#426

- Add ClientIPLoggingMiddleware that extracts REMOTE_ADDR (already
  corrected by ReverseProxyFixedIPMiddleware) and logs the HTTP method,
  path, and client IP via the soroscan.ip_access logger.
- Static-asset paths (/static/, /media/, /favicon.ico) are excluded
  to avoid noise.
- Register middleware in MIDDLEWARE after ReverseProxyFixedIPMiddleware
  so the IP is already resolved when the log entry is written.
- Add soroscan.ip_access logger config in settings.py.
- Add test_ip_logging_middleware.py covering: normal requests, static
  exclusion, unknown-IP fallback, extra log-record fields, and
  proxy-resolved IP.
@drips-wave
Copy link
Copy Markdown

drips-wave Bot commented Jun 1, 2026

@Xaxxoo Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

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.

Add middleware to log the IP address of every incoming request

1 participant