How to safely pass Redux store from the server to the client

azangru

I am working on a React app with server-side rendering. The app uses Redux for state management and Redux Saga for async actions. In pseudocode, what I am doing on the server side right now is:

1) initialize Redux store and run Redux saga on it
2) wait until all necessary data is fetched
3) render React component to string
4) send rendered React component to the client
   (along with the populated store for rehydration on the client side)

My problem is with step 4. Right now, I am passing the rendered React component and the store to an ejs view that looks roughly like this:

<html>
    <head>
        <meta charset="utf-8">
        <title>Test</title>
        <link rel="stylesheet" type="text/css" href="/styles.css">
        <script>
            var __data = <%-JSON.stringify(store) %>;
        </script>
    </head>
    <body>

        <main><%- app %></main>

        <script type="text/javascript" src="/vendor.js"></script>
        <script type="text/javascript" src="/bundle.js"></script>
    </body>
</html>

(in the code above, app is the rendered React component, and store is the Redux store)

Trouble is, the above code, specifically

<script>
    var __data = <%-JSON.stringify(store) %>;
</script>

does not escape html, so if my store contains a string with the <script> tag, it will be valid html, and opens the app to XSS attacks.

If instead of <%- %> use <%= %> like so: var __data = "<%= JSON.stringify(store) %>";, I get a string that is invalid JSON:

"{&#34;greetingReducer&#34;:{&#34;message&#34;:&#34;Hello world!&#34;} etc.

which I am not sure how to transform back to valid JSON.

So my question (silly as it may seem) is: what’s a proper way of passing a Redux store (HTML-escaped string of a JSON object) in the HTML template to the client and how to transform that string back to a JavaScript object on the client.

Abhishek Nair

You can try serialize-javascript if you're ok with including another module to do this. It has support to escape harmful HTML strings.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pass value to AppolloBoost client from Redux?

How to store client in redux?

How to store an array of objects from server to client

How to pass data from Server to Client in Meteor

How to pass CSRF token from server to client?

How do I pass redux store from one component to another?

How To Pass Props From Redux Store into Apollo GraphQL Query

Async props pass in React: How to pass a key to update redux store in react? [First from the higher component and then from redux store]

How to initialize globally redux store with data from server?

How to display and store images from a server on the client side?

Pass values from a component to Redux Store

how to pass jwt from swift client to node.js server

How to Pass Image from Server to Client Flask with AJAX

How to pass a parameter from client side to server in python

how to send redux store to the client when routing in redux

Data fetched from server not loading into redux store?

How to recieve a buffer of ints and strings from a client , and store them right? (cpp server , python client)

Store Variable value from server to client

"Safely" store (on client side) tokens for reuse?

How to safely pass value from WP Forms to PHP code?

Meteor pass data from client to server

Pass data from client side to server side

NodeJS - Pass an array from client to server with ajax

how to store client's data in the server

How to safely store secret key?

Should I use the Redux store to pass data from child to parent?

How to load app state asynchronously from server while creating store in flutter redux?

How to Get Param Id from URL in express/mongo/mongoose on server side, axios/react/redux on client side

How to read from redux persisted store (localstorage)