Skip to content

[ISSUE]: CSRF Token Cookie Parsing Logic Bug in security.js #396

Closed

Description

@Aditya8369

Description

Description:
In getCSRFToken() function in frontend/js/security.js, the cookie parsing logic has a subtle bug. The loop uses indexOf() incorrectly - it checks if the cookie string CONTAINS the search term anywhere but doesn't properly match cookie boundaries:

// In getCSRFToken (lines 159-167):
for (let i = 0; i < cookieArray.length; i++) {
    let cookie = cookieArray[i].trim()
    if (cookie.indexOf(name) === 0) {  // BUG: indexOf returns position, not boolean!
        return cookie.substring(name.length)
    }
}

The issue is that cookie.indexOf(name) === 0 is incorrect because:

  1. It should use cookie.startsWith(name) or compare with === 0
  2. More critically, the code should also check for = after the cookie name to properly parse cookie values

Steps to fix:

  1. Open frontend/js/security.js
  2. Find the getCSRFToken() function
  3. Replace the cookie parsing loop with:
    for (let i = 0; i < cookieArray.length; i++) {
        let cookie = cookieArray[i].trim();
        // Properly check if this cookie starts with the name and has = separator
        if (cookie.startsWith(name) || cookie.indexOf(name + '=') === 0) {
            // Handle both "name=value" and "name = value" formats
            let prefix = name + '=';
            if (cookie.startsWith(prefix)) {
                return cookie.substring(prefix.length);
            }
            // Handle without = sign (edge case)
            return cookie.substring(name.length);
        }
    }

Additional Context

No response

Activity

  1. Aditya8369 commented on May 1, 2026

    @Aditya8369
    ContributorAuthor

    @devanshi14malhotra please assign me

  2. added
    NSoC'26For issues and PRs which are a part of the Nexus Spring of Code open source event
    on May 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    NSoC'26For issues and PRs which are a part of the Nexus Spring of Code open source eventfrontendlevel25 points

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions