<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Katiyarkartik on Medium]]></title>
        <description><![CDATA[Stories by Katiyarkartik on Medium]]></description>
        <link>https://medium.com/@katiyarkartik0?source=rss-cbcd63188048------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*h5Pn3fwFaDhGtetj</url>
            <title>Stories by Katiyarkartik on Medium</title>
            <link>https://medium.com/@katiyarkartik0?source=rss-cbcd63188048------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 15:27:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@katiyarkartik0/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Why you cannot access or modify cookies from the browser/client side, even though cookies are set…]]></title>
            <link>https://medium.com/@katiyarkartik0/why-you-cannot-access-or-modify-cookies-from-the-browser-client-side-even-though-cookies-are-set-90ad1b515a1d?source=rss-cbcd63188048------2</link>
            <guid isPermaLink="false">https://medium.com/p/90ad1b515a1d</guid>
            <category><![CDATA[nextjs-headers]]></category>
            <category><![CDATA[cookies]]></category>
            <category><![CDATA[nextjs]]></category>
            <dc:creator><![CDATA[Katiyarkartik]]></dc:creator>
            <pubDate>Sun, 31 Mar 2024 08:11:50 GMT</pubDate>
            <atom:updated>2024-03-31T08:11:50.700Z</atom:updated>
            <content:encoded><![CDATA[<h3>Why you cannot access or modify cookies from the browser/client side, even though cookies are set on the client side?</h3><pre>import { cookies } from next/headers</pre><p>Cookies are a fundamental mechanism for storing small pieces of data (often used for authentication or user preferences) on a user’s browser. Next.js provides a convenient way to manage cookies within your application using the cookies function imported from next/headers.</p><h3>Can we modify cookies from next/headers in client components ?</h3><p>No, you cannot directly use next/headers to set or modify cookies within client components in Next.js. This is because next/headers interacts with the HTTP request and response headers, which are server-side concepts. Client components run in the user&#39;s browser, and modifying cookies directly from there would pose security risks.</p><h3><strong>So you are saying i can open and see cookies from dev-tools in browser but cannot run a script in browser to access that cookie even though it is in browser?</strong></h3><p>You’re absolutely right. That’s the key security principle at play here. Here’s a more detailed explanation:</p><ul><li><strong>Browser Developer Tools:</strong> When you open the developer tools in your browser (like Chrome DevTools) and inspect cookies, you’re essentially viewing them from a trusted environment provided by the browser itself. This allows developers to diagnose and troubleshoot issues.</li><li><strong>Client-Side Scripting Limitations:</strong> However, JavaScript running on a website (client-side scripts) cannot directly access cookies set for that domain due to security restrictions. This prevents malicious scripts from stealing sensitive information stored in cookies (like login tokens or user preferences).</li><li><strong>Sandboxing:</strong> Browsers implement a concept called “sandboxing” to isolate different parts of a web page. Client-side scripts are confined to their own sandbox and cannot interact with other sandboxes, including the one that stores cookies.</li></ul><p><strong>The Importance of Security:</strong></p><p>This separation is crucial for protecting user data. If any script could access any cookie, it would be a major security vulnerability. Malicious websites could steal login credentials, track users across different sites, or inject unwanted content.</p><h3>Interesting! but how does server interacts with client/browser to modify/access cookies?</h3><p>The answer to this lies in understanding how does request-response cycle works in Nextjs client and server components.</p><h3>Request-Response Cycle with Cookie Setting in Next.js Server Actions</h3><p>Here’s a breakdown of the request-response cycle when you set cookies using next/headers within a Next.js Server Action:</p><p><strong>1. Client-Side Request:</strong></p><ul><li>The user interacts with your Next.js application, triggering a client-side event (e.g., a button click or form submission).</li><li>This event might involve sending data (payload) to the server using methods like fetch or dedicated Next.js mechanisms for Server Actions.</li></ul><p><strong>2. Server-Side Execution:</strong></p><ul><li>The server receives the client-side request containing the payload (if any).</li><li>A Server Action is invoked to handle the request and perform the necessary server-side logic.</li></ul><p><strong>3. Cookie Setting:</strong></p><ul><li>Inside the Server Action, you utilize cookies().set() from next/headers to set the desired cookie. You can specify:</li><li>name: The name of the cookie.</li><li>value: The data you want to store.</li><li>options: Configuration options like path, maxAge, expires, httpOnly, secure (as explained previously).</li></ul><p><strong>4. Response with Headers:</strong></p><ul><li>The Server Action finishes its execution and prepares a response to send back to the client.</li><li>This response includes the HTTP headers that will be used by the browser when it receives the response.</li><li>One of the headers set in this response is the Set-Cookie header, which contains information about the newly created cookie (name, value, and options).</li></ul><p><strong>5. Client-Side Response Handling:</strong></p><ul><li>The client receives the response from the server.</li><li>The browser parses the response headers, including the Set-Cookie header.</li><li>If the Set-Cookie header is present, the browser stores the cookie information locally based on the specified options (path, domain, etc.).</li></ul><p><strong>6. Subsequent Requests:</strong></p><ul><li>From this point onwards, whenever the client makes subsequent requests to the same domain or path (depending on the cookie’s settings), the browser automatically includes the newly created cookie in the request headers. This allows the server to identify the user and potentially access the cookie’s value for further processing.</li></ul><p><strong>Key Points:</strong></p><ul><li>The entire cookie setting process happens on the server-side within the Server Action.</li><li>The client doesn’t directly create or modify cookies through JavaScript.</li><li>The communication between server and client involves sending and receiving HTTP headers, including the Set-Cookie header to instruct the browser on how to handle the cookie.</li></ul><p>By understanding this request-response cycle, you can effectively leverage Server Actions to manage cookies in your Next.js application, ensuring a secure and well-coordinated interaction between server and client.</p><h3>What does the httpOnly flag do in the code snippet below?</h3><pre>cookies().set({<br>    name: &#39;name&#39;,<br>    value: &#39;lee&#39;,<br>    httpOnly: true,<br>    path: &#39;/&#39;,<br>  })</pre><p>When you set the httpOnly flag to true for a cookie, the browser won&#39;t include that cookie in the document.cookie property or make it accessible through JavaScript. This effectively renders the document.cookie approach useless for XSS attacks, as the malicious script cannot access the cookie value at all.</p><h3>See, you said cookies cannot be accessed in the browser, but then you said we use the httpOnly flag to protect the cookies from a browser-side(XSS) attack. But if someone cannot access cookies from the browser in the first place, then how does the httpOnly flag of any use to us?</h3><p>You’re right, there seems to be a slight inconsistency in how I explained cookies and the httpOnly flag. Let me clarify:</p><p><strong>Client-Side Script Limitations:</strong></p><ul><li>By default, client-side JavaScript <strong>cannot directly access cookies</strong> set for the same domain due to security restrictions. This means scripts running on a webpage generally cannot read or modify cookie values.</li></ul><p><strong>However, there’s a nuance:</strong></p><ul><li>Older browsers or specific configurations might allow limited access to cookies through the document.cookie property. This property can potentially be used to read cookie values (in a single string format) for cookies on the same domain and path.</li></ul><p><strong>The </strong>httpOnly<strong> Flag and Its Importance:</strong></p><ul><li>Even though direct access is restricted, the httpOnly flag provides an extra layer of security by preventing cookies with that flag from being included in the document.cookie property. This effectively blocks attempts to exploit document.cookie in scenarios like:</li><li><strong>XSS Attacks:</strong> Cross-Site Scripting vulnerabilities can inject malicious scripts into a website. If a cookie containing sensitive information (like a session token) lacks the HttpOnly flag, the injected script could potentially exploit document.cookie to read the cookie&#39;s value.</li><li><strong>Limited Browser Access:</strong> While uncommon in modern browsers, HttpOnly helps mitigate potential vulnerabilities in older browser versions where document.cookie access might be less restricted.</li></ul><p><strong>Summary:</strong></p><ul><li>HttpOnly doesn&#39;t completely prevent all client-side access, but it specifically protects against vulnerabilities that rely on the document.cookie property.</li><li>Even if someone cannot directly access cookies in most cases, HttpOnly adds security by closing a potential loophole for XSS attacks and older browser behavior.</li></ul><p><strong>Best Practices:</strong></p><ul><li>Always set the httpOnly flag for cookies containing sensitive information like session tokens or authentication credentials.</li><li>Combine httpOnly with other security measures like HTTPS connections and proper input validation to create a robust security posture for your web application.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=90ad1b515a1d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>