<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>199928</bug_id>
          
          <creation_ts>2019-07-18 20:16:35 -0700</creation_ts>
          <short_desc>sessionStorage/localStorage disconnected from persistent storage if accessed &quot;too early&quot;</short_desc>
          <delta_ts>2019-07-22 13:05:20 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebKit Misc.</component>
          <version>Safari 12</version>
          <rep_platform>All</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Florian Schoppmann">mail</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>achristensen</cc>
    
    <cc>beidson</cc>
    
    <cc>cdumez</cc>
    
    <cc>mail</cc>
    
    <cc>sihui_liu</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1554112</commentid>
    <comment_count>0</comment_count>
    <who name="Florian Schoppmann">mail</who>
    <bug_when>2019-07-18 20:16:35 -0700</bug_when>
    <thetext>Consider the following code that writes to sessionStorage/localStorage and then immediately sets a new location with an origin different from the current one:

sessionStorage.setItem(key, value);
window.location.href = url;

Now assume that the site at `url` immediately redirects back. Upon redirect, however, sessionStorage.getItem(key) === null.

https://fschopp.github.io/safari-local-storage-bug/ is a test case that reliably shows the bug both with Safari 12.1.1 as well as with Safari on iOS 12.3.1. The test case works as expected in Chrome and Firefox; that is, in the browsers sessionStorage.getItem(key) returns the value that was previously written.

Background: I stumbled across the problem when attempting to store local state during OAuth authentication (as opposed to, say, encoding local state in the OAuth redirect URL).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1554114</commentid>
    <comment_count>1</comment_count>
    <who name="Florian Schoppmann">mail</who>
    <bug_when>2019-07-18 20:25:39 -0700</bug_when>
    <thetext>I had a cursory look at various relevant places in the WebKit source code some weeks ago, but didn&apos;t find anything obvious. Just in case: Back then, I collected some findings and links in a GitHub issue. https://github.com/fschopp/project-planning-for-you-track/issues/1</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1554321</commentid>
    <comment_count>2</comment_count>
    <who name="Florian Schoppmann">mail</who>
    <bug_when>2019-07-19 14:30:49 -0700</bug_when>
    <thetext>It appears that the WebKit bug occurs if sessionStorage or localStorage are accessed “too early.” In that case, they appear disconnected (for both reads and writes) from the persistent storage, for the entire time the user stays on the current page.

A workaround is for the JavaScript code to wait before accessing the storage. I updated my test case at https://fschopp.github.io/safari-local-storage-bug/ so it demos both the bug (as before) and the workaround.

Interestingly, it appears Firefox used to have a similar problem.
https://stackoverflow.com/questions/13852209/localstorage-unreliable-in-firefox/13856156
(The workaround mentioned on StackOverflow -- accessing sessionStorage.length -- does not work for Safari, though.)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1554628</commentid>
    <comment_count>3</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2019-07-21 17:10:57 -0700</bug_when>
    <thetext>&lt;rdar://problem/53375713&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1554803</commentid>
    <comment_count>4</comment_count>
    <who name="Sihui Liu">sihui_liu</who>
    <bug_when>2019-07-22 13:05:20 -0700</bug_when>
    <thetext>Hi Florian,

Thank you for the report.

I think this is related to https://bugs.webkit.org/show_bug.cgi?id=195074, where session storage is lost when process is swapped on navigation to another domain.

I just tested your web page on macOS Mojave with system Safari 12.1, Safari Technology Preview Release 87 and Safari 13.0 beta. (Safari comes with its own WebKit framework). STP and beta load the page correctly, so this should be fixed.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>