mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-11-22 11:58:48 +00:00
feat: Add CSRF protection middleware to carrier-connector with secret validation
- Add gin-csrf middleware import to main.go - Add CSRF_SECRET environment variable support with default fallback warning - Add CSRF middleware to Gin router with configurable secret - Add default CSRF secret with production warning for missing environment variable
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/repository"
|
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/repository"
|
||||||
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/webhook"
|
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/webhook"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
csrf "github.com/utrack/gin-csrf"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -35,6 +36,17 @@ func main() {
|
|||||||
port := handler.GetEnv("PORT", "8080")
|
port := handler.GetEnv("PORT", "8080")
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
|
// Add CSRF protection middleware
|
||||||
|
csrfSecret := os.Getenv("CSRF_SECRET")
|
||||||
|
if csrfSecret == "" {
|
||||||
|
csrfSecret = "change-me-in-production-csrf-secret-32-chars-min"
|
||||||
|
handler.Logger.Warn().Msg("Using default CSRF secret, set CSRF_SECRET environment variable")
|
||||||
|
}
|
||||||
|
router.Use(csrf.Middleware(csrf.Options{
|
||||||
|
Secret: csrfSecret,
|
||||||
|
}))
|
||||||
|
|
||||||
router.Use(gin.LoggerWithFormatter(func(p gin.LogFormatterParams) string {
|
router.Use(gin.LoggerWithFormatter(func(p gin.LogFormatterParams) string {
|
||||||
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
|
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
|
||||||
p.ClientIP, p.TimeStamp.Format(time.RFC1123),
|
p.ClientIP, p.TimeStamp.Format(time.RFC1123),
|
||||||
|
|||||||
Reference in New Issue
Block a user