OKTA-1093549 - CSRF on todo0 and agent0 applications in Secure AI Agents Example#14
Merged
iamspathan merged 1 commit intomainfrom Jan 29, 2026
Merged
OKTA-1093549 - CSRF on todo0 and agent0 applications in Secure AI Agents Example#14iamspathan merged 1 commit intomainfrom
iamspathan merged 1 commit intomainfrom
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Approach
Session-Based CSRF Tokens: Generate a cryptographically secure token using Node's built-in
crypto.randomUUID()and store it in the user's session.Token Validation Middleware: Validate the token on all state-changing requests (POST, PUT, DELETE, PATCH). Reject requests with missing or invalid tokens with 403 Forbidden.
Token Delivery:
<meta>tag for JavaScript access_csrffield in all formsImplementation (minimal, no external dependencies):
Files Changed
packages/todo0/src/app-server.tscrypto.randomUUID()packages/todo0/views/index.ejs<meta name="csrf-token">tag; added hidden_csrffields to all formsVerification
Automated tests confirm:
Design Decisions
No external library: Used Node's built-in
crypto.randomUUID()instead of adding dependencies likecsurf(deprecated) orcsrf-csrf. This keeps the sample app simple.Preserved
sameSite: 'lax': Did not change tostrictbecause it would break OAuth callback flow where Okta redirects back to the app.agent0 not modified: The
/api/chatendpoint usesfetch()withcredentials: 'include', not form submission. Cross-origin fetch requests don't include cookies withsameSite: 'lax', so CSRF protection is already in place for API calls.