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

Detect if access to localStorage is forbidden by the user's browser #55080

Merged
merged 3 commits into from
Oct 18, 2018

Conversation

thanatos
Copy link
Contributor

If the user's cookie/persistent storage setting forbid access to localStorage, catch the exception and abort the access.

Currently, attempting to use the expand/contract links at the top of the page for structs/consts/etc. fails due to an unhandled error while accessing localStorage, if such access is forbidden, as the exception from the failed access propagates all the way out, interrupting the expand/contract. Instead, I would like to degrade gracefully; the access won't happen (the collapse/expand state won't get persisted) but the actual expanding/contracting of the item will go on to succeed.

Fixes #55079

@rust-highfive
Copy link
Collaborator

Some changes occurred in HTML/CSS.

cc @GuillaumeGomez

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @GuillaumeGomez (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 15, 2018
@thanatos
Copy link
Contributor Author

Note: I'm not sure how to actually test this change, so I haven't done that yet.

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-5.0 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.

[00:11:11] travis_fold:start:tidy
travis_time:start:tidy
tidy check
[00:11:12] tidy error: /checkout/src/librustdoc/html/static/storage.js:31: tab character
[00:11:12] tidy error: /checkout/src/librustdoc/html/static/storage.js:32: tab character
[00:11:12] tidy error: /checkout/src/librustdoc/html/static/storage.js:33: tab character
[00:11:12] tidy error: /checkout/src/librustdoc/html/static/storage.js:34: tab character
[00:11:12] tidy error: /checkout/src/librustdoc/html/static/storage.js:35: tab character
[00:11:12] tidy error: /checkout/src/librustdoc/html/static/storage.js:36: tab character
[00:11:13] some tidy checks failed
[00:11:13] 
[00:11:13] 
[00:11:13] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-tools-bin/tidy" "/checkout/src" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "--no-vendor" "--quiet"
[00:11:13] 
[00:11:13] 
[00:11:13] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test src/tools/tidy
[00:11:13] Build completed unsuccessfully in 0:00:46
[00:11:13] Build completed unsuccessfully in 0:00:46
[00:11:13] make: *** [tidy] Error 1
[00:11:13] Makefile:79: recipe for target 'tidy' failed

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:04bca9c8
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
---
travis_time:end:0213adc0:start=1539564062655847355,finish=1539564062663845416,duration=7998061
travis_fold:end:after_failure.3
travis_fold:start:after_failure.4
travis_time:start:07826e80
$ ln -s . checkout && for CORE in obj/cores/core.*; do EXE=$(echo $CORE | sed 's|obj/cores/core\.[0-9]*\.!checkout!\(.*\)|\1|;y|!|/|'); if [ -f "$EXE" ]; then printf travis_fold":start:crashlog\n\033[31;1m%s\033[0m\n" "$CORE"; gdb --batch -q -c "$CORE" "$EXE" -iex 'set auto-load off' -iex 'dir src/' -iex 'set sysroot .' -ex bt -ex q; echo travis_fold":"end:crashlog; fi; done || true
travis_fold:end:after_failure.4
travis_fold:start:after_failure.5
travis_time:start:0dbff480
travis_time:start:0dbff480
$ cat ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers || true
cat: ./obj/build/x86_64-unknown-linux-gnu/native/asan/build/lib/asan/clang_rt.asan-dynamic-i386.vers: No such file or directory
travis_fold:end:after_failure.5
travis_fold:start:after_failure.6
travis_time:start:31a79876
$ dmesg | grep -i kill

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

If the user's cookie/persistent storage setting forbid access to localStorage,
catch the exception and abort the access.

Currently, attempting to use the expand/contract links at the top of the page
for structs/consts/etc. fails due to an unhandled error while accessing
localStorage, if such access is forbidden, as the exception from the failed
access propagates all the way out, interrupting the expand/contract. Instead, I
would like to degrade gracefully; the access won't happen (the collapse/expand
state won't get persisted) but the actual expanding/contracting of the item
will go on to succeed.

Fixes rust-lang#55079
@GuillaumeGomez
Copy link
Member

Thanks for the PR! Please update the "getCurrentValue" function (just below the one you updated) too.

…tValue()

1. Extract the tests for whether or not we have workable localStorage out into
   a helper method, so it can be more easily reused
2. Use it in getCurrentValue() too, for the same reasons, as suggested in code
   review
@thanatos
Copy link
Contributor Author

Given the way getCurrentValue was written, it was much easier to update that function if the check itself was a separate function, so I've extracted the code to a separate function. (Which, honestly, I think was the right thing overall, and makes the update function nicer too.)

LMK if there's anything else needed; also, feel free to squash/not squash however the rust-lang folks like to do.

I ran cargo doc in a project of mine and replaced most of the storage.js that cargo emitted w/ the file as it is in my revision. (The exception being that the one cargo emits contains,

var resourcesSuffix="";

just prior to the copyright banner; I have no idea where that comes from, so I left it for my test, only replacing the stuff below the copyright banner. If there's some more rigorous test I could perform, LMK, and I'll do that too.)

return localStorage[name];
}
return null;
}

function usableLocalStorage() {
Copy link
Member

Choose a reason for hiding this comment

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

Can move this function above the two others please? It's just me being careful but better be safe than sorry. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

The order shouldn't matter; in JS, function declarations are "hoisted" to the top of the file (in the case that they're at the top-level, and not contained in another function), so they appear to happen prior to code actually running. (Function expressions are not, and the MDN article notes that.)

Copy link
Member

Choose a reason for hiding this comment

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

Try this code then:

foo();

function foo() {
    console.log('hello');
}

@GuillaumeGomez
Copy link
Member

Thanks!

@bors: r+ rollup

@bors
Copy link
Contributor

bors commented Oct 17, 2018

📌 Commit cbe98ec has been approved by GuillaumeGomez

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 17, 2018
kennytm added a commit to kennytm/rust that referenced this pull request Oct 18, 2018
…GuillaumeGomez

Detect if access to localStorage is forbidden by the user's browser

If the user's cookie/persistent storage setting forbid access to `localStorage`, catch the exception and abort the access.

Currently, attempting to use the expand/contract links at the top of the page for structs/consts/etc. fails due to an unhandled error while accessing `localStorage`, if such access is forbidden, as the exception from the failed access propagates all the way out, interrupting the expand/contract. Instead, I would like to degrade gracefully; the access won't happen (the collapse/expand state won't get persisted) but the actual expanding/contracting of the item will go on to succeed.

Fixes rust-lang#55079
bors added a commit that referenced this pull request Oct 18, 2018
Rollup of 18 pull requests

Successful merges:

 - #54646 (improve documentation on std::thread::sleep)
 - #54933 (Cleanup the rest of codegen_llvm)
 - #54964 (Run both lldb and gdb tests)
 - #55016 (Deduplicate some code and compile-time values around vtables)
 - #55031 (Improve verify_llvm_ir config option)
 - #55050 (doc std::fmt: the Python inspiration is already mentioned in precedin…)
 - #55077 (rustdoc: Use dyn keyword when rendering dynamic traits)
 - #55080 (Detect if access to localStorage is forbidden by the user's browser)
 - #55090 (regression test for move out of borrow via pattern)
 - #55102 (resolve: Do not skip extern prelude during speculative resolution)
 - #55104 (Add test for #34229)
 - #55111 ([Rustc Book] Explain --cfg's arguments)
 - #55122 (Cleanup mir/borrowck)
 - #55127 (Remove HybridBitSet::dummy)
 - #55128 (Fix LLVMRustInlineAsmVerify return type mismatch)
 - #55142 (miri: layout should not affect CTFE checks (outside of validation))
 - #55151 (Cleanup nll)
 - #55161 ([librustdoc] Disable spellcheck for search field)
@bors bors merged commit cbe98ec into rust-lang:master Oct 18, 2018
@kornelski
Copy link
Contributor

kornelski commented Oct 25, 2018

I think it'd be safer and more future-proof to wrap getCurrentValue and updateLocalStorage body try/catch.

For example, if browsers implemented read-only localStorage current check would pass, but write could still throw.

@GuillaumeGomez
Copy link
Member

The write can only be performed on the setting page, therefore it's not really an issue if it doesn't work. The setting will just not get updated.

@thanatos thanatos deleted the fix-localstorage-crash branch August 22, 2020 22:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants