-
Notifications
You must be signed in to change notification settings - Fork 662
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
KostLinux
authored
Mar 28, 2024
1 parent
eab1fd8
commit 0ebcc73
Showing
4 changed files
with
143 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 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 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,41 @@ | ||
# Example of a secure web application with Gin | ||
|
||
This is an example of a secure web application with Gin. It includes the following security headers: | ||
|
||
- Content-Security-Policy | ||
- Permissions-Policy | ||
- Referrer-Policy | ||
- Strict-Transport-Security | ||
- X-Frame-Options | ||
- X-Xss-Protection | ||
- X-Content-Type-Options | ||
|
||
Also the web application has strict Host Header to avoid SSRF and Host Header Injection. | ||
|
||
1. Security Headers Example | ||
|
||
``` | ||
christofherkost at L-EEM091 in ~/D/s/c/gin-examples | ||
↪ curl http://localhost:8080 -I | ||
HTTP/1.1 404 Not Found | ||
Content-Security-Policy: default-src 'self'; connect-src *; font-src *; script-src-elem * 'unsafe-inline'; img-src * data:; style-src * 'unsafe-inline'; | ||
Content-Type: text/plain | ||
Permissions-Policy: geolocation=(),midi=(),sync-xhr=(),microphone=(),camera=(),magnetometer=(),gyroscope=(),fullscreen=(self),payment=() | ||
Referrer-Policy: strict-origin | ||
Strict-Transport-Security: max-age=31536000; includeSubDomains; preload | ||
X-Content-Type-Options: nosniff | ||
X-Frame-Options: DENY | ||
X-Xss-Protection: 1; mode=block | ||
Date: Thu, 28 Mar 2024 11:38:05 GMT | ||
Content-Length: 18 | ||
``` | ||
|
||
2. Host Header Injection Example | ||
``` | ||
christofherkost at L-EEM091 in ~/D/s/c/gin-examples | ||
↪ curl http://localhost:8080 -I -H "Host:neti.ee" | ||
HTTP/1.1 400 Bad Request | ||
Content-Type: application/json; charset=utf-8 | ||
Date: Thu, 28 Mar 2024 11:38:23 GMT | ||
Content-Length: 31 | ||
``` |
This file contains 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,73 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/gin-contrib/static" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func NewRouter() *gin.Engine { | ||
// Set the router as the default one shipped with Gin | ||
router := gin.Default() | ||
expectedHost := "localhost:8080" | ||
|
||
// Setup Security Headers | ||
router.Use(func(c *gin.Context) { | ||
if c.Request.Host != expectedHost { | ||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid host header"}) | ||
return | ||
} | ||
c.Header("X-Frame-Options", "DENY") | ||
c.Header("Content-Security-Policy", "default-src 'self'; connect-src *; font-src *; script-src-elem * 'unsafe-inline'; img-src * data:; style-src * 'unsafe-inline';") | ||
c.Header("X-XSS-Protection", "1; mode=block") | ||
c.Header("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload") | ||
c.Header("Referrer-Policy", "strict-origin") | ||
c.Header("X-Content-Type-Options", "nosniff") | ||
c.Header("Permissions-Policy", "geolocation=(),midi=(),sync-xhr=(),microphone=(),camera=(),magnetometer=(),gyroscope=(),fullscreen=(self),payment=()") | ||
c.Next() | ||
}) | ||
|
||
// Serve frontend static files | ||
router.Use(static.Serve("/", static.LocalFile("./public/", true))) | ||
|
||
// Setup route group for the API | ||
api := router.Group("/api") | ||
{ | ||
apiHandler := func(c *gin.Context) { | ||
c.JSON(http.StatusOK, gin.H{ | ||
"message": "Uniform API", | ||
}) | ||
} | ||
api.GET("", apiHandler) | ||
api.GET("/", apiHandler) | ||
} | ||
|
||
return router | ||
} | ||
|
||
func main() { | ||
|
||
httpPort := os.Getenv("API_PORT") | ||
if httpPort == "" { | ||
httpPort = "8080" | ||
} | ||
|
||
// Initialize router | ||
router := NewRouter() | ||
|
||
// Create server with timeout | ||
srv := &http.Server{ | ||
Addr: ":" + httpPort, | ||
Handler: router, | ||
// set timeout due CWE-400 - Potential Slowloris Attack | ||
ReadHeaderTimeout: 5 * time.Second, | ||
} | ||
|
||
if err := srv.ListenAndServe(); err != nil { | ||
log.Printf("Failed to start server: %v", err) | ||
} | ||
} |