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

Guard against Chrome moving Gamepad to HTTPS secure origin #20890

Merged
merged 3 commits into from
Dec 11, 2023

Conversation

juj
Copy link
Collaborator

@juj juj commented Dec 11, 2023

Chrome has (apparently?) moved to requiring a secure HTTPS origin in order to access gamepad data. Therefore constrain HTML5 Gamepad API functions to return EMSCRIPTEN_RESULT_NOT_SUPPORTED when getGamepads() does not work but throws an exception.

W3C Github entry that I found related w3c/gamepad#145 .

Caniuse info is partial and does not mention the requirement of a secure origin: https://caniuse.com/gamepad

Oddly MDN does not say that Chrome would require secure origin, but it instead documents that Firefox requires a secure origin. https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getGamepads

This change has a potential to break a lot of Emscripten compiled web pages, since these functions were never developed with the anticipation that navigator.getGamepads() could throw. (the spec stated no such wording in circa 2014 or so)

…order to access gamepad data. Therefore constrain HTML5 Gamepad API functions to return EMSCRIPTEN_RESULT_NOT_SUPPORTED when getGamepads() does not work but throws an exception.
src/library_html5.js Outdated Show resolved Hide resolved
@juj juj enabled auto-merge (squash) December 11, 2023 17:46
@saschanaz
Copy link
Collaborator

saschanaz commented Dec 11, 2023

This PR seems to expect that the function can throw, but the actual behavior is that the function returns with an empty array for insecure contexts. See https://wpt.fyi/results/gamepad/gamepad-secure-context.html?label=experimental&label=master&aligned and https://hacks.mozilla.org/2020/07/securing-gamepad-api/.

Edit: Although somehow Chrome is broken and returns an array filled with null. 🤔

@juj juj merged commit 9295b0a into emscripten-core:main Dec 11, 2023
2 checks passed
@juj
Copy link
Collaborator Author

juj commented Dec 11, 2023

Hey @saschanaz thanks for the pointer.

I am operating based on this report that we have received: https://forum.unity.com/threads/crash-on-facebook-instant-games-due-to-gamepads-permission.1524454/

There it looks clear that Chrome has thrown an exception from attempting to call navigator.getGamepads().

It would be really good to get a clarification to this from Google. I'll try to ping some people.

@saschanaz
Copy link
Collaborator

Thanks for the context! That seems irrelevant to the secure context but about the permission policy, e.g. when the code runs within an iframe without explicitly allowing gamepad permission. See: https://w3c.github.io/gamepad/#dom-navigator-getgamepads

  1. If doc is not allowed to use the "gamepad" permission, then throw a "SecurityError" DOMException and abort these steps.

It does sounds right to handle the error in that case. I was confused by the title of the PR 😁

@juj
Copy link
Collaborator Author

juj commented Dec 13, 2023

Ah, right. That is a good point, I confused HTTPS and iframe security policy in the exception message.

I get word from Chrome team that some time in the future navigator.getGamePads() might move behind HTTPS, so having this PR land helps pave for that way.

@ImBoop
Copy link

ImBoop commented Feb 9, 2024

Good news, looks like the major vendors are interested in rolling back the https requirement!

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