Skip to content
Snippets Groups Projects

Set correct value of X-Forwarded-For header in PreAuthorize request

Merged username-removed-378947 requested to merge x-forwarded-for-pre-authorize into master
All threads resolved!

Closes #82 (closed)

This is my first "real" Go code :trumpet:. Shamelessly copied from https://github.com/golang/go/blob/5bfba30d3325d87ef89dd877f05e5d1e2d618bc3/src/net/http/httputil/reverseproxy.go#L193.

If you are curious why strings.Join(prior, ", ") is necessary:

Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list [i.e., #(values)]. It MUST be possible to combine the multiple header fields into one "field-name: field-value" pair, without changing the semantics of the message, by appending each subsequent field-value to the first, each separated by a comma. The order in which header fields with the same field-name are received is therefore significant to the interpretation of the combined field value, and thus a proxy MUST NOT change the order of these field values when a message is forwarded.

This probably needs some tests.

/cc @nick.thomas

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Now that I look at it again, I think the two tests can be combined in one single table-driven test.

    testCases := []struct{
    remoteAddr string
    header *http.Header
    expected string
    }{
    {"8.8.8.8:3000", nil, "8.8.8.8"},
    // etc.
    }
    for tc := range testCases {
      headers := http.Header{}
      originalRequest := http.Request{RemoteAddr: tc.remoteAddr}
      // etc.
    }
    Edited by Jacob Vosmaer (GitLab)
  • added 1 commit

    • 896251b1 - Set correct value of X-Forwarded-For header in PreAuthorize request

    Compare with previous version

  • username-removed-378947 resolved all discussions

    resolved all discussions

  • @jacobvosmaer-gitlab Thanks for the review. I followed your suggestion and added a third test case, because it was easier :smile:.

  • Test table looks great, do have another question for you. :)

  • @jacobvosmaer-gitlab The second test case checks a single X-Forwarded-For header with two IPs separated by comma ("138.124.33.63, 151.146.211.237"). The third test case checks two X-Forwarded-For headers with single IP in each ("8.154.76.107", "115.206.118.179"). It's because:

    Multiple message-header fields with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list

    Visually the difference is small, but I think that it's nice to also check for this case.

  • Thanks, yes those are two different things and worth checking.

  • Jacob Vosmaer (GitLab) resolved all discussions

    resolved all discussions

  • Nick Thomas mentioned in commit 9b0d5297

    mentioned in commit 9b0d5297

  • merged

  • Great job, nothing to add from me :)

  • Please register or sign in to reply
    Loading