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

Test for opaque redirect handling #10449

Closed
wants to merge 1 commit into from

Conversation

annevk
Copy link
Member

@annevk annevk commented Apr 12, 2018

As per whatwg/fetch#633 (comment) except using global state instead of the cache API. (Improvements welcome!)

@annevk
Copy link
Member Author

annevk commented Apr 12, 2018

@jakearchibald @wanderview I'd appreciate your help with this. This is the first service worker code/"test" I've written...

I've run it in Firefox and it doesn't do what I discussed in whatwg/fetch#633 as the problematic scenario.

It resolves the Location header when it gets the response the second time, leading it to a different URL than the server intended.

I don't think you can violate the same-origin policy with this and we only ever handle these type of responses for navigation requests, which will be same-origin with the service worker, but it still seems a little sketchy and might allow a clever XSS to exfiltrate redirect data?

@wanderview
Copy link
Member

@annevk You probably need to wait for the service worker to activate with waitForState(). You should also add cleanup handlers to unregister the service worker and remove the frame.

BTW, now that we can use async functions some of this can be cleaner to write.

@jakearchibald
Copy link
Contributor

+1 to async functions & wait_for_state. I do cleanup stuff as part of setup since we don't have async cleanup handlers yet (@wanderview or has that changed?).

https://github.com/w3c/web-platform-tests/pull/10348/files#diff-2098c79116c31c91424e3965506554a3R41 - here's the pattern I use.

@jakearchibald
Copy link
Contributor

jakearchibald commented Apr 13, 2018

I also tend to put files that aren't defining tests into a resources directory. That seems to be the convention elsewhere. Also, it means the tests sit outside the service worker scope. I don't know if it's a problem if that isn't the case, but it feels safer.

Copy link
Contributor

@jakearchibald jakearchibald left a comment

Choose a reason for hiding this comment

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

LGTM aside from the nits mentions, and wait_for_state.

e.respondWith(new Promise(async resolve => {
evilGlobalState = await fetch(e.request);
resolve(new Response("stage 1 completed\n"));
}));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the above 4 lines could be:

evilGlobalState = fetch(e.request);
e.respondWith(new Response("stage 1 completed\n"));

Copy link
Member Author

Choose a reason for hiding this comment

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

Wouldn't that increase the chance evilGlobalState is null when the second fetch comes in?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, but if that's your intent then use waitUntil

event.waitUntil(new Promise(r => self.globalStateCollected = r));

And call globalStateCollected() once evilGlobalState is no longer needed.

@wanderview
Copy link
Member

I do cleanup stuff as part of setup since we don't have async cleanup handlers yet (@wanderview or has that changed?).

That's true, but if you use a unique scope it doesn't matter in practice. (Async cleanup would be great, though.)

@annevk
Copy link
Member Author

annevk commented Apr 15, 2018

Should be (mostly) good now! Thanks again for the help. As I noted on IRC, Safari passes this. I'm not sure how to run this in Edge via BrowserStack yet.

@jakearchibald
Copy link
Contributor

I think the service worker should go in resources too right?

@jakearchibald
Copy link
Contributor

Other than the above, LGTM.

@annevk annevk force-pushed the annevk/opaque-redirect-handling branch from 8e87d5c to 4fcb0f3 Compare June 6, 2018 09:48
@annevk
Copy link
Member Author

annevk commented Jun 6, 2018

I fixed that issue. What remains is that we need to decide how we want to handle redirects more generally as per the linked Fetch issue and then also add tests for those other scenarios.

@gsnedders gsnedders closed this Jan 24, 2020
@gsnedders gsnedders deleted the annevk/opaque-redirect-handling branch January 24, 2020 18:01
@gsnedders gsnedders restored the annevk/opaque-redirect-handling branch January 24, 2020 18:42
@Hexcles Hexcles reopened this Jan 24, 2020
As per whatwg/fetch#633 (comment) except using global state instead of the cache API. (Improvements welcome!)
@annevk
Copy link
Member Author

annevk commented Apr 3, 2020

Hmm, should opaque-redirect.window.js have .https in the filename or is that implicit for the service-workers directory?

@annevk annevk deleted the annevk/opaque-redirect-handling branch February 5, 2021 13:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants