3

Is it possible for an included script that is hosted on a different domain to access the local storage of the current domain? This still remains unclear for me after reading https://developer.mozilla.org/de/docs/Web/API/Window/localStorage

For example: mydomain.com includes <script src="https://www.youtube.com/iframe_api" async></script>. Can this included script access the localstorage from mydomain.com?

asked Jul 30, 2022 at 5:24
1

1 Answer 1

2

Scripts you include in your page using <script> can definitely access Local storage, they are running in same origin as your other scripts. This is also the reason behind advisories on not to store authentication tokens inside Local storage, because an injected script using an XSS attack can read and write to the Local storage.

This is however different for an <iframe> since they have their own origin.

answered Jul 30, 2022 at 7:34
4
  • Thank you. You nailed it, since I was intending to store a session id in localstorage. Since I have no backend available, is there a way to store a session id? Can localstorage be configured such that only scripts LOADED from the same domain can access it? Commented Jul 30, 2022 at 20:09
  • Would it be save if EVERY external script would be self-hosted (and checked for localstorage access), such that it would be impossible for another script to be injected? Commented Jul 30, 2022 at 20:15
  • @SimonFerndriger I don't believe this is possible. the best practice is to store it inside an http-only cookie. Regarding self-hosted scripts, you should not only check for localstorage access but also including a third script or any other type of executing a dynamic piece of code! (this self-hosed script can download and execute another remote script) Commented Jul 30, 2022 at 20:22
  • 1
    @SimonFerndriger maybe this answer gives some insight on the security side security.stackexchange.com/a/15193/107894 Commented Jul 30, 2022 at 20:27

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.