Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

Commit

Permalink
Relaxed CORS restriction from /api/ endpoint, see #46
Browse files Browse the repository at this point in the history
Now cross-site xhr-requests can work inside browser(by scripts or extensions) even if the authentication is enabled.
Tested on an extension which uses /api/ endpoint with authentication feature and solves [https://github.com/ketankr9/cloud-torrent-extension/issues/1#issuecomment-570915569](https://github.com/ketankr9/cloud-torrent-extension/issues/1#issuecomment-570915569)
Note: CORS only affects webHandle
  • Loading branch information
ketankr9 committed Jan 10, 2020
1 parent 6621e7c commit a55227d
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions server/server_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ func (s *Server) webHandle(w http.ResponseWriter, r *http.Request) {
}
//api call
if strings.HasPrefix(r.URL.Path, "/api/") {
w.Header().Set("Access-Control-Allow-Headers", "authorization")
origin := r.Header.Get("Origin")
if origin == "" {
origin = "*"
}
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Credentials", "true")
s.restAPIhandle(w, r)
return
}
Expand All @@ -51,7 +56,6 @@ func (s *Server) webHandle(w http.ResponseWriter, r *http.Request) {
func (s *Server) restAPIhandle(w http.ResponseWriter, r *http.Request) {
ret := "Bad Request"
if strings.HasPrefix(r.URL.Path, "/api/") {
w.Header().Set("Access-Control-Allow-Origin", "*")
switch r.Method {
case "POST":
if err := s.apiPOST(r); err == nil {
Expand Down

0 comments on commit a55227d

Please sign in to comment.