Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($sniffer): don't use history.pushState for sandboxed Chrome apps #15021

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/ng/sniffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,13 @@ function $SnifferProvider() {
this.$get = ['$window', '$document', function($window, $document) {
var eventSupport = {},
// Chrome Packaged Apps are not allowed to access `history.pushState`. They can be detected by
// the presence of `chrome.app.runtime` (see https://developer.chrome.com/apps/api_index)
isChromePackagedApp = $window.chrome && $window.chrome.app && $window.chrome.app.runtime,
// the presence of `chrome.app.runtime` (see https://developer.chrome.com/apps/api_index).
// For sandboxed apps, check for an extension runtime ID, but no access to other Chrome
// runtime APIs. See https://developer.chrome.com/apps/manifest/sandbox
isChromePackagedApp =
$window.chrome &&
($window.chrome.app && $window.chrome.app.runtime ||
!$window.chrome.app && $window.chrome.runtime && $window.chrome.runtime.id),
Copy link
Member

Choose a reason for hiding this comment

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

I don't think this check can distinguish sandboxed apps from extensions. We need a check that can reliably detect sandboxed apps only.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be able to. Extensions should always have a $window.chrome.app object. It may not have much in the object (there's no runtime child like with apps), but it will be there nonetheless, whereas with sandbox apps the object does not exist at all, because the APIs are not provided.

I'll try and investigate a better way of detecting this, but this seems to be the only way I can find that does not use APIs that would require an extension to have non-generic permissions for that API.

Copy link
Member

@gkalpak gkalpak Aug 17, 2016

Choose a reason for hiding this comment

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

Ah, I didn't pay much attention to the !$window.chrome.app check. It should be good then.
I wish there were a more straight-forward way to detect chrome packaged apps 😞

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the delay. I've waited for advice, but it appears there really is no other way of checking at the moment.

hasHistoryPushState = !isChromePackagedApp && $window.history && $window.history.pushState,
android =
toInt((/android (\d+)/.exec(lowercase(($window.navigator || {}).userAgent)) || [])[1]),
Expand Down
20 changes: 20 additions & 0 deletions test/ng/snifferSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ describe('$sniffer', function() {

expect(pushStateAccessCount).toBe(0);
});

it('should not try to access `history.pushState` in sandbox Chrome Packaged Apps', function() {
var pushStateAccessCount = 0;

var mockHistory = Object.create(Object.prototype, {
pushState: {get: function() { pushStateAccessCount++; return noop; }}
});
var mockWindow = {
chrome: {
runtime: {
id: 'x'
}
},
history: mockHistory
};

sniffer(mockWindow);

expect(pushStateAccessCount).toBe(0);
});
});


Expand Down