-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Enhance gin security and configuration options #18
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
5 commits
Select commit
Hold shift + click to select a range
3506add
feat: Add security headers and noindex
timokoessler 596ed79
fix: Justfile not working on Windows
timokoessler d6ae147
feat: CSRF protection and trusted proxies
timokoessler 9ac32aa
feat: Add host env var
timokoessler d18a37c
Apply review suggestions
timokoessler 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ dist-ssr | |
| .nitro | ||
| .tanstack | ||
| bin | ||
| .claude/settings.local.json | ||
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
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,38 @@ | ||
| package middleware | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "strings" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| var safeMethods = map[string]bool{ | ||
| http.MethodGet: true, | ||
| http.MethodHead: true, | ||
| http.MethodOptions: true, | ||
| } | ||
|
|
||
| func ValidateOrigin(appURL string) gin.HandlerFunc { | ||
| return func(c *gin.Context) { | ||
| if safeMethods[c.Request.Method] { | ||
| c.Next() | ||
| return | ||
| } | ||
|
|
||
| origin := strings.TrimSuffix(c.GetHeader("Origin"), "/") | ||
| if origin == "" { | ||
| c.String(http.StatusForbidden, "403 forbidden: missing origin header") | ||
| c.Abort() | ||
| return | ||
| } | ||
|
|
||
| if origin != appURL { | ||
| c.String(http.StatusForbidden, "403 forbidden: invalid origin") | ||
| c.Abort() | ||
| return | ||
| } | ||
|
|
||
| c.Next() | ||
| } | ||
| } |
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,18 @@ | ||
| package middleware | ||
|
|
||
| import "github.com/gin-gonic/gin" | ||
|
|
||
| func SecurityHeaders() gin.HandlerFunc { | ||
| return func(c *gin.Context) { | ||
| c.Header("X-Frame-Options", "DENY") | ||
| c.Header("X-Content-Type-Options", "nosniff") | ||
| c.Header("Referrer-Policy", "same-origin") | ||
| c.Header("X-Robots-Tag", "noindex, nofollow, noarchive") | ||
| c.Header("Content-Security-Policy", "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; frame-ancestors 'none'; base-uri 'self'; object-src 'none'") | ||
| c.Header("Cross-Origin-Opener-Policy", "same-origin") | ||
| c.Header("Cross-Origin-Resource-Policy", "same-origin") | ||
| c.Header("Cross-Origin-Embedder-Policy", "require-corp") | ||
|
|
||
| c.Next() | ||
| } | ||
| } |
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,50 @@ | ||
| package hub | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "net/url" | ||
| "strings" | ||
| ) | ||
|
|
||
| var defaultPorts = map[string]string{ | ||
| "http": "80", | ||
| "https": "443", | ||
| } | ||
|
|
||
| // parseAppURL validates and normalises the APP_URL value. | ||
| // It must contain a scheme and host, must not contain a path (other than a | ||
| // single leading slash which is stripped), and must not contain a query or | ||
| // fragment. | ||
| func parseAppURL(raw string) (string, error) { | ||
| if raw == "" { | ||
| return "", errors.New("APP_URL is required") | ||
| } | ||
|
|
||
| u, err := url.Parse(raw) | ||
| if err != nil { | ||
| return "", fmt.Errorf("APP_URL is not a valid URL: %w", err) | ||
| } | ||
|
|
||
| if u.Scheme == "" || u.Host == "" { | ||
| return "", errors.New("APP_URL must include a scheme and host (e.g. https://example.com)") | ||
| } | ||
|
|
||
timokoessler marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| path := strings.TrimPrefix(u.Path, "/") | ||
| if path != "" { | ||
| return "", errors.New("APP_URL must not include a path") | ||
| } | ||
|
|
||
| if u.RawQuery != "" || u.Fragment != "" { | ||
| return "", errors.New("APP_URL must not include a query string or fragment") | ||
| } | ||
|
|
||
| scheme := strings.ToLower(u.Scheme) | ||
| host := strings.ToLower(u.Hostname()) | ||
| port := u.Port() | ||
| if port != "" && port != defaultPorts[scheme] { | ||
| host = host + ":" + port | ||
| } | ||
|
|
||
| return scheme + "://" + host, nil | ||
| } | ||
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,2 @@ | ||
| User-agent: * | ||
| Disallow: / |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.