fix(platform): guard unscoped raw field queries

This commit is contained in:
lingniu
2026-07-04 12:45:53 +08:00
parent 773338cce9
commit 34116142e6
3 changed files with 77 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package platform
import (
"context"
"errors"
"math"
"net/url"
"sort"
@@ -56,6 +57,23 @@ type Service struct {
runtime RuntimeInfo
}
type clientError struct {
Code string
Message string
}
func (e clientError) Error() string {
return e.Message
}
func asClientError(err error) (clientError, bool) {
var target clientError
if errors.As(err, &target) {
return target, true
}
return clientError{}, false
}
var canonicalVehicleProtocols = []string{"GB32960", "JT808", "YUTONG_MQTT"}
func completeProtocolStats(stats []ProtocolStat) []ProtocolStat {
@@ -737,6 +755,12 @@ func (s *Service) RawFrames(ctx context.Context, query RawFrameQuery) (Page[RawF
if query.Limit <= 0 || query.Limit > 500 {
query.Limit = 100
}
if query.IncludeFields && !rawFrameQueryHasNarrowScope(query) {
return Page[RawFrameRow]{}, clientError{
Code: "RAW_FRAME_SCOPE_REQUIRED",
Message: "RAW 解析字段查询需要提供车辆或时间范围",
}
}
if strings.TrimSpace(query.VIN) == "" {
query.VIN = query.Keyword
}
@@ -750,6 +774,14 @@ func (s *Service) RawFrames(ctx context.Context, query RawFrameQuery) (Page[RawF
return s.store.RawFrames(ctx, query)
}
func rawFrameQueryHasNarrowScope(query RawFrameQuery) bool {
return strings.TrimSpace(query.VIN) != "" ||
strings.TrimSpace(query.Keyword) != "" ||
strings.TrimSpace(query.DateFrom) != "" ||
strings.TrimSpace(query.DateTo) != "" ||
len(query.Fields) > 0
}
func (s *Service) MileageSummary(ctx context.Context, query url.Values) (MileageSummary, error) {
resolvedQuery, err := s.resolveVehicleQuery(ctx, query)
if err != nil {