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 (#47)
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

Co-authored-by: Preston <[email protected]>
  • Loading branch information
Utsav Krishnan and boypt authored Jul 21, 2021
1 parent 1cc6068 commit 732e936
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 @@ -52,7 +52,12 @@ func (s *Server) webHandle(w http.ResponseWriter, r *http.Request) {
case "search":
s.scraperh.ServeHTTP(w, r)
case "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)
case "download":
s.dlfilesh.ServeHTTP(w, r)
Expand All @@ -67,7 +72,6 @@ func (s *Server) webHandle(w http.ResponseWriter, r *http.Request) {

// restAPIhandle is used both by main webserver and restapi server
func (s *Server) restAPIhandle(w http.ResponseWriter, r *http.Request) {
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 732e936

Please sign in to comment.