Fix session cleanup on error paths
Signed-off-by: Sean Du <do4suki@gmail.com>
This commit is contained in:
+12
-43
@@ -4,7 +4,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
@@ -48,16 +47,19 @@ func IsUnimplementedError(err error) bool {
|
||||
const maintenanceHeader = "Maintenance"
|
||||
|
||||
type ErrorContent struct {
|
||||
Detail string `json:"detail,omitempty"`
|
||||
Exception string `json:"exception,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Traceback []string `json:"traceback,omitempty"`
|
||||
Detail string `json:"detail,omitempty"`
|
||||
Exception string `json:"exception,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func IsMainPHPRequest(path string) bool {
|
||||
return path == "/main.php" || strings.HasPrefix(path, "/main.php/")
|
||||
}
|
||||
|
||||
func IsMainLoginEndpoint(path string) bool {
|
||||
return path == "/main.php/login/authkey" || path == "/main.php/login/login"
|
||||
}
|
||||
|
||||
func WriteMaintenanceJSON(setHeader func(string, string), writeJSON func(int, any), status int, content any) {
|
||||
setHeader("Content-Type", "application/json; charset=utf-8")
|
||||
setHeader("status_code", strconv.Itoa(status))
|
||||
@@ -80,46 +82,13 @@ func NewNotFoundContent(path string) ErrorContent {
|
||||
return NewDetailContent(fmt.Sprintf("Endpoint not found: %s", path))
|
||||
}
|
||||
|
||||
func NewInternalErrorContent(err error) ErrorContent {
|
||||
func NewInternalErrorContent() ErrorContent {
|
||||
return ErrorContent{
|
||||
Exception: exceptionName(err),
|
||||
Message: err.Error(),
|
||||
Exception: "InternalServerError",
|
||||
Message: http.StatusText(http.StatusInternalServerError),
|
||||
}
|
||||
}
|
||||
|
||||
func NewErrorContent(exception, message string) ErrorContent {
|
||||
return ErrorContent{
|
||||
Exception: exception,
|
||||
Message: message,
|
||||
}
|
||||
}
|
||||
|
||||
func NewPanicContent(recovered any, stack []byte) ErrorContent {
|
||||
return ErrorContent{
|
||||
Exception: "panic",
|
||||
Message: fmt.Sprint(recovered),
|
||||
Traceback: splitLines(stack),
|
||||
}
|
||||
}
|
||||
|
||||
func exceptionName(err error) string {
|
||||
if err == nil {
|
||||
return http.StatusText(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
typ := reflect.TypeOf(err)
|
||||
if typ.Kind() == reflect.Pointer {
|
||||
typ = typ.Elem()
|
||||
}
|
||||
if name := typ.Name(); name != "" {
|
||||
return name
|
||||
}
|
||||
return typ.String()
|
||||
}
|
||||
|
||||
func splitLines(stack []byte) []string {
|
||||
if len(stack) == 0 {
|
||||
return nil
|
||||
}
|
||||
return strings.Split(strings.TrimSpace(string(stack)), "\n")
|
||||
func NewPanicContent() ErrorContent {
|
||||
return NewInternalErrorContent()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package honokautils
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestInternalErrorContentIsSanitized(t *testing.T) {
|
||||
content := NewInternalErrorContent()
|
||||
|
||||
if content.Exception != "InternalServerError" {
|
||||
t.Fatalf("unexpected exception: %q", content.Exception)
|
||||
}
|
||||
if content.Message != http.StatusText(http.StatusInternalServerError) {
|
||||
t.Fatalf("unexpected message: %q", content.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPanicContentIsSanitized(t *testing.T) {
|
||||
content := NewPanicContent()
|
||||
|
||||
if content != NewInternalErrorContent() {
|
||||
t.Fatalf("panic content exposes a distinct internal detail: %#v", content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsMainLoginEndpoint(t *testing.T) {
|
||||
for _, path := range []string{"/main.php/login/authkey", "/main.php/login/login"} {
|
||||
if !IsMainLoginEndpoint(path) {
|
||||
t.Fatalf("expected %q to be a main.php login endpoint", path)
|
||||
}
|
||||
}
|
||||
|
||||
for _, path := range []string{"/login/authkey", "/main.php/user/userInfo"} {
|
||||
if IsMainLoginEndpoint(path) {
|
||||
t.Fatalf("expected %q not to be a main.php login endpoint", path)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user