Skip to content

Commit 55475c0

Browse files
authored
Merge pull request #45 from factorysh/features/badge-referer
Badge checking for referer
2 parents 712ea8c + 28cf343 commit 55475c0

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

application/application.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type Application struct {
3030
Services map[string]service.Service
3131
serviceFolder string
3232
Domain string
33-
GitlabDomain string
33+
GitlabURL string
3434
Router http.Handler
3535
storage storage.Storage
3636
volumes *volumes.Volumes
@@ -103,7 +103,7 @@ func New(cfg *conf.Conf) (*Application, error) {
103103
Services: svcs,
104104
Domain: cfg.OAuth.AppURL,
105105
// FIXME: use dedicated variable
106-
GitlabDomain: cfg.OAuth.ProviderURL,
106+
GitlabURL: cfg.OAuth.ProviderURL,
107107
serviceFolder: cfg.Services,
108108
storage: s,
109109
Router: MagicPathHandler(r),
@@ -131,22 +131,33 @@ func New(cfg *conf.Conf) (*Application, error) {
131131
r.Get("/services", a.ServicesHandler)
132132
r.Get("/service/{serviceID}", a.ReadmeHandler)
133133
r.Route("/service/{serviceID}/{project}", func(r chi.Router) {
134-
r.Use(authMiddleware.Middleware())
135134
r.Use(a.ServiceMiddleware)
136135
r.Route("/", func(r chi.Router) {
137136
r.Route("/{branch}", func(r chi.Router) {
138137
r.Route("/{commit}", func(r chi.Router) {
139-
r.Post("/", a.PostTaskHandler)
140-
r.Get("/", a.TaskHandler(false))
141-
r.Get("/status", badge.StatusBadge(a.storage, false))
142-
r.Get("/badge/{badge}", a.BadgeMyTaskHandler(false))
143-
r.Get("/volumes/*", a.VolumesHandler(6, false))
138+
r.Group(func(r chi.Router) {
139+
r.Use(authMiddleware.Middleware())
140+
r.Post("/", a.PostTaskHandler)
141+
r.Get("/", a.TaskHandler(false))
142+
r.Get("/volumes/*", a.VolumesHandler(6, false))
143+
})
144+
r.Group(func(r chi.Router) {
145+
r.Use(a.RefererMiddleware)
146+
r.Get("/status", badge.StatusBadge(a.storage, false))
147+
r.Get("/badge/{badge}", a.BadgeMyTaskHandler(false))
148+
})
144149
})
145150
r.Route("/latest", func(r chi.Router) {
146-
r.Get("/", a.TaskHandler(true))
147-
r.Get("/status", badge.StatusBadge(a.storage, true))
148-
r.Get("/badge/{badge}", a.BadgeMyTaskHandler(true))
149-
r.Get("/volumes/*", a.VolumesHandler(6, true))
151+
r.Group(func(r chi.Router) {
152+
r.Use(authMiddleware.Middleware())
153+
r.Get("/", a.TaskHandler(true))
154+
r.Get("/volumes/*", a.VolumesHandler(6, true))
155+
})
156+
r.Group(func(r chi.Router) {
157+
r.Use(a.RefererMiddleware)
158+
r.Get("/status", badge.StatusBadge(a.storage, true))
159+
r.Get("/badge/{badge}", a.BadgeMyTaskHandler(true))
160+
})
150161
})
151162
})
152163
})

application/referer.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package application
2+
3+
import "net/http"
4+
5+
// RefererMiddleware ensure that requests comes from the gitlab domain
6+
func (a *Application) RefererMiddleware(next http.Handler) http.Handler {
7+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8+
referer := r.Referer()
9+
if referer != a.GitlabURL {
10+
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
11+
return
12+
}
13+
14+
next.ServeHTTP(w, r)
15+
})
16+
}

application/volume.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func (a *Application) VolumesHandler(basePathLen int, latest bool) http.HandlerF
6565
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
6666
l.Warn("Path not found", zap.Error(err))
6767

68-
data, err := NewResultFromTask(t, "No result for this task", a.GitlabDomain)
68+
data, err := NewResultFromTask(t, "No result for this task", a.GitlabURL)
6969
if err != nil {
7070
l.Error("when creating result from a task", zap.Error(err))
7171
w.WriteHeader(http.StatusInternalServerError)
@@ -128,7 +128,7 @@ func (a *Application) renderResultPageForTask(t *task.Task, filePath string, w h
128128
return err
129129
}
130130

131-
data, err := NewResultFromTask(t, template.HTML(content), a.GitlabDomain)
131+
data, err := NewResultFromTask(t, template.HTML(content), a.GitlabURL)
132132
// create the page
133133
p := html.Page{
134134
Domain: a.Domain,

0 commit comments

Comments
 (0)