Tighten opaque plugin content boundaries

This commit is contained in:
npc0-hue
2026-09-03 18:24:39 +08:00
parent 80cddbf19d
commit 14cbc63e61
31 changed files with 452 additions and 558 deletions
+31 -47
View File
@@ -17,7 +17,6 @@ type componentLogServerContextKey struct{}
const (
logEventHeartbeatInterval = 15 * time.Second
managedLogSessionIDPrefix = "log-session:"
)
// serverLogEvents streams platform-accepted live append events for the terminal drawer.
@@ -26,7 +25,7 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
writeMethodNotAllowed(w, http.MethodGet)
return
}
instance, streams, liveEligible, subscription, err := h.openLogEventSubscription(r)
instance, streams, subscription, err := h.openLogEventSubscription(r)
if err != nil {
writeServiceError(w, err)
return
@@ -46,12 +45,10 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
active := supervisedLogSession{}
if liveEligible {
if isComponentLogRequest(r) {
active = activeComponentLogSession(streams)
} else {
active = activeSupervisedLogSession(streams)
}
if isComponentLogRequest(r) {
active = activeComponentLogSession(streams)
} else {
active = activeSupervisedLogSession(streams)
}
emittedThrough, err := h.writeCurrentLogSession(w, instance.ID, active)
if err != nil {
@@ -82,7 +79,6 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
continue
}
if subscriptionEvent.ProcessState != domain.ServerInstanceStateRunning {
liveEligible = false
if active.sessionID == "" {
continue
}
@@ -94,14 +90,11 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
flusher.Flush()
continue
}
streams, liveEligible, err = h.loadLiveLogSnapshot(instance.ID, isComponentLogRequest(r))
streams, err = h.loadLiveLogSnapshot(instance.ID, isComponentLogRequest(r))
if err != nil {
return
}
next := supervisedLogSession{}
if liveEligible {
next = activeSupervisedLogSession(streams)
}
next := activeSupervisedLogSession(streams)
if sameSupervisedLogSession(active, next) {
continue
}
@@ -113,7 +106,7 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
flusher.Flush()
continue
}
if subscriptionEvent.Kind != service.LogEventSubscriptionEventLog || !liveEligible {
if subscriptionEvent.Kind != service.LogEventSubscriptionEventLog {
continue
}
event := subscriptionEvent.LogEvent
@@ -122,20 +115,16 @@ func (h *coreHandlers) serverLogEvents(w http.ResponseWriter, r *http.Request) {
candidate = supervisedLogSession{}
}
if candidate.sessionID != "" && newerLogSession(candidate, active) {
streams, liveEligible, err = h.loadLiveLogSnapshot(instance.ID, isComponentLogRequest(r))
if err != nil {
return
}
next := supervisedLogSession{}
if liveEligible {
next = activeSupervisedLogSession(streams)
}
next := candidate
if !sameSupervisedLogSession(active, next) {
active = next
emittedThrough, err = h.writeCurrentLogSession(w, instance.ID, active)
if err != nil {
return
}
if event.Entry.Seq > 0 && emittedThrough[event.Stream.ID] >= event.Entry.Seq {
emittedThrough[event.Stream.ID] = event.Entry.Seq - 1
}
flusher.Flush()
}
}
@@ -251,10 +240,9 @@ func (h *coreHandlers) writeCurrentLogSession(w http.ResponseWriter, serverInsta
return emittedThrough, nil
}
func (h *coreHandlers) openLogEventSubscription(r *http.Request) (domain.ServerInstance, []domain.LogStream, bool, service.LogEventSubscription, error) {
func (h *coreHandlers) openLogEventSubscription(r *http.Request) (domain.ServerInstance, []domain.LogStream, service.LogEventSubscription, error) {
var instance domain.ServerInstance
var streams []domain.LogStream
var liveEligible bool
var subscription service.LogEventSubscription
var err error
if serverInstanceID, ok := r.Context().Value(componentLogServerContextKey{}).(string); ok && strings.TrimSpace(serverInstanceID) != "" {
@@ -266,29 +254,29 @@ func (h *coreHandlers) openLogEventSubscription(r *http.Request) (domain.ServerI
sessionID := bearerToken(r)
instance, err = h.core.GetServerInstanceForSession(sessionID, r.PathValue("id"))
if err != nil {
return domain.ServerInstance{}, nil, false, subscription, err
return domain.ServerInstance{}, nil, subscription, err
}
subscription, err = h.core.SubscribeLogEventsForSession(sessionID, instance.ID)
if err != nil {
return domain.ServerInstance{}, nil, false, subscription, err
return domain.ServerInstance{}, nil, subscription, err
}
} else {
instance, err = h.core.GetServerInstance(r.PathValue("id"))
if err != nil {
return domain.ServerInstance{}, nil, false, subscription, err
return domain.ServerInstance{}, nil, subscription, err
}
subscription, err = h.core.SubscribeLogEvents(instance.ID)
if err != nil {
return domain.ServerInstance{}, nil, false, subscription, err
return domain.ServerInstance{}, nil, subscription, err
}
}
if err == nil {
streams, liveEligible, err = h.loadLiveLogSnapshot(instance.ID, isComponentLogRequest(r))
streams, err = h.loadLiveLogSnapshot(instance.ID, isComponentLogRequest(r))
}
if err != nil && subscription.Close != nil {
subscription.Close()
}
return instance, streams, liveEligible, subscription, err
return instance, streams, subscription, err
}
func withComponentLogServer(r *http.Request, serverInstanceID string) *http.Request {
@@ -300,46 +288,42 @@ func isComponentLogRequest(r *http.Request) bool {
return ok
}
func (h *coreHandlers) loadLiveLogSnapshot(serverInstanceID string, includeDeclaredStreams bool) ([]domain.LogStream, bool, error) {
func (h *coreHandlers) loadLiveLogSnapshot(serverInstanceID string, includeDeclaredStreams bool) ([]domain.LogStream, error) {
instance, err := h.core.GetServerInstance(serverInstanceID)
if err != nil {
return nil, false, err
return nil, err
}
if instance.State != domain.ServerInstanceStateRunning || strings.TrimSpace(instance.RunEndpointID) == "" {
return nil, false, nil
if strings.TrimSpace(instance.RunEndpointID) == "" {
return nil, nil
}
if !includeDeclaredStreams && instance.State != domain.ServerInstanceStateRunning {
return nil, nil
}
endpoint, err := h.core.GetRunEndpoint(instance.RunEndpointID)
if err != nil {
return nil, false, err
return nil, err
}
if endpoint.Status != domain.RunEndpointStatusOnline {
return nil, false, nil
}
logSessionID := strings.TrimPrefix(instance.LifecycleProcessID, managedLogSessionIDPrefix)
if logSessionID == instance.LifecycleProcessID || strings.TrimSpace(logSessionID) == "" {
return nil, false, nil
return nil, nil
}
streams, err := h.core.ListLogStreams(domain.LogStreamFilter{ServerInstanceID: instance.ID})
if err != nil {
return nil, false, err
return nil, err
}
current := make([]domain.LogStream, 0, len(streams))
for _, stream := range streams {
if includeDeclaredStreams {
if stream.LogSessionID != "" && stream.LogSessionID != logSessionID {
continue
}
if stream.Source != domain.LogStreamSourceProcess && stream.Source != domain.LogStreamSourceFile && stream.Source != domain.LogStreamSourceManagementProgram {
continue
}
current = append(current, stream)
continue
}
if stream.Source == domain.LogStreamSourceProcess && stream.LogSessionID == logSessionID {
if stream.Source == domain.LogStreamSourceProcess && strings.TrimSpace(stream.LogSessionID) != "" && !stream.SessionStartedAt.IsZero() {
current = append(current, stream)
}
}
return current, true, nil
return current, nil
}
func writeSSEJSON(w http.ResponseWriter, eventName string, id string, value any) error {