Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-workhorse
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-workhorse
Commits
896251b1
Commit
896251b1
authored
Jan 02, 2017
by
Adam Niedzielski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Set correct value of X-Forwarded-For header in PreAuthorize request
parent
0fe5a09e
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
11 deletions
+70
-11
internal/api/api.go
internal/api/api.go
+2
-0
internal/helper/helpers.go
internal/helper/helpers.go
+18
-0
internal/helper/helpers_test.go
internal/helper/helpers_test.go
+49
-0
internal/terminal/terminal.go
internal/terminal/terminal.go
+1
-11
No files found.
internal/api/api.go
View file @
896251b1
...
...
@@ -157,6 +157,8 @@ func (api *API) newRequest(r *http.Request, body io.Reader, suffix string) (*htt
// configurations (Passenger) to solve auth request routing problems.
authReq
.
Header
.
Set
(
"Gitlab-Workhorse"
,
api
.
Version
)
helper
.
SetForwardedFor
(
&
authReq
.
Header
,
r
)
tokenString
,
err
:=
secret
.
JWTTokenString
(
secret
.
DefaultClaims
)
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"newRequest: sign JWT: %v"
,
err
)
...
...
internal/helper/helpers.go
View file @
896251b1
...
...
@@ -3,10 +3,12 @@ package helper
import
(
"errors"
"log"
"net"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"syscall"
)
...
...
@@ -142,3 +144,19 @@ func DisableResponseBuffering(w http.ResponseWriter) {
func
AllowResponseBuffering
(
w
http
.
ResponseWriter
)
{
w
.
Header
()
.
Del
(
NginxResponseBufferHeader
)
}
func
SetForwardedFor
(
newHeaders
*
http
.
Header
,
originalRequest
*
http
.
Request
)
{
if
clientIP
,
_
,
err
:=
net
.
SplitHostPort
(
originalRequest
.
RemoteAddr
);
err
==
nil
{
var
header
string
// If we aren't the first proxy retain prior
// X-Forwarded-For information as a comma+space
// separated list and fold multiple headers into one.
if
prior
,
ok
:=
originalRequest
.
Header
[
"X-Forwarded-For"
];
ok
{
header
=
strings
.
Join
(
prior
,
", "
)
+
", "
+
clientIP
}
else
{
header
=
clientIP
}
newHeaders
.
Set
(
"X-Forwarded-For"
,
header
)
}
}
internal/helper/helpers_test.go
0 → 100644
View file @
896251b1
package
helper
import
(
"net/http"
"testing"
)
func
TestSetForwardedForGeneratesHeader
(
t
*
testing
.
T
)
{
testCases
:=
[]
struct
{
remoteAddr
string
previousForwardedFor
[]
string
expected
string
}{
{
"8.8.8.8:3000"
,
nil
,
"8.8.8.8"
,
},
{
"8.8.8.8:3000"
,
[]
string
{
"138.124.33.63, 151.146.211.237"
},
"138.124.33.63, 151.146.211.237, 8.8.8.8"
,
},
{
"8.8.8.8:3000"
,
[]
string
{
"8.154.76.107"
,
"115.206.118.179"
},
"8.154.76.107, 115.206.118.179, 8.8.8.8"
,
},
}
for
_
,
tc
:=
range
testCases
{
headers
:=
http
.
Header
{}
originalRequest
:=
http
.
Request
{
RemoteAddr
:
tc
.
remoteAddr
,
}
if
tc
.
previousForwardedFor
!=
nil
{
originalRequest
.
Header
=
http
.
Header
{
"X-Forwarded-For"
:
tc
.
previousForwardedFor
,
}
}
SetForwardedFor
(
&
headers
,
&
originalRequest
)
result
:=
headers
.
Get
(
"X-Forwarded-For"
)
if
result
!=
tc
.
expected
{
t
.
Fatalf
(
"Expected %v, got %v"
,
tc
.
expected
,
result
)
}
}
}
internal/terminal/terminal.go
View file @
896251b1
...
...
@@ -2,9 +2,7 @@ package terminal
import
(
"log"
"net"
"net/http"
"strings"
"time"
"github.com/gorilla/websocket"
...
...
@@ -102,15 +100,7 @@ func pingLoop(conn Connection) {
func
connectToServer
(
terminal
*
api
.
TerminalSettings
,
r
*
http
.
Request
)
(
Connection
,
error
)
{
terminal
=
terminal
.
Clone
()
// Pass along X-Forwarded-For, appending request.RemoteAddr, to the server
// we're connecting to.
if
ip
,
_
,
err
:=
net
.
SplitHostPort
(
r
.
RemoteAddr
);
err
==
nil
{
if
chains
,
ok
:=
r
.
Header
[
"X-Forwarded-For"
];
ok
{
terminal
.
Header
.
Set
(
"X-Forwarded-For"
,
strings
.
Join
(
chains
,
", "
)
+
", "
+
ip
)
}
else
{
terminal
.
Header
.
Set
(
"X-Forwarded-For"
,
ip
)
}
}
helper
.
SetForwardedFor
(
&
terminal
.
Header
,
r
)
conn
,
_
,
err
:=
terminal
.
Dial
()
if
err
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment