Bug 199752 - window.location.hash is not URL encoded when set from Javascript
Summary: window.location.hash is not URL encoded when set from Javascript
Status: RESOLVED CONFIGURATION CHANGED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore JavaScript (show other bugs)
Version: Safari 12
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2019-07-12 09:05 PDT by Robert Flack
Modified: 2022-05-31 09:58 PDT (History)
5 users (show)

See Also:


Attachments
Demo of inconsistent hash readback. (387 bytes, text/html)
2019-07-12 09:05 PDT, Robert Flack
no flags Details
Safari 15.5 matches other browsers (400.66 KB, image/png)
2022-05-30 09:54 PDT, Ahmad Saleem
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Robert Flack 2019-07-12 09:05:34 PDT
Created attachment 374014 [details]
Demo of inconsistent hash readback.

When Javascript sets the location hash, it reads back as the exact string which Javascript set. This seems to remain even if the page is reloaded.

However, if the user copies the URL to another tab, the string is URL encoded resulting in a different location.hash read from Javascript. This results in a subtle bug if the developer does not explicitly encode / decode the location hash.

See https://output.jsbin.com/jikefe for live demo or the following:

<!DOCTYPE html>
<body>
  <div id="log"></div>
</body>
<script>
document.addEventListener('DOMContentLoaded', function() {
  if (window.location.hash == '')
    window.location.hash = 'test encoding';
  else
    hashchange();
});

function hashchange() {
  document.getElementById('log').textContent = window.location.hash;
}
window.addEventListener('hashchange', hashchange);
</script>

When the demo site sets the location hash it is read back with a space in Safari 12 and Edge 18, in Chrome and Firefox the value read back is url encoded which it will be if the URL is copied to another tab.
Comment 1 Robert Flack 2019-07-12 10:47:15 PDT
FYI I've created a web-platform-tests PR here:
https://github.com/web-platform-tests/wpt/pull/17807
Comment 2 Radar WebKit Bug Importer 2019-07-12 17:00:04 PDT
<rdar://problem/53034220>
Comment 3 Ahmad Saleem 2022-05-30 09:54:46 PDT
Created attachment 459866 [details]
Safari 15.5 matches other browsers

Based on testing with Safari 15.5, now due to Spec change or browser changes on handling URL encoding, all browsers now behave same based on the test case. Hence, I think this should be marked as "Resolved Fixed". Thanks!
Comment 4 Alexey Proskuryakov 2022-05-31 09:58:26 PDT
Thank you for checking! We have this WPT test imported now, and it passes.