[PR #603] [MERGED] fix(log): record user real ip from headers #688

Closed
opened 2026-02-25 23:35:08 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/go-shiori/shiori/pull/603
Author: @Tomilla
Created: 4/18/2023
Status: Merged
Merged: 6/11/2023
Merged by: @fmartingr

Base: masterHead: get_user_real_ip_from_header


📝 Commits (10+)

  • f21e0e2 fix(real_ip): get user real ip from headers of request
  • 4ef5c34 fix(real_ip): compatible with those header with multiple IP values separated by commas
  • d3c8fe7 test(real_ip): add benchmark for IPv4 and IPv6 private address check
  • 9bd5231 fix(real_ip): check empty, then remove leading and tailing comma char, finally locate first IP field
  • c5c0b60 test(real_ip): move checker logic into utils and add more unit test cases
  • fb3100d test(real_ip): write unit tests covering all code branches of the util-ip module
  • 593ccd0 refactor(real_ip): use one-line testify.assert.Panics to capture intended panic in test case
  • 5d3681e chore(real_ip): add module private variable UserRealIpHeaderCandidates
  • 38e1522 doc(real_ip): write docstring for each function in the utils-ip module
  • 87e7a55 chore(real_ip): choose more concrete and unambiguous name for test helper function

📊 Changes

3 files changed (+447 additions, -2 deletions)

View changed files

📝 internal/webserver/server.go (+2 -2)
internal/webserver/utils_ip.go (+216 -0)
internal/webserver/utils_ip_test.go (+229 -0)

📄 Description

Fix the problem that the system log cannot record the user's real connection IP when the system is deployed in the container or behind the reverse proxy. Details of implement as follow:

  1. First, determine whether the remote addr of request is a private address. If it is a public network address, return it directly;
  2. If it is private, get the real IP from X-REAL-IP and X-Forwarded-For headers in turn. ( The code handles multiple comma-separated IP lists, that is, through multi-layer reverse proxy);
  3. If the above headers do not exist or are invalid, the remote addr is returned as is.

Thank you for reviewing my code.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/go-shiori/shiori/pull/603 **Author:** [@Tomilla](https://github.com/Tomilla) **Created:** 4/18/2023 **Status:** ✅ Merged **Merged:** 6/11/2023 **Merged by:** [@fmartingr](https://github.com/fmartingr) **Base:** `master` ← **Head:** `get_user_real_ip_from_header` --- ### 📝 Commits (10+) - [`f21e0e2`](https://github.com/go-shiori/shiori/commit/f21e0e275f2dab12bdf78be3e609f0c0cfe24eeb) fix(real_ip): get user real ip from headers of request - [`4ef5c34`](https://github.com/go-shiori/shiori/commit/4ef5c3483d29931975857d495d89346b0e2e9b56) fix(real_ip): compatible with those header with multiple IP values separated by commas - [`d3c8fe7`](https://github.com/go-shiori/shiori/commit/d3c8fe71d45497f27c61af56b6c47484da441c42) test(real_ip): add benchmark for IPv4 and IPv6 private address check - [`9bd5231`](https://github.com/go-shiori/shiori/commit/9bd5231f2529784fc494c32f8c144141ad0e7770) fix(real_ip): check empty, then remove leading and tailing comma char, finally locate first IP field - [`c5c0b60`](https://github.com/go-shiori/shiori/commit/c5c0b60b21dac3401d99ed98fbf002edb5a0019c) test(real_ip): move checker logic into utils and add more unit test cases - [`fb3100d`](https://github.com/go-shiori/shiori/commit/fb3100dbe3113faa92dd3ac94de69192d534d6eb) test(real_ip): write unit tests covering all code branches of the `util-ip` module - [`593ccd0`](https://github.com/go-shiori/shiori/commit/593ccd07feae3a07b9ddbe4e86a28961578563a0) refactor(real_ip): use one-line `testify.assert.Panics` to capture intended panic in test case - [`5d3681e`](https://github.com/go-shiori/shiori/commit/5d3681e275f8dfa17648c8eb062b863a360fa39d) chore(real_ip): add module private variable `UserRealIpHeaderCandidates` - [`38e1522`](https://github.com/go-shiori/shiori/commit/38e1522b95ccef29b0eb0b14e93c3f7b76aea6d5) doc(real_ip): write docstring for each function in the `utils-ip` module - [`87e7a55`](https://github.com/go-shiori/shiori/commit/87e7a55c6d15981c2cf72bd8b062f7b079d99783) chore(real_ip): choose more concrete and unambiguous name for test helper function ### 📊 Changes **3 files changed** (+447 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `internal/webserver/server.go` (+2 -2) ➕ `internal/webserver/utils_ip.go` (+216 -0) ➕ `internal/webserver/utils_ip_test.go` (+229 -0) </details> ### 📄 Description Fix the problem that the system log cannot record the user's real connection IP when the system is deployed in the container or behind the reverse proxy. Details of implement as follow: 1. First, determine whether the remote addr of request is a private address. If it is a public network address, return it directly; 2. If it is private, get the real IP from `X-REAL-IP` and `X-Forwarded-For` headers in turn. ( The code handles multiple comma-separated IP lists, that is, through multi-layer reverse proxy); 3. If the above headers do not exist or are invalid, the remote addr is returned as is. Thank you for reviewing my code. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-02-25 23:35:08 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/shiori#688
No description provided.