Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Post-based identity bug fix #21533

Merged
merged 9 commits into from
Apr 2, 2019

Conversation

hellokoji
Copy link
Contributor

Bug fix

Fix a bug with POST based identity request using batched-xhr. In Pull Request (#21107), I mistakenly removed the crossorigin attribute check in the batchFetchJsonFor in favor of checking for the identity token. See this discussion.

Expected behavior: Adding the crossorigin="amp-viewer-auth-token-via-post" attribute to amp-state, amp-list & amp-form should transform the request to POST and attach the viewer identity token.

Current behavior: Without an identity token, amp-state and amp-list will send a GET request regardless of the presence of the crossorigin attribute.

Proposed solution: When the crossorigin attribute is present on the remote xhr extension, the request should always be transformed into a POST request. Without an identity token, an empty token will be sent instead.

Additional Clean Up

  • Added tests for getViewerAuthTokenIfAvailable in the new xhr-utils tests.
  • Removed the window argument from getViewerAuthTokenIfAvailable as requested here.

cc @choumx

Change-Id: If62f3e30fc6610e454c388751a19775b1c920aba
Change-Id: I6f879df5d908045585fe5d30d744ddfcd9e48d86
Change-Id: Ib05f5620fbbcb565b466574b819476d74cb9ba8a
Change-Id: Ia8be818ae853973d8ff362bed1bb39a5d36d480c
@dreamofabear dreamofabear self-requested a review March 21, 2019 22:05
Copy link

@dreamofabear dreamofabear left a comment

Choose a reason for hiding this comment

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

Ah, sorry didn't realize the full implications of that comment. Thanks for the fix!

* @param {!Element} element
* @return {!Promise<string|undefined>}
*/
export function getViewerAuthTokenIfAvailable(win, element) {
export function getViewerAuthTokenIfAvailable(element) {

Choose a reason for hiding this comment

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

An alternative fix is to resolve with empty string instead of undefined, and then change the check in batchFetchJsonFor to compare against undefined instead of truthy. I.e.

return Services.viewerAssistanceForDocOrNull(element).then(va => {
  userAssert(va, 'crossorigin="amp-viewer-auth-token-post" requires amp-viewer-assistance extension.');
  return va.getIdTokenPromise();
}).then(token => token || '').catch(() => ''); // Return empty string if token is not available.

This preserves the current semantics by keeping the "crossorigin" attribute encapsulated in this helper function. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am okay with having the token return an empty string instead of undefined without a token, but it's important to still transform the request without a token based on the presence of the crossorigin attribute -- not the identity token.

If I'm understanding your proposal correctly, the token would only be undefined in cases where getViewerAuthTokenIfAvailable isn't called at all. However, getViewerAuthTokenIfAvailable will always be called in amp-state and amp-list before being passed to the batched-json call, so it will always transform the request into a POST because the opt_token !== undefined.

Choose a reason for hiding this comment

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

Here's a clearer snippet of what I'm suggesting:

if (crossOriginAttr 
    && crossOriginAttr.trim() === 'amp-viewer-auth-token-via-post') {
  return Services.viewerAssistanceForDocOrNull(element).then(va => {
    userAssert(va, 'crossorigin="amp-viewer-auth-token-post" '
        + 'requires amp-viewer-assistance extension.');
    return va.getIdTokenPromise();
  })
  // If crossorigin attribute is present, resolve with token or empty string.
  .then(token => token || '').catch(() => ''); 
}
// If crossorigin attribute is missing, always resolve with undefined.
return Promise.resolve(undefined);

Would that work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, that makes sense. This solution should work! Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just made the changes and added some tests. Thanks for the suggestion!

src/batched-json.js Outdated Show resolved Hide resolved
Change-Id: I9f9aab8d87e0cffe273072b3409c6cb40d2d1e4d
Change-Id: I0a3c5ba2e5297611898d0c9e08a24758f565c6f7
Change-Id: Id9d7246a0b70cc5e6b64cdfad9fbc6ad504a8f75
…-fix

Change-Id: I791d22fef6876cebfcc150a1298d9c2daea65f31
test/unit/test-batched-json.js Outdated Show resolved Hide resolved
test/unit/test-batched-json.js Outdated Show resolved Hide resolved
test/unit/utils/test-xhr-utils.js Show resolved Hide resolved
test/unit/utils/test-xhr-utils.js Outdated Show resolved Hide resolved
test/unit/utils/test-xhr-utils.js Show resolved Hide resolved
Change-Id: I3e39fe0dbd2d2dc74404d603c3460e750d07be19
@dreamofabear
Copy link

Looks great.

@dreamofabear dreamofabear merged commit ffe5655 into ampproject:master Apr 2, 2019
@hellokoji hellokoji deleted the POST-identity-bug-fix branch April 2, 2019 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants