Documentation no example amp-cors-requests.md
What's the issue?
https://github.com/ampproject/amphtml/blob/master/spec/amp-cors-requests.md
In reading this documentation there isn't a full example and it is not fully clear what headers need to be implemented to ensure CORS compatibility consistently.
A full example of headers used would make this much more comprehensible.
for example looking in ampbyexample amp-list example looks like these are set:
access-control-allow-credentials: true access-control-allow-headers: Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token access-control-allow-methods: POST, GET, OPTIONS access-control-allow-origin: https://ampbyexample.com access-control-expose-headers: AMP-Access-Control-Allow-Source-Origin amp-access-control-allow-source-origin:
Why is the last one empty and what do the other things mean and which are generally required and which are just used in this context?
The docs (https://www.ampproject.org/docs/guides/amp-cors-requests) talk about:
The CORS Origin header
The AMP-Same-Origin custom header
Source origin restrictions via __amp_source_origin
and suggests
AMP-Same-Origin: true
And then talks about
If the Origin header is set:
If the origin does not match one of the following values, stop and return an error response:
*.ampproject.org
*.amp.cloudflare.com
the publisher's origin (aka yours)
where * represents a wildcard match, and not an actual asterisk ( * ).
If the value of the __amp_source_origin query parameter is not the publisher's origin, stop and return an error response.
If the two checks above pass, process the request.
If the Origin header is NOT set:
Verify that the request contains the AMP-Same-Origin: true header. If the request does not contain this header, stop and return an error response.
Otherwise, process the request.
But in the example code none of these seem to be set.
And finally it says:
Ensuring secure responses The resulting HTTP response to a CORS request must contain the following headers:
Access-Control-Allow-Origin: origin https://ampbyexample.com
This header is a W3 CORS Spec requirement, where origin refers to the requesting origin that was allowed via the CORS Origin request header (for example, "https://cdn.ampproject.org"). Although the W3 CORS spec allows the value of * to be returned in the response, for improved security, you should validate and echo the value of the "Origin" header.
AMP-Access-Control-Allow-Source-Origin: source-origin seems to be empty???? how is source-origin different from origin?
This header allows the specified source-origin to read the authorization response. The source-origin is the value specified and verified in the "__amp_source_origin" URL parameter (for example, "https://publisher1.com").
Access-Control-Expose-Headers: AMP-Access-Control-Allow-Source-Origin access-control-expose-headers: AMP-Access-Control-Allow-Source-Origin
that one seems to be the same.
This header simply allows the CORS response to contain the AMP-Access-Control-Allow-Source-Origin header.
Overall the documentation left me wondering what was actually required.
What adds to the confusion is when I follow the above I get
url.js:345 Response must contain the AMP-Access-Control-Allow-Source-Origin header
But when I include the same host as the host that is calling the page all is fine.
To my view there should be a number of examples to demosntrate the various different combinations of options that are possible for example what are the headers needed for:
- page at example.com loads a JSON endpoint from example.com
- page at example.com loads a JSON endpoint from othersite.com
- page at google cache/cloudflare cache loads a JSON endpoint from example.com
- page at google cache/cloudflare cache loads a JSON endpoint from othersite.com
I think those are all the potential examples that one might have(?)
In the code for ampbyexample:
https://github.com/ampproject/amp-by-example/blob/master/backend/request.go#L42
func EnableCors(w http.ResponseWriter, r *http.Request) {
origin := GetOrigin(r)
sourceOrigin := GetSourceOrigin(r)
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token")
w.Header().Set("Access-Control-Expose-Headers", "AMP-Access-Control-Allow-Source-Origin")
w.Header().Set("AMP-Access-Control-Allow-Source-Origin", sourceOrigin)
w.Header().Set("Access-Control-Allow-Credentials", "true")
}
func GetOrigin(r *http.Request) string {
origin := r.Header.Get("Origin")
if origin != "" {
return origin
}
return GetHost(r)
}
func GetSourceOrigin(r *http.Request) string {
// TODO perform checks if source origin is allowed
return r.URL.Query().Get("__amp_source_origin")
}
func GetHost(r *http.Request) string {
if r.TLS == nil {
return "http://" + r.Host
}
return "https://" + r.Host
}
func SetContentTypeJson(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
}
There is some context which seems to clarify this.
I'm still not 100% certain how the Origin is defined here.
I agree with QES. People I've hired to help can't even figure this out. Please update your documentation with CORS endpoint examples and samples that non-programmers can follow and learn from.