Inline scripts are duplicated after hydration when the CSP nonce is sent in a header
Maintainer thường phản hồi trong vòng 1 ngày
Đánh giá
- Độ khó
- 1/5
- Thời gian dự kiến
- Dưới một giờ
- Mức phù hợp với người mới
- 90/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- react, typescript
- Lĩnh vực
- frontend
Hướng nghiên cứu
Bắt đầu trong packages/react-router/src/Asset.tsx, tại phần tìm kiếm inline script quanh dòng 204. Kiểm tra cách nonce của script hiện có được đọc trong quá trình hydration, sau đó xác minh hành vi bằng reproducer tối giản được liên kết hoặc một ứng dụng Start sử dụng CSP nonce trong response header. Hoàn thành khi script được server render được khớp và không có script trùng lặp nào được thêm vào.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Which project does this relate to?
Router
Describe the bug
After hydration, Script looks for an existing inline script with the same text, type and nonce before it creates a new one:
When the CSP arrives in a response header, browsers apply nonce hiding. On a connected element, getAttribute('nonce') returns '', and only the .nonce property keeps the value. So the comparison is '' vs. the real nonce, and the server-rendered script never matches. Script then creates a new <script type="module" nonce>import("/assets/index-….js")</script>, sets its textContent, and appends it to <head>.
Complete minimal reproducer
https://github.com/gabriel-le/tanstack-nonce-inline-scripts-duplication
Steps to Reproduce the Bug
This works in any Start app:
- Set
ssr.nonceon the router and sendContent-Security-Policy: script-src 'nonce-<n>'as a response header. - Load a page.
document.head.querySelectorAll('script[type=module]')includes a re-addedimport("/assets/…")script. ItsgetAttribute('nonce')is'', and its.noncehas the value. - Control: send the same policy in a
<meta http-equiv>, or send no policy. Nonces aren't hidden, the lookup matches, and nothing is added. - Add
; require-trusted-types-for 'script'to the header. Hydration fails with the error above.
I tested this using react-router, but the issue is likely the same in solid and vue
Expected behavior
The lookup matches the server-rendered script, and nothing is created or appended.
Proposed fix
- const sNonce = el.getAttribute('nonce') ?? undefined
+ const sNonce = el.nonce || undefined
The fix uses || rather than ?? because .nonce is '' when an element has no nonce. With ??, apps that don't use a nonce would start creating duplicates.
Screenshots or Videos
No response
Platform
- Router / Start Version: 1.170.40
- OS: macOS
- Browser: Tested in chrome, safari and firefox
- Browser Version: Safari Version 27.2 (22625.2.5.11.1)
- Bundler: vite
- Bundler Version: 8.0.0
Additional context
No response
- Ngôn ngữ chính
- TypeScript
- Star
- 15.2k
- Fork
- 1.9k
- Merge trung bình
- 1 ngày 11 giờ
- Pull request đã merge (30 ngày)
- 73
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của TanStack/router
-
docs: `createLink` has no API reference page; 3 how-to guides link to a missing `#createlink` anchorĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
start-plugin-core: sitemap lastmod and pages.json lastBuilt are stamped with the build's wall clock (no SOURCE_DATE_EPOCH support)Có thể đã có người làm @treadnot đã nhận 22 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Empty .ts route file is scaffolded with JSX, breaking all route generationCó thể đã có người làm @nathan-logan đã nhận 26 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
information needed
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
TanStack/router#8464 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
start-compiler-plugin fails to lazy-load dependencies with Vite bundledDevCó thể đã có người làm @lx3133584 đã nhận 7 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
TanStack/router#8407 · 1 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của TanStack/router
Issue tương tự
-
"Contains" page watch never matches when its text has a trailing spaceCó thể đã có người làm @RaphaelFakhri đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
CopilotKit/openmuse#184 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
bootstrap-vue-next/bootstrap-vue-next#3357 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
[quality] tide.ts: six mutation survivors in the merge-queue gate, draft refusal, fork-403 workflow-file diagnosis and getPull's fork flag are unpinned by any testCó thể đã có người làm @hivecommons-hive đã nhận hôm nay. Đang mởagent/quality needs-kind stage/040-covered-by-pr testing
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
cncf/prow-github-actions#517 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Unsampled pageload span is profiled when profileLifecycle is 'trace' (browser UI profiling)Có thể đã có người làm @Jxxunnn đã nhận hôm nay. Đang mởBrowser Waiting for: Product Owner
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
getsentry/sentry-javascript#25497 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày