-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
15 changed files
with
207 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package middleware | ||
|
||
import "github.com/labstack/echo/v4" | ||
|
||
func CacheControl() echo.MiddlewareFunc { | ||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
c.Response().Header().Set("Cache-Control", "private, max-age=3600") // 1 hour in seconds | ||
|
||
return next(c) | ||
} | ||
} | ||
} |
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,27 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"regexp" | ||
|
||
"github.com/labstack/echo/v4" | ||
echoMiddleware "github.com/labstack/echo/v4/middleware" | ||
) | ||
|
||
func allowOrigin(origin string) (bool, error) { | ||
return regexp.MatchString(`^https:\/\/piccolo.pics$`, origin) | ||
} | ||
|
||
func Cors() echo.MiddlewareFunc { | ||
return echoMiddleware.CORSWithConfig(echoMiddleware.CORSConfig{ | ||
Skipper: Skipper, | ||
AllowOriginFunc: allowOrigin, | ||
AllowMethods: []string{ | ||
http.MethodGet, | ||
http.MethodPatch, | ||
http.MethodPut, | ||
http.MethodPost, | ||
http.MethodDelete, | ||
}, | ||
}) | ||
} |
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,16 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/labstack/echo/v4" | ||
echoMiddleware "github.com/labstack/echo/v4/middleware" | ||
) | ||
|
||
func Csrf() echo.MiddlewareFunc { | ||
return echoMiddleware.CSRFWithConfig(echoMiddleware.CSRFConfig{ | ||
Skipper: Skipper, | ||
TokenLookup: "form:csrf", | ||
CookieName: "_csrf", | ||
CookieSecure: true, | ||
CookieHTTPOnly: true, | ||
}) | ||
} |
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,22 @@ | ||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
echoMiddleware "github.com/labstack/echo/v4/middleware" | ||
) | ||
|
||
func HttpsRedirect() echo.MiddlewareFunc { | ||
return echoMiddleware.HTTPSRedirectWithConfig(echoMiddleware.RedirectConfig{ | ||
Skipper: Skipper, | ||
Code: http.StatusMovedPermanently, | ||
}) | ||
} | ||
|
||
func HttpsNonWWWRedirect() echo.MiddlewareFunc { | ||
return echoMiddleware.HTTPSNonWWWRedirectWithConfig(echoMiddleware.RedirectConfig{ | ||
Skipper: Skipper, | ||
Code: http.StatusMovedPermanently, | ||
}) | ||
} |
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,51 @@ | ||
package middleware | ||
|
||
import ( | ||
"crypto/rand" | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"github.com/labstack/echo/v4" | ||
echoMiddleware "github.com/labstack/echo/v4/middleware" | ||
) | ||
|
||
func generateNonce() string { | ||
nonce := make([]byte, 16) | ||
|
||
_, err := rand.Read(nonce) | ||
if err != nil { | ||
panic("failed to generate nonce") | ||
} | ||
|
||
return base64.StdEncoding.EncodeToString(nonce) | ||
} | ||
|
||
func Secure() echo.MiddlewareFunc { | ||
nonce := generateNonce() | ||
|
||
secureMiddlewareFunc := echoMiddleware.SecureWithConfig(echoMiddleware.SecureConfig{ | ||
Skipper: echoMiddleware.DefaultSkipper, | ||
XSSProtection: "1; mode=block", | ||
ContentTypeNosniff: "nosniff", | ||
ContentSecurityPolicy: fmt.Sprintf(` | ||
default-src 'none'; | ||
script-src 'self' 'nonce-%s' https://cdn.tailwindcss.com; | ||
connect-src 'self'; | ||
img-src 'self' https://f005.backblazeb2.com data:; | ||
style-src 'self' 'unsafe-inline'; | ||
font-src 'self'; | ||
frame-src 'none'; | ||
base-uri 'self'; | ||
form-action 'self';`, nonce), | ||
XFrameOptions: "SAMEORIGIN", | ||
HSTSPreloadEnabled: false, | ||
}) | ||
|
||
return func(next echo.HandlerFunc) echo.HandlerFunc { | ||
return func(c echo.Context) error { | ||
c.Set("nonce", nonce) | ||
|
||
return secureMiddlewareFunc(next)(c) | ||
} | ||
} | ||
} |
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,13 @@ | ||
package middleware | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
func Skipper(c echo.Context) bool { | ||
env := os.Getenv("ENV") | ||
|
||
return env == "local" | ||
} |
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,32 @@ | ||
package page | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/labstack/echo/v4" | ||
echoMiddleware "github.com/labstack/echo/v4/middleware" | ||
) | ||
|
||
type PageInfo struct { | ||
Nonce string | ||
CsrfToken string | ||
Title string | ||
} | ||
|
||
func NewPageInfo(c echo.Context, title string) PageInfo { | ||
env := os.Getenv("ENV") | ||
|
||
nonce := c.Get("nonce").(string) | ||
|
||
var csrfToken string | ||
|
||
if env != "local" { | ||
csrfToken = c.Get(echoMiddleware.DefaultCSRFConfig.ContextKey).(string) | ||
} | ||
|
||
return PageInfo{ | ||
Nonce: nonce, | ||
CsrfToken: csrfToken, | ||
Title: title, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,13 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>400 - Bad Request</title> | ||
</head> | ||
<body> | ||
<section style="margin: 10% 0; text-align: center;"> | ||
<h1>400 - Bad Request</h1> | ||
</section> | ||
</body> | ||
</html> |
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