-
Notifications
You must be signed in to change notification settings - Fork 1.5k
feat(viewer): add defense-in-depth security headers #735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package viewer | ||
|
|
||
| import "net/http" | ||
|
|
||
| // contentSecurityPolicy locks the viewer down to first-party resources only. | ||
| // The viewer loads no third-party scripts, styles, fonts, or frames, so a | ||
| // strict same-origin policy holds without any 'unsafe-inline' relaxation | ||
| // (the previously-inline session script now lives in static/session.js). | ||
| // This mitigates injection of active content should any user- or LLM-supplied | ||
| // value ever escape HTML escaping in a template. | ||
| const contentSecurityPolicy = "default-src 'self'; " + | ||
| "script-src 'self'; " + | ||
| "style-src 'self'; " + | ||
| "img-src 'self' data:; " + | ||
| "object-src 'none'; " + | ||
| "base-uri 'none'; " + | ||
| "frame-ancestors 'none'; " + | ||
| "form-action 'none'" | ||
|
|
||
| // securityHeaders wraps a handler and sets defense-in-depth response headers on | ||
| // every reply. These harden the local viewer's browser-facing surface (the | ||
| // session JSONL exposed here contains reviewed source code and the LLM's | ||
| // analysis of it). HSTS is intentionally omitted: the viewer serves plain HTTP | ||
| // on loopback, where HSTS is meaningless and would wrongly pin localhost. | ||
| func securityHeaders(next http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| h := w.Header() | ||
| h.Set("Content-Security-Policy", contentSecurityPolicy) | ||
| h.Set("X-Content-Type-Options", "nosniff") | ||
| h.Set("X-Frame-Options", "DENY") | ||
| h.Set("Referrer-Policy", "no-referrer") | ||
| h.Set("Permissions-Policy", "geolocation=(), camera=(), microphone=()") | ||
| next.ServeHTTP(w, r) | ||
| }) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| package viewer | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestSecurityHeadersSetsAllHeaders(t *testing.T) { | ||
| inner := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| w.WriteHeader(http.StatusOK) | ||
| _, _ = w.Write([]byte("ok")) | ||
| }) | ||
|
|
||
| rec := httptest.NewRecorder() | ||
| req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| securityHeaders(inner).ServeHTTP(rec, req) | ||
|
|
||
| want := map[string]string{ | ||
| "Content-Security-Policy": contentSecurityPolicy, | ||
| "X-Content-Type-Options": "nosniff", | ||
| "X-Frame-Options": "DENY", | ||
| "Referrer-Policy": "no-referrer", | ||
| "Permissions-Policy": "geolocation=(), camera=(), microphone=()", | ||
| } | ||
| for k, v := range want { | ||
| if got := rec.Header().Get(k); got != v { | ||
| t.Errorf("header %q = %q, want %q", k, got, v) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // The viewer serves plain HTTP on loopback; HSTS would wrongly pin localhost. | ||
| func TestSecurityHeadersOmitsHSTS(t *testing.T) { | ||
| inner := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}) | ||
| rec := httptest.NewRecorder() | ||
| req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
| securityHeaders(inner).ServeHTTP(rec, req) | ||
|
|
||
| if got := rec.Header().Get("Strict-Transport-Security"); got != "" { | ||
| t.Errorf("HSTS should not be set on the loopback viewer, got %q", got) | ||
| } | ||
| } | ||
|
|
||
| // The CSP must stay strict: no 'unsafe-inline'/'unsafe-eval' relaxation, since | ||
| // the formerly-inline session script now lives in static/session.js. | ||
| func TestContentSecurityPolicyIsStrict(t *testing.T) { | ||
| for _, bad := range []string{"unsafe-inline", "unsafe-eval", "*"} { | ||
| if strings.Contains(contentSecurityPolicy, bad) { | ||
| t.Errorf("CSP unexpectedly contains %q: %s", bad, contentSecurityPolicy) | ||
| } | ||
| } | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| document.querySelectorAll('.response-text').forEach(function(el) { | ||
| const text = el.textContent; | ||
| const esc = function(s) { | ||
| return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>') | ||
| .replace(/"/g, '"').replace(/'/g, '''); | ||
| }; | ||
| const codeBlocks = []; | ||
| let html = esc(text); | ||
| html = html.replace(/ | ||
| html = html | ||
| .replace(/`([^`]+)`/g, '<code class="inline-code">$1</code>') | ||
| .replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>') | ||
| .replace(/^### (.+)$/gm, '<div class="md-h3">$1</div>') | ||
| .replace(/^## (.+)$/gm, '<div class="md-h2">$1</div>') | ||
| .replace(/^# (.+)$/gm, '<div class="md-h1">$1</div>') | ||
| .replace(/^[-*] (.+)$/gm, '<div class="md-li">• $1</div>') | ||
| .replace(/\n{2,}/g, '<br><br>') | ||
| .replace(/\n/g, '<br>'); | ||
| codeBlocks.forEach(function(code, i) { | ||
| html = html.replace('%%CODEBLOCK_' + i + '%%', | ||
| '<pre class="code-block"><code>' + code + '</code></pre>'); | ||
| }); | ||
| el.innerHTML = html; | ||
| }); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -231,35 +231,7 @@ <h4 class="task-type-label"> | |
| {{end}} | ||
| </div> | ||
|
|
||
| <script> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Factory |
||
| document.querySelectorAll('.response-text').forEach(function(el) { | ||
| var text = el.textContent; | ||
| var esc = function(s) { | ||
| return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>') | ||
| .replace(/"/g, '"').replace(/'/g, '''); | ||
| }; | ||
| var codeBlocks = []; | ||
| var html = esc(text); | ||
| html = html.replace(/```(\w*)\n([\s\S]*?)```/g, function(_, lang, code) { | ||
| codeBlocks.push(code.replace(/^\n|\n$/g, '')); | ||
| return '%%CODEBLOCK_' + (codeBlocks.length - 1) + '%%'; | ||
| }); | ||
| html = html | ||
| .replace(/`([^`]+)`/g, '<code class="inline-code">$1</code>') | ||
| .replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>') | ||
| .replace(/^### (.+)$/gm, '<div class="md-h3">$1</div>') | ||
| .replace(/^## (.+)$/gm, '<div class="md-h2">$1</div>') | ||
| .replace(/^# (.+)$/gm, '<div class="md-h1">$1</div>') | ||
| .replace(/^[-*] (.+)$/gm, '<div class="md-li">• $1</div>') | ||
| .replace(/\n{2,}/g, '<br><br>') | ||
| .replace(/\n/g, '<br>'); | ||
| codeBlocks.forEach(function(code, i) { | ||
| html = html.replace('%%CODEBLOCK_' + i + '%%', | ||
| '<pre class="code-block"><code>' + code + '</code></pre>'); | ||
| }); | ||
| el.innerHTML = html; | ||
| }); | ||
| </script> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ,,, |
||
| <script src="/static/session.js"></script> | ||
| </main> | ||
| </body> | ||
| </html> | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revisado