Skip to content

feat(config): Add TrustedProxies support to config #21

Merged
madalinpopa merged 16 commits intomainfrom
branch/push-nyzzpuwpsspy
Feb 7, 2026
Merged

feat(config): Add TrustedProxies support to config #21
madalinpopa merged 16 commits intomainfrom
branch/push-nyzzpuwpsspy

Conversation

@madalinpopa
Copy link
Copy Markdown
Owner

No description provided.

Refactor string slice env parsing for AllowedHosts and TrustedProxies.
Ensures robust handling of comma-separated values from environment
variables, including Docker scenarios.
- Add stack trace logging on panic recovery for better debugging
- Set CSRF cookie SameSite attribute to Lax for improved security
- Implement Unwrap method for responseWriter to support
  http.ResponseController
Enhances getClientIP to validate X-Forwarded-For only from trusted
proxies.
Adds helpers for trusted proxy checks, allowed hosts, and IP
normalization.
Adds unit tests for getClientIP and CheckAllowedHosts middleware,
covering trusted proxies, X-Forwarded-For handling, and allowed host
logic for domains, IPs, and CIDRs.
Ensure session is destroyed only if authenticated but user ID is
missing,
preventing possible session tampering.
Covers redirect, session destruction, error logging, and cache control.
@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @madalinpopa, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the application's network configuration and security by introducing explicit support for trusted proxies, which is crucial for accurate client IP identification in production environments. It also refines existing middleware functionalities, such as panic recovery and session management, and bolsters the codebase with new, comprehensive unit tests for these critical components. The changes ensure more robust and secure handling of incoming requests and application state.

Highlights

  • Trusted Proxies Configuration: Introduced a new configuration option, TrustedProxies, allowing the application to correctly identify client IP addresses when operating behind reverse proxies. This includes parsing comma-separated IP addresses or CIDR blocks from environment variables.
  • Refactored Host Validation: The logic for validating AllowedHosts has been refactored into a shared helper function, getStringSliceFromEnv, which also handles the parsing of TrustedProxies from environment variables, ensuring consistent behavior for slice-based configurations.
  • Enhanced Middleware Logic: Improvements were made to several middleware components: the Recover middleware now logs full stack traces for panics, SameSiteLaxMode was added to CSRF cookies for better security, and the LoginRequired middleware's session handling was refined to prevent unnecessary session destruction.
  • Comprehensive Unit Tests: A new test file, middleware_test.go, was added with extensive unit tests for getClientIP, CheckAllowedHosts, and LoginRequired middleware functions, covering various scenarios including trusted proxies, allowed hosts, and session management.
  • Makefile Updates: Updated Go commands in the Makefile (test, vet, staticcheck) to use ... for recursive directory scanning, ensuring all internal packages are covered.
Changelog
  • Makefile
    • Corrected Go test, vet, and staticcheck commands to use ... for recursive directory scanning.
  • envrc.template
    • Added a commented-out TRUSTED_PROXIES environment variable example for documentation.
  • internal/config/config.go
    • Added TrustedProxies field to the Config struct.
    • Initialized TrustedProxies as an empty string slice in NewWithLogger.
    • Introduced getStringSliceFromEnv helper function to parse comma-separated environment variables into string slices, handling both single-element and empty string cases.
    • Updated LoadEnvironments to utilize getStringSliceFromEnv for both AllowedHosts and TrustedProxies.
  • internal/interfaces/web/middleware.go
    • Replaced slices import with runtime/debug.
    • Added Unwrap method to the responseWriter struct for http.ResponseController compatibility.
    • Enhanced the Recover middleware to log the full stack trace when a panic occurs.
    • Added SameSiteLaxMode to the CSRF cookie options for improved security.
    • Refactored CheckAllowedHosts to use new isAllowedHost and normalizeHost helper functions for clearer and more robust host validation.
    • Modified getClientIP to incorporate TrustedProxies logic, using new isTrustedProxy and extractRemoteIP helper functions.
    • Introduced new helper functions: isTrustedProxy (checks if a remote IP is in the trusted proxies list), isAllowedHost (checks if a host is in the allowed hosts list, supporting CIDR), extractRemoteIP (parses IP from remote address), and normalizeHost (cleans up host strings).
    • Updated LoginRequired middleware to first check IsAuthenticated before attempting to destroy a session if userID is missing, preventing unnecessary session destruction.
  • internal/interfaces/web/middleware_test.go
    • Added a new test file for middleware functionalities.
    • Implemented stubAuthSessionManager and stubErrorHandler for mocking dependencies in tests.
    • Added unit tests for Middleware_getClientIP covering scenarios with trusted and untrusted proxies, X-Forwarded-For headers, and malformed addresses.
    • Added unit tests for Middleware_CheckAllowedHosts covering various host types (domain, IPv4, IPv4 CIDR, IPv6, IPv6 CIDR) and their validation.
    • Added unit tests for Middleware_LoginRequired covering unauthenticated users, authenticated users with missing user IDs (including error logging for session destruction), and successfully authenticated users.
Activity
  • The pull request introduces a new feature to support TrustedProxies in the application's configuration.
  • It includes significant refactoring of network-related middleware logic and configuration parsing.
  • Comprehensive unit tests have been added to validate the new and modified functionalities.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces support for TrustedProxies to correctly identify client IPs behind a proxy, which is a great security and functionality enhancement. The changes include updates to configuration, middleware for handling trusted proxies and allowed hosts, and improved panic recovery logging. The introduction of comprehensive tests for the new middleware logic is also a valuable addition.

My review focuses on a couple of performance considerations in the new middleware logic. Specifically, both isAllowedHost and isTrustedProxy functions re-process configuration strings on every request, which can be inefficient. I've suggested refactoring to pre-process these configurations once during middleware initialization. Overall, this is a solid contribution with significant improvements.

Comment thread internal/interfaces/web/middleware.go
Comment thread internal/interfaces/web/middleware.go Outdated
Trusted proxies are now parsed once and cached for efficient lookup,
reducing repeated parsing and improving performance.
Improve allowed host validation by normalizing hosts once and reusing
the
parsed list, reducing redundant normalization and improving performance.
@madalinpopa madalinpopa merged commit 3a5669a into main Feb 7, 2026
6 checks passed
@madalinpopa madalinpopa deleted the branch/push-nyzzpuwpsspy branch February 7, 2026 21:41
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.

1 participant