feat: Add middleware pattern with native per-request nonce support#6
Conversation
Introduces v0.2 API with native TanStack Start middleware pattern for per-request CSP nonce generation. Maintains backward compatibility with v0.1 handler wrapper API. New Features: - createCspMiddleware() for TanStack Start global middleware - createNonceGetter() for isomorphic nonce access (server + client) - generateNonce() for cryptographic random nonce generation - buildCspHeader() for low-level CSP header building - CSP Level 3 support with granular directive copying - Strict nonce-based CSP (no unsafe-inline for scripts in production) Breaking Changes: - Adds @tanstack/start-storage-context peer dependency - Recommended API changed from handler wrapper to middleware pattern Security Improvements: - Per-request nonce generation (vs static headers) - No unsafe-inline fallback for scripts - Integration with TanStack router ssr.nonce option - Support for strict-dynamic CSP directive The old createSecureHandler API remains available but is deprecated. See README for migration guide and new API documentation.
🦋 Changeset detectedLatest commit: 7a5cdaf The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Summary of ChangesHello @Enalmada, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request delivers a significant update to the security header management library for TanStack Start applications. It refactors the core mechanism from a handler wrapper to a native middleware pattern, primarily to enable robust, per-request Content Security Policy (CSP) nonce generation. This change dramatically improves the application's security posture by ensuring that scripts are executed only if explicitly authorized by a unique, cryptographically secure token, thereby mitigating common cross-site scripting (XSS) vulnerabilities. The update also streamlines integration with the TanStack Router's built-in nonce capabilities and provides a clear migration path for existing users. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a significant and well-executed update to implement a middleware pattern with per-request nonce support, which is a great security improvement. The code is generally clean and the documentation updates are thorough. I've identified a few areas for improvement, mainly concerning a potential security issue in the documentation's example code, and some opportunities to enhance code robustness and maintainability in the new logic. My feedback focuses on correcting the documentation example and refactoring small parts of the code for better clarity and resilience.
Consolidate development branches - add-ci-workflows was accidentally set as default branch. This merge brings in all changes including: - Middleware pattern with per-request nonce support (#6) - Code review fixes (package.json exports, CSP builder improvements) - Workflow fixes (changesets-renovate.yml lockfile path) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Summary
This PR introduces a major update (v0.2) that adds native middleware support for TanStack Start with per-request nonce generation. This replaces the previous handler wrapper pattern with a more integrated and secure approach.
Key Changes:
createCspMiddleware()- Middleware factory for TanStack Start with per-request nonce generationcreateNonceGetter()- Isomorphic nonce retrieval (works on server and client)generateNonce()- Cryptographically secure random nonce generatorbuildCspHeader()- Low-level CSP header building utility-elem,-attr)'unsafe-inline'in production)ssr.nonceoptioncreateSecureHandler(v0.1 API) - kept for backward compatibilitySecurity Improvements:
'unsafe-inline'fallback for scripts in production'strict-dynamic'CSP directiveBreaking Changes:
@tanstack/start-storage-context >= 1.0.0Test plan
🤖 Generated with Claude Code