Skip to content

Commit 46886c5

Browse files
committed
feat(routing): Extra validation on route addition
Routes that are not public are now verified to be within the same cookiescope
1 parent a77ad5a commit 46886c5

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

cmd/tobab/tobab.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (app *Tobab) setTobabRoutes(r *mux.Router) {
5050
return
5151
}
5252

53-
if ok, err := h.Validate(); !ok {
53+
if ok, err := h.Validate(app.config.CookieScope); !ok {
5454
http.Error(w, fmt.Sprintf("invalid backend: %e", err), http.StatusBadRequest)
5555
return
5656
}
@@ -156,7 +156,7 @@ func (app *Tobab) GetHosts(in *clirpc.Empty, out *clirpc.GetHostsOut) error {
156156
}
157157

158158
func (app *Tobab) AddHost(in *clirpc.AddHostIn, out *clirpc.Empty) error {
159-
ok, err := in.Host.Validate()
159+
ok, err := in.Host.Validate(app.config.CookieScope)
160160
if !ok {
161161
return err
162162
}

types.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Globs: %s
4747
`, aurora.Magenta(aurora.Bold(h.Hostname)), h.Backend, h.Type, h.Public, h.Globs)
4848
}
4949

50-
func (h *Host) Validate() (bool, error) {
50+
func (h *Host) Validate(cookiescope string) (bool, error) {
5151
ok, err := govalidator.ValidateStruct(h)
5252
if !ok {
5353
return ok, err
@@ -62,6 +62,9 @@ func (h *Host) Validate() (bool, error) {
6262
if !strings.HasPrefix(u.Scheme, "http") {
6363
return false, fmt.Errorf("%s has invalid or missing scheme", h.Backend)
6464
}
65+
if !strings.HasSuffix(h.Hostname, cookiescope) && !h.Public {
66+
return false, fmt.Errorf("'%s' won't be accessible because the cookiescope ('%s') does not match this domain", h.Hostname, cookiescope)
67+
}
6568
if !h.Public && len(h.Globs) == 0 {
6669
return false, fmt.Errorf("%s will not be accessible by anybody", h.Hostname)
6770
}
@@ -93,7 +96,16 @@ func (h Host) HasAccess(user string) bool {
9396
}
9497

9598
func (c *Config) Validate() (bool, error) {
96-
return govalidator.ValidateStruct(c)
99+
ok, err := govalidator.ValidateStruct(c)
100+
if !ok {
101+
return ok, err
102+
}
103+
104+
if !strings.HasSuffix(c.Hostname, c.CookieScope) {
105+
return false, fmt.Errorf("Hostname: '%s' should be in the same domain as the cookiescope: '%s'", c.Hostname, c.CookieScope)
106+
}
107+
108+
return ok, err
97109
}
98110

99111
func LoadConf(path string) (Config, error) {

types_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tobab
33
import "testing"
44

55
func TestHost_Validate(t *testing.T) {
6+
cookiescope := "example.com"
67
type fields struct {
78
Hostname string
89
Backend string
@@ -93,6 +94,18 @@ func TestHost_Validate(t *testing.T) {
9394
want: true,
9495
wantErr: false,
9596
},
97+
{
98+
name: "unreachable because of domain",
99+
fields: fields{
100+
Hostname: "test.example.co.uk",
101+
Backend: "http://localhost:1234",
102+
Type: "http",
103+
Public: false,
104+
Globs: []Glob{"*"},
105+
},
106+
want: false,
107+
wantErr: true,
108+
},
96109
}
97110
for _, tt := range tests {
98111
t.Run(tt.name, func(t *testing.T) {
@@ -103,7 +116,7 @@ func TestHost_Validate(t *testing.T) {
103116
Public: tt.fields.Public,
104117
Globs: tt.fields.Globs,
105118
}
106-
got, err := h.Validate()
119+
got, err := h.Validate(cookiescope)
107120
if (err != nil) != tt.wantErr {
108121
t.Errorf("Host.Validate() error = %v, wantErr %v", err, tt.wantErr)
109122
return

0 commit comments

Comments
 (0)