Skip to content

Update CORS, add AMP-Same-Origin when Origin is missing on same origin requests#4879

Merged
mkhatib merged 8 commits intoampproject:masterfrom
mkhatib:update-cors-forms
Sep 13, 2016
Merged

Update CORS, add AMP-Same-Origin when Origin is missing on same origin requests#4879
mkhatib merged 8 commits intoampproject:masterfrom
mkhatib:update-cors-forms

Conversation

@mkhatib
Copy link
Copy Markdown
Contributor

@mkhatib mkhatib commented Sep 9, 2016

ITI: #3343


<h4>Subscribe to our weekly Newsletter (POST xhr same origin)</h4>
<form method="post"
action="/form/html/post"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FMI: Why both action and action-xhr?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always require action as a fallback in case amp-form extension failed to load for any reason the form would still be submittable.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Unfortunately we don't have a security story in that case, right? Maybe only allow fallback for same origin?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have document-submit that gets bundled part of amp runtime. It also handles the cases where amp-form might fail. For example, the POST + non-XHR case is prevented by document-submit. GET requests are ok to fallback as long as we clearly communicate they shouldn't be used for state changing requests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For POST this is very misleading. We should just remove action. We need to find a way to block un-upgraded submission in our submit handler so it doesn't post to itself (although this is probably already happening).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For POST this is very misleading. We should just remove action

I am assuming this is just for the examples? sgtm

We need to find a way to block un-upgraded submission in our submit handler so it doesn't post to itself

document-submit does assert action is provided, https and not on cdn for GET. We do stop un-upgraded POST submissions in this PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Examples and validator.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@cramforce
Copy link
Copy Markdown
Member

Please document the submission rules in the amp-form docs.

@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Sep 9, 2016

Done. PTAL 👀


**Important**: Your XHR endpoints need to follow and implement [CORS Requests in AMP spec](https://github.com/ampproject/amphtml/blob/master/spec/amp-cors-requests.md).

**Important**: See [Protecting against XSRF attacks](
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Broken link.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@molnarg
Copy link
Copy Markdown

molnarg commented Sep 9, 2016

It's worth mentioning that target=_blank won't work with XHR.

Your XHR endpoints need to follow and implement [CORS Requests in AMP spec](https://github.com/ampproject/amphtml/blob/master/spec/amp-cors-requests.md).

### Protecting against XSRF
In addition to following AMP CORS spec, please pay extra attention to [non-idempotent requests note]()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: dot at the end of the sentence.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, missing link.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think idempotence is the wrong concept here. Maybe just "mutating" or "state changing"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, "state changing" would be better and simpler.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Sep 9, 2016

Done. 👀 PTAL

message: '__amp_source_origin parameter is invalid.'
}));
return;
if (req.headers['amp-same-origin'] != 'true') {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good time to move this test in a separate function. E.g. checkAmpCors or such.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In light of the discussion below, we should reverse the checks and process Origin header first.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's discuss this in the other thread, I think there's no change needed here.

@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Sep 11, 2016

PTAL 👀

@molnarg
Copy link
Copy Markdown

molnarg commented Sep 13, 2016

LGTM

const init = {method: 'post', body: {}};
location.href = '/path';
xhr.fetchJson('/whatever', init);
expect(init['headers']['AMP-Same-Origin']).to.equal('true');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a test to ensure it uses actual origins and NOT source origins.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@dvoytenko
Copy link
Copy Markdown
Contributor

LGTM with one more test requested. Please add the test and merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants