Skip to content

Commit

Permalink
Add [SecureContext] to BatteryManager and the Navigator partial inter…
Browse files Browse the repository at this point in the history
…face.

In other words, stop exposing this API to insecure origins. Even though this
API is not new, it provides user information that, transmitted insecurely,
can pose a risk to user privacy. See
https://w3ctag.github.io/design-principles/#secure-context for more
information on the guidelines we are trying to follow.

This has been discussed since at least 2016 (see #5). #11 made access from
an insecure origin throw a SecurityError at a time when the
`[SecureContext]` Web IDL extended attribute was not widespread.
Unfortunately, the spec change was not accompanied by a change in the
implementation(s) and, to this day, Blink's implementation (the only
remaining one) continues to expose the API to insecure origins.

Years later, #30 was added in the context of the discussions in #15 where it
was noted that the spec was still manually throwing a SecurityError when
checking if it was called by a secure origin. Besides stopping throwing a
SecurityError (which was never implemented), #30 also recognized the current
situation by noting that the API _should_ use `[SecureContext]` but did not
because it only made sense to do so when the implementation was updated
accordingly.

This change finally adds `[SecureContext]` to the spec's Web IDL because I
am also handling the Blink implementation [1][2]: starting with Chrome 99,
users will be warned that using the Battery Status API in an insecure origin
is deprecated, and starting with Chrome 103 doing so will no longer be
possible.

[1] https://chromestatus.com/feature/4878376799043584
[2] https://groups.google.com/a/chromium.org/g/blink-dev/c/w80tJL8uEV8/m/PfrHlvtgAgAJ

From a testing perspective, there isn't much to be done:
- web-platform-tests/wpt#5871 changed the existing tests to run in HTTPS.
- web-platform-tests/wpt#32556 removed the test for the SecurityError
  exception that never passed.

Fixes #15
  • Loading branch information
rakuco committed Feb 1, 2022
1 parent b451e98 commit 081b09d
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,11 @@ <h2>
Extensions to the `Navigator` interface
</h2>
<pre class="idl">
[SecureContext]
partial interface Navigator {
Promise&lt;BatteryManager&gt; getBattery();
};
</pre>
<p class="warning">
This method is not currently restricted to a <a>secure context</a>, but
it should be. Track progress on that in <a href=
"https://github.com/w3c/battery/issues/15">issue #15</a>.
</p>
<section>
<h3>
Internal slots
Expand Down Expand Up @@ -236,7 +232,7 @@ <h2>
The `BatteryManager` interface
</h2>
<pre class="idl">
[Exposed=Window]
[SecureContext, Exposed=Window]
interface BatteryManager : EventTarget {
readonly attribute boolean charging;
readonly attribute unrestricted double chargingTime;
Expand Down

0 comments on commit 081b09d

Please sign in to comment.