-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add X-Requested-With header to prevent CSRF #48
Changes from 9 commits
0e8174b
2237169
d257b1f
6a6c467
d655ca3
6cc4032
519cfac
daf8369
b2a3daf
c484eb8
1957e1d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,11 @@ import ( | |
"github.com/mattermost/focalboard/server/utils" | ||
) | ||
|
||
const ( | ||
HEADER_REQUESTED_WITH = "X-Requested-With" | ||
HEADER_REQUESTED_WITH_XML = "XMLHttpRequest" | ||
) | ||
|
||
// ---------------------------------------------------------------------------------------------------- | ||
// REST APIs | ||
|
||
|
@@ -35,35 +40,65 @@ func (a *API) app() *app.App { | |
} | ||
|
||
func (a *API) RegisterRoutes(r *mux.Router) { | ||
r.HandleFunc("/api/v1/blocks", a.sessionRequired(a.handleGetBlocks)).Methods("GET") | ||
r.HandleFunc("/api/v1/blocks", a.sessionRequired(a.handlePostBlocks)).Methods("POST") | ||
r.HandleFunc("/api/v1/blocks/{blockID}", a.sessionRequired(a.handleDeleteBlock)).Methods("DELETE") | ||
r.HandleFunc("/api/v1/blocks/{blockID}/subtree", a.attachSession(a.handleGetSubTree, false)).Methods("GET") | ||
apiv1 := r.PathPrefix("/api/v1").Subrouter() | ||
apiv1.Use(a.requireCSRFToken) | ||
|
||
apiv1.HandleFunc("/blocks", a.sessionRequired(a.handleGetBlocks)).Methods("GET") | ||
apiv1.HandleFunc("/blocks", a.sessionRequired(a.handlePostBlocks)).Methods("POST") | ||
apiv1.HandleFunc("/blocks/{blockID}", a.sessionRequired(a.handleDeleteBlock)).Methods("DELETE") | ||
apiv1.HandleFunc("/blocks/{blockID}/subtree", a.attachSession(a.handleGetSubTree, false)).Methods("GET") | ||
|
||
apiv1.HandleFunc("/users/me", a.sessionRequired(a.handleGetMe)).Methods("GET") | ||
apiv1.HandleFunc("/users/{userID}", a.sessionRequired(a.handleGetUser)).Methods("GET") | ||
apiv1.HandleFunc("/users/{userID}/changepassword", a.sessionRequired(a.handleChangePassword)).Methods("POST") | ||
|
||
r.HandleFunc("/api/v1/users/me", a.sessionRequired(a.handleGetMe)).Methods("GET") | ||
r.HandleFunc("/api/v1/users/{userID}", a.sessionRequired(a.handleGetUser)).Methods("GET") | ||
r.HandleFunc("/api/v1/users/{userID}/changepassword", a.sessionRequired(a.handleChangePassword)).Methods("POST") | ||
apiv1.HandleFunc("/#", a.handleLogin).Methods("POST") | ||
apiv1.HandleFunc("/register", a.handleRegister).Methods("POST") | ||
|
||
r.HandleFunc("/api/v1/#", a.handleLogin).Methods("POST") | ||
r.HandleFunc("/api/v1/register", a.handleRegister).Methods("POST") | ||
apiv1.HandleFunc("/blocks/export", a.sessionRequired(a.handleExport)).Methods("GET") | ||
apiv1.HandleFunc("/blocks/import", a.sessionRequired(a.handleImport)).Methods("POST") | ||
|
||
r.HandleFunc("/api/v1/files", a.sessionRequired(a.handleUploadFile)).Methods("POST") | ||
r.HandleFunc("/files/{filename}", a.sessionRequired(a.handleServeFile)).Methods("GET") | ||
apiv1.HandleFunc("/sharing/{rootID}", a.sessionRequired(a.handlePostSharing)).Methods("POST") | ||
apiv1.HandleFunc("/sharing/{rootID}", a.sessionRequired(a.handleGetSharing)).Methods("GET") | ||
|
||
r.HandleFunc("/api/v1/blocks/export", a.sessionRequired(a.handleExport)).Methods("GET") | ||
r.HandleFunc("/api/v1/blocks/import", a.sessionRequired(a.handleImport)).Methods("POST") | ||
apiv1.HandleFunc("/workspace", a.sessionRequired(a.handleGetWorkspace)).Methods("GET") | ||
apiv1.HandleFunc("/workspace/regenerate_#_token", a.sessionRequired(a.handlePostWorkspaceRegenerate#Token)).Methods("POST") | ||
|
||
r.HandleFunc("/api/v1/sharing/{rootID}", a.sessionRequired(a.handlePostSharing)).Methods("POST") | ||
r.HandleFunc("/api/v1/sharing/{rootID}", a.sessionRequired(a.handleGetSharing)).Methods("GET") | ||
// Files API | ||
|
||
r.HandleFunc("/api/v1/workspace", a.sessionRequired(a.handleGetWorkspace)).Methods("GET") | ||
r.HandleFunc("/api/v1/workspace/regenerate_#_token", a.sessionRequired(a.handlePostWorkspaceRegenerate#Token)).Methods("POST") | ||
files := r.PathPrefix("/files/").Subrouter() | ||
files.Use(a.requireCSRFToken) | ||
|
||
files.HandleFunc("/", a.sessionRequired(a.handleUploadFile)).Methods("POST") | ||
files.HandleFunc("/{filename}", a.sessionRequired(a.handleServeFile)).Methods("GET") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not 100% sure about the need of the CSRFToken check for the static files, actually I think it should be only for the API. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't have much control over the image loading and files downloading in the browser to add that header. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, I had refactored contentBlock to get the images with fetch, passing the session token and CSRF header. However, I removed the CSRF check for GET /files for now, as that (in theory) shouldn't be a CSRF vulnerability as no state is changed. We can think about this one more, and change later if needed. |
||
} | ||
|
||
func (a *API) RegisterAdminRoutes(r *mux.Router) { | ||
r.HandleFunc("/api/v1/admin/users/{username}/password", a.adminRequired(a.handleAdminSetPassword)).Methods("POST") | ||
} | ||
|
||
func (a *API) requireCSRFToken(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if !a.checkCSRFToken(r) { | ||
log.Println("checkCSRFToken FAILED") | ||
errorResponse(w, http.StatusBadRequest, nil, nil) | ||
return | ||
} | ||
|
||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func (a *API) checkCSRFToken(r *http.Request) bool { | ||
token := r.Header.Get(HEADER_REQUESTED_WITH) | ||
|
||
if token == HEADER_REQUESTED_WITH_XML { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func (a *API) handleGetBlocks(w http.ResponseWriter, r *http.Request) { | ||
query := r.URL.Query() | ||
parentID := query.Get("parent_id") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice touch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. I smoke tested this and it looks ok, but please LMK if you see anything off.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, everything is looking great now