<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Pandu Mangalawadkar</title>
    <description>The latest articles on DEV Community by Pandu Mangalawadkar (@pandu_mangalawadkar_75e98).</description>
    <link>https://dev.to/pandu_mangalawadkar_75e98</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3216806%2Fae3bcc9c-08d4-42cb-9d59-702c2e280890.png</url>
      <title>DEV Community: Pandu Mangalawadkar</title>
      <link>https://dev.to/pandu_mangalawadkar_75e98</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/pandu_mangalawadkar_75e98"/>
    <language>en</language>
    <item>
      <title>vulnerabilities -ISSUE-Improper control of generation of code ('Code Injection')</title>
      <dc:creator>Pandu Mangalawadkar</dc:creator>
      <pubDate>Wed, 28 May 2025 06:35:25 +0000</pubDate>
      <link>https://dev.to/pandu_mangalawadkar_75e98/vulnerabilities-issue-improper-control-of-generation-of-code-code-injection-2ha</link>
      <guid>https://dev.to/pandu_mangalawadkar_75e98/vulnerabilities-issue-improper-control-of-generation-of-code-code-injection-2ha</guid>
      <description>&lt;p&gt;this is my code &lt;/p&gt;

&lt;p&gt;import { useRef, useCallback, useEffect } from 'react';&lt;br&gt;
import { getInactivityTimeout } from '../../sessionConfig';&lt;/p&gt;

&lt;p&gt;const useInactivityLogout = () =&amp;gt; {&lt;br&gt;
    const timerRef = useRef(null);&lt;br&gt;
    const eventHandlerSet = useRef(false);&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const logout = useCallback(() =&amp;gt; {
    // Clear access token for frontend logout
    sessionStorage.removeItem('access_token');
}, []);

const resetInactivityTimer = useCallback(() =&amp;gt; {
    const now = Date.now();
    sessionStorage.setItem('lastActivityTime', now.toString());

    if (timerRef.current) {
        clearTimeout(timerRef.current);
    }

    // Defensive: Ensure getInactivityTimeout() returns a safe number
    const safeTimeout = Math.max(0, Number(getInactivityTimeout()) || 0);

    // Safe: setTimeout is used with a function, no string eval here
    timerRef.current = setTimeout(() =&amp;gt; {
        const stored = sessionStorage.getItem('lastActivityTime');
        const storedTime = stored !== null &amp;amp;&amp;amp; !Number.isNaN(stored) ? Number(stored) : null;

        // Defensive: Check inactivity and logout if necessary
        if (
            stored !== null &amp;amp;&amp;amp;
            storedTime !== null &amp;amp;&amp;amp;
            !Number.isNaN(storedTime) &amp;amp;&amp;amp;
            Date.now() - storedTime &amp;gt;= safeTimeout
        ) {
            logout();
        }
    }, safeTimeout);
}, [logout]);

const setupInactivityListeners = useCallback(() =&amp;gt; {
    if (!eventHandlerSet.current) {
        const events = ['mousemove', 'keydown', 'click'];
        const activityHandler = () =&amp;gt; resetInactivityTimer();

        events.forEach(event =&amp;gt; window.addEventListener(event, activityHandler));

        eventHandlerSet.current = true;
        resetInactivityTimer();
    }
}, [resetInactivityTimer]);

useEffect(
    () =&amp;gt; () =&amp;gt; {
        if (timerRef.current) {
            clearTimeout(timerRef.current);
        }
    },
    []
);

return { setupInactivityListeners };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;};&lt;/p&gt;

&lt;p&gt;export default useInactivityLogout;&lt;/p&gt;

&lt;p&gt;Note :The application was found calling the dangerous dynamic execution \nmethods with untrusted input. These functions can lead to code \ninjection, allowing attackers to execute arbitrary JavaScript \ncode, which may result in severe security risks such as remote \ncode execution (RCE) on the server or Cross-Site Scripting (XSS) \nin the browser (DOM XSS). Code injection occurs when untrusted \nuser input is passed directly to functions that execute code from \nstrings. When these vulnerable functions are used in client-side \ncode, they can lead to DOM-based XSS attacks, allowing attackers \nto inject and execute malicious scripts within the user's browser.\n\nTo remediate this issue, take the following measures:\n1. Avoid using dangerous functions with strings: Use safe alternatives \nby passing functions as arguments to setTimeout(), setInterval(), and\navoid using eval(), Function() and execScript().\n2. Sanitize user input: Sanitize untrusted inputs from the DOM or URL \nbefore using them in any context that may lead to code execution.\n3. Use safer alternatives: Replace innerHTML with textContent or \ninnerText when updating the DOM, and avoid passing user input to \neval()-like functions.\n4. Validate inputs: Ensure that user input is validated and conforms \nto expected data types and formats before processing it.\n\nSecure Code Example:\n&lt;br&gt;
&lt;br&gt;
&lt;code&gt;\nvar userInput = location.search.substring(1);\n\n// Safe: Using a function instead of a string for setTimeout\nsetTimeout(function() {\n    console.log("User input: " + userInput);  // Safe: No code execution\n}, 1000);\n&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
\n\nFor more information on why not to use &lt;code&gt;eval&lt;/code&gt;, and alternatives see:\n- &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#never_use_eval!%5Cn%5CnOther" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#never_use_eval!\n\nOther&lt;/a&gt; References:\n- &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function%5Cn-" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/Function\n-&lt;/a&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/setTimeout%5Cn-" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/setTimeout\n-&lt;/a&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/setInterval%5Cn" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/setInterval\n&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
