-
Notifications
You must be signed in to change notification settings - Fork 1
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
12 changed files
with
157 additions
and
38 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
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,43 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func secureHeaders(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Security-Policy", | ||
"default-src 'self'; style-src 'self' fonts.googleapis.com; font-src fonts.gstatic.com") | ||
w.Header().Set("Referrer-Policy", "origin-when-cross-origin") | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
w.Header().Set("X-Frame-Options", "deny") | ||
w.Header().Set("X-XSS-Protection", "0") | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func (app *application) logRequest(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
app.infoLog.Printf("%s - %s %s %s", r.RemoteAddr, r.Proto, r.Method, r.URL.RequestURI()) | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func (app *application) recoverPanic(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
// Create a deferred function (which will always be run in the event of | ||
// panic as Go unwinds the stack) | ||
defer func() { | ||
// Use the builtin recover function to check if there has been a | ||
// panic or not. If there has... | ||
if err := recover(); err != nil { | ||
// Set a "Connection: Close" header on the response. | ||
w.Header().Set("Connection", "close") | ||
// Call the app.serverError helper method to return a 500 Internal Server response | ||
app.serverError(w, fmt.Errorf("%s", err)) | ||
} | ||
}() | ||
next.ServeHTTP(w, r) | ||
}) | ||
} |
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,53 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"github.com/rk1165/pse/internal/assert" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestSecureHeaders(t *testing.T) { | ||
rr := httptest.NewRecorder() | ||
|
||
r, err := http.NewRequest(http.MethodGet, "/", nil) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Create a moc http handler that we can pass to our secureHeaders | ||
// middleware, which writes a 200 status code and an "OK" response body | ||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Write([]byte("OK")) | ||
}) | ||
|
||
// Pass the mock HTTP handler to our secureHeaders middleware. | ||
secureHeaders(next).ServeHTTP(rr, r) | ||
rs := rr.Result() | ||
|
||
expectedValue := "default-src 'self'; style-src 'self' fonts.googleapis.com; font-src fonts.gstatic.com" | ||
assert.Equal(t, rs.Header.Get("Content-Security-Policy"), expectedValue) | ||
|
||
expectedValue = "origin-when-cross-origin" | ||
assert.Equal(t, rs.Header.Get("Referrer-Policy"), expectedValue) | ||
|
||
expectedValue = "nosniff" | ||
assert.Equal(t, rs.Header.Get("X-Content-Type-Options"), expectedValue) | ||
|
||
expectedValue = "deny" | ||
assert.Equal(t, rs.Header.Get("X-Frame-Options"), expectedValue) | ||
|
||
expectedValue = "0" | ||
assert.Equal(t, rs.Header.Get("X-XSS-Protection"), expectedValue) | ||
|
||
assert.Equal(t, rs.StatusCode, http.StatusOK) | ||
defer rs.Body.Close() | ||
body, err := io.ReadAll(rs.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
bytes.TrimSpace(body) | ||
assert.Equal(t, string(body), "OK") | ||
} |
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
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,29 @@ | ||
package assert | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func Equal[T comparable](t *testing.T, actual, expected T) { | ||
t.Helper() | ||
if actual != expected { | ||
t.Errorf("got: %v; want: %v", actual, expected) | ||
} | ||
} | ||
|
||
func StringContains(t *testing.T, actual, expectedSubstring string) { | ||
t.Helper() | ||
|
||
if !strings.Contains(actual, expectedSubstring) { | ||
t.Errorf("got: %q; expected to contain: %q", actual, expectedSubstring) | ||
} | ||
} | ||
|
||
func NilError(t *testing.T, actual error) { | ||
t.Helper() | ||
|
||
if actual != nil { | ||
t.Errorf("got: %v; expected: nil", actual) | ||
} | ||
} |
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