Skip to content

CORS for isAjax requests #822

@POD666

Description

@POD666

I have a SPA that is running separately from API webserver:
SPA -> oauth2-proxy -> API webserver

When OAUTH2_PROXY_COOKIE_REFRESH cookie expires, SPA gets 401 in response but without CORS headers and so blocked by a browser.

Expected Behavior

401 Response can be handled

Current Behavior

401 Response is blocked by a browser because of CORS:

Access to XMLHttpRequest at 'http://localhost:4180/api/v1/features' from origin 'http://localhost:7777' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Where http://localhost:7777 is my SPA.

Possible Solution

Following changes here work for me:

// ErrorJSON returns the error code with an application/json mime type
func (p *OAuthProxy) ErrorJSON(req *http.Request, rw http.ResponseWriter, code int) {
	rw.Header().Set("Content-Type", applicationJSON)
	origin := req.Header.Get("Origin")
	if origin != "" {
		rw.Header().Set("Access-Control-Allow-Origin", origin)
		rw.Header().Set("Access-Control-Allow-Credentials", "true")
	}
	rw.WriteHeader(code)
}

Is it good enough to prepare PR? Is there a better solution/workaround?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions