|
|
@@ -10,6 +10,7 @@ import (
|
|
|
"log/slog"
|
|
|
"net"
|
|
|
"net/http"
|
|
|
+ "runtime/debug"
|
|
|
"sync"
|
|
|
"syscall"
|
|
|
"time"
|
|
|
@@ -18,6 +19,7 @@ import (
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
"golang.org/x/time/rate"
|
|
|
|
|
|
+ "github.com/mk6i/open-oscar-server/server/oscar/middleware"
|
|
|
"github.com/mk6i/open-oscar-server/wire"
|
|
|
)
|
|
|
|
|
|
@@ -30,14 +32,21 @@ var (
|
|
|
|
|
|
// errTOCProcessing indicates that an error occurred in the TOC handler
|
|
|
errTOCProcessing = errors.New("failed to process TOC request")
|
|
|
-)
|
|
|
-
|
|
|
-type contextKey string
|
|
|
|
|
|
-const (
|
|
|
- screenNameContextKey contextKey = "screenName"
|
|
|
+ // errPanic wraps a value recovered from a panic while handling a connection
|
|
|
+ errPanic = errors.New("recovered from panic")
|
|
|
)
|
|
|
|
|
|
+// catchPanic recovers an in-flight panic into *err, joined with any error
|
|
|
+// already there, so a panic while handling untrusted input drops the session
|
|
|
+// rather than the whole process. Defer it directly (defer catchPanic(&err));
|
|
|
+// wrapping it in another deferred closure stops recover from seeing the panic.
|
|
|
+func catchPanic(err *error) {
|
|
|
+ if r := recover(); r != nil {
|
|
|
+ *err = errors.Join(*err, fmt.Errorf("%w: %v\n%s", errPanic, r, debug.Stack()))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
// bufferedConn is a wrapper around net.Conn that allows peeking into the
|
|
|
// incoming connection without consuming data. It is useful for multiplexing
|
|
|
// TOC/HTTP and TOC/FLAP connections.
|
|
|
@@ -264,8 +273,13 @@ func (s *Server) acceptLoop(ctx context.Context, ln net.Listener, httpCh chan ne
|
|
|
}
|
|
|
|
|
|
go func() {
|
|
|
+ ctx := middleware.WithIP(ctx, conn.RemoteAddr().String())
|
|
|
if err := s.handleConnection(conn, ctx, httpCh); err != nil {
|
|
|
- s.logger.InfoContext(ctx, "user session failed", "err", err.Error())
|
|
|
+ if errors.Is(err, errPanic) {
|
|
|
+ s.logger.ErrorContext(ctx, "recovered from panic in connection handler", "err", err.Error())
|
|
|
+ } else {
|
|
|
+ s.logger.InfoContext(ctx, "user session failed", "err", err.Error())
|
|
|
+ }
|
|
|
}
|
|
|
}()
|
|
|
}
|
|
|
@@ -274,7 +288,9 @@ func (s *Server) acceptLoop(ctx context.Context, ln net.Listener, httpCh chan ne
|
|
|
// handleConnection inspects and routes an incoming connection. If the connection
|
|
|
// starts with "FLAP", handle as TOC/FLAP; otherwise, dispatch for HTTP
|
|
|
// processing.
|
|
|
-func (s *Server) handleConnection(conn net.Conn, ctx context.Context, httpCh chan net.Conn) error {
|
|
|
+func (s *Server) handleConnection(conn net.Conn, ctx context.Context, httpCh chan net.Conn) (err error) {
|
|
|
+ defer catchPanic(&err)
|
|
|
+
|
|
|
bufCon := newBufferedConn(conn)
|
|
|
|
|
|
doFlap := "FLAP"
|
|
|
@@ -342,8 +358,13 @@ func (s *Server) dispatchFLAP(ctx context.Context, conn net.Conn) error {
|
|
|
// the group runs every goroutine of the TOC session, starting with the
|
|
|
// BOS connection's reader during signon
|
|
|
g, ctx := errgroup.WithContext(ctx)
|
|
|
- goAsync := func(f func(ctx context.Context) error) {
|
|
|
- g.Go(func() error { return f(ctx) })
|
|
|
+ // launch f on g with a panic net, so a panic while handling untrusted input
|
|
|
+ // drops the session rather than the whole process
|
|
|
+ goAsync := func(f func(context.Context) error) {
|
|
|
+ g.Go(func() (err error) {
|
|
|
+ defer catchPanic(&err)
|
|
|
+ return f(ctx)
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
me, err := s.login(ctx, clientFlap, conn.RemoteAddr(), goAsync)
|
|
|
@@ -356,7 +377,7 @@ func (s *Server) dispatchFLAP(ctx context.Context, conn net.Conn) error {
|
|
|
return nil // signon failed
|
|
|
}
|
|
|
|
|
|
- ctx = context.WithValue(ctx, screenNameContextKey, me.IdentScreenName())
|
|
|
+ ctx = middleware.WithScreenName(ctx, me.IdentScreenName())
|
|
|
|
|
|
return s.handleTOCRequest(ctx, g, closeConn, me, clientFlap)
|
|
|
}
|
|
|
@@ -380,8 +401,13 @@ func (s *Server) handleTOCRequest(
|
|
|
msgCh := make(chan []string, 1)
|
|
|
|
|
|
me.toCh = msgCh
|
|
|
- me.goAsync = func(f func(ctx context.Context) error) {
|
|
|
- g.Go(func() error { return f(ctx) })
|
|
|
+ // launch f on g with a panic net, so a panic while handling untrusted input
|
|
|
+ // drops the session rather than the whole process
|
|
|
+ me.goAsync = func(f func(context.Context) error) {
|
|
|
+ g.Go(func() (err error) {
|
|
|
+ defer catchPanic(&err)
|
|
|
+ return f(ctx)
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
// process TOC client requests and enqueue TOC server responses
|