修复本机核查长路径传输并记录竞态与静态检查进度
This commit is contained in:
1 parent
75b3b719cc
commit
6b77730114
12 files changed
+426
-11
No files matched your search
@@ -37,7 +37,7 @@ func Begin(ctx context.Context, conn net.Conn) (*Client, error) {
|
||||
client.stop = context.AfterFunc(ctx, func() { _ = conn.Close() })
|
||||
var response Response
|
||||
if err := readFrame(client.reader, &response); err != nil || !validChallenge(response) ||
|
||||
len(response.Methods) == 0 || len(response.Methods) > 8 || response.RecoveryPage != nil || response.Inspection != nil {
|
||||
len(response.Methods) == 0 || len(response.Methods) > 8 || response.RecoveryPage != nil || response.Inspection != nil || response.InspectionPart != nil {
|
||||
_ = client.Close()
|
||||
return nil, ErrProtocol
|
||||
}
|
||||
@@ -77,7 +77,7 @@ func (c *Client) exchange(request Request, stage int) error {
|
||||
_ = c.Close()
|
||||
return ErrProtocol
|
||||
}
|
||||
if response.RecoveryAvailable || response.RecoveryPage != nil || response.Inspection != nil {
|
||||
if response.RecoveryAvailable || response.RecoveryPage != nil || response.Inspection != nil || response.InspectionPart != nil {
|
||||
c.stage = -1
|
||||
_ = c.Close()
|
||||
return ErrProtocol
|
||||
|
||||
@@ -59,6 +59,7 @@ type Response struct {
|
||||
RecoveryAvailable bool `json:"recovery_available,omitempty"`
|
||||
RecoveryPage *deployment.PendingRecoveryPage `json:"recovery_page,omitempty"`
|
||||
Inspection *deployment.RecoveryInspection `json:"inspection,omitempty"`
|
||||
InspectionPart *inspectionPart `json:"inspection_part,omitempty"`
|
||||
}
|
||||
|
||||
// Session 由主服务创建,身份验证及密码持久化仍由主服务负责。
|
||||
|
||||
@@ -81,7 +81,7 @@ func (c *Client) recoveryExchange(request Request) (Response, error) {
|
||||
return fail()
|
||||
}
|
||||
if request.Action == "recovery_list" {
|
||||
if response.RecoveryPage == nil || response.Inspection != nil || len(response.RecoveryPage.Items) > deployment.RecoveryPageSize {
|
||||
if response.RecoveryPage == nil || response.Inspection != nil || response.InspectionPart != nil || len(response.RecoveryPage.Items) > deployment.RecoveryPageSize {
|
||||
return fail()
|
||||
}
|
||||
previous := request.Cursor
|
||||
@@ -95,9 +95,17 @@ func (c *Client) recoveryExchange(request Request) (Response, error) {
|
||||
(len(response.RecoveryPage.Items) != deployment.RecoveryPageSize || cursor != previous) {
|
||||
return fail()
|
||||
}
|
||||
} else if response.Inspection == nil || response.RecoveryPage != nil || response.Inspection.TransactionID != request.TransactionID ||
|
||||
len(response.Inspection.Files) != 9 || len(response.Inspection.Locks) != 3 || response.Inspection.ObservedAt.IsZero() {
|
||||
return fail()
|
||||
} else {
|
||||
if response.InspectionPart != nil {
|
||||
inspection, err := readInspectionParts(c.reader, response)
|
||||
if err != nil {
|
||||
return fail()
|
||||
}
|
||||
response.Inspection, response.InspectionPart = inspection, nil
|
||||
}
|
||||
if !validInspectionPayload(response.Inspection) || response.RecoveryPage != nil || response.Inspection.TransactionID != request.TransactionID {
|
||||
return fail()
|
||||
}
|
||||
}
|
||||
c.nonce, c.stage = response.Nonce, 3
|
||||
return response, nil
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
package localreset
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
)
|
||||
|
||||
const (
|
||||
inspectionPartSize = 8 << 10
|
||||
maxInspectionParts = 48
|
||||
maxInspectionBytes = inspectionPartSize * maxInspectionParts
|
||||
maxInspectionPathBytes = 4096 + len(".scert-backup-") + 160
|
||||
)
|
||||
|
||||
// 单帧仍限制为 16 KiB;总量覆盖十二条最长路径经 JSON 六倍转义后的大小。
|
||||
// 分片不增加业务请求次数,不重复采样;任何缺片、乱序或超限均不返回部分核查结果。
|
||||
type inspectionPart struct {
|
||||
Index int `json:"index"`
|
||||
Total int `json:"total"`
|
||||
Data []byte `json:"data"`
|
||||
}
|
||||
|
||||
func validInspectionPayload(value *deployment.RecoveryInspection) bool {
|
||||
if value == nil || !validRecoveryID(value.TransactionID) || len(value.Files) != 9 || len(value.Locks) != 3 || value.ObservedAt.IsZero() {
|
||||
return false
|
||||
}
|
||||
for _, file := range value.Files {
|
||||
if len(file.Path) > maxInspectionPathBytes {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for _, lock := range value.Locks {
|
||||
if len(lock.Path) > maxInspectionPathBytes {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func writeRecoveryResponse(ctx context.Context, writer io.Writer, response Response) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if response.Inspection == nil {
|
||||
return writeFrame(writer, response)
|
||||
}
|
||||
if !validInspectionPayload(response.Inspection) || response.InspectionPart != nil || response.RecoveryPage != nil {
|
||||
return ErrProtocol
|
||||
}
|
||||
encoded, err := json.Marshal(response)
|
||||
if err != nil {
|
||||
return ErrProtocol
|
||||
}
|
||||
if len(encoded)+1 <= MaxFrameSize {
|
||||
return writeFrame(writer, response)
|
||||
}
|
||||
data, err := json.Marshal(response.Inspection)
|
||||
if err != nil || len(data) > maxInspectionBytes {
|
||||
return ErrProtocol
|
||||
}
|
||||
total := (len(data) + inspectionPartSize - 1) / inspectionPartSize
|
||||
response.Inspection = nil
|
||||
for index := 0; index < total; index++ {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
start := index * inspectionPartSize
|
||||
end := min(start+inspectionPartSize, len(data))
|
||||
response.InspectionPart = &inspectionPart{Index: index, Total: total, Data: data[start:end]}
|
||||
if err := writeFrame(writer, response); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func readInspectionParts(reader *bufio.Reader, first Response) (*deployment.RecoveryInspection, error) {
|
||||
if first.InspectionPart == nil || first.InspectionPart.Total < 1 || first.InspectionPart.Total > maxInspectionParts {
|
||||
return nil, ErrProtocol
|
||||
}
|
||||
total := first.InspectionPart.Total
|
||||
data := make([]byte, 0, total*inspectionPartSize)
|
||||
response := first
|
||||
for index := 0; index < total; index++ {
|
||||
if index > 0 {
|
||||
response = Response{}
|
||||
if err := readFrame(reader, &response); err != nil {
|
||||
return nil, ErrProtocol
|
||||
}
|
||||
}
|
||||
part := response.InspectionPart
|
||||
if !validChallenge(response) || response.Nonce != first.Nonce || len(response.Methods) != 0 || response.RecoveryAvailable ||
|
||||
response.Inspection != nil || response.RecoveryPage != nil || part == nil || part.Index != index || part.Total != total ||
|
||||
len(part.Data) == 0 || len(part.Data) > inspectionPartSize || (index < total-1 && len(part.Data) != inspectionPartSize) {
|
||||
return nil, ErrProtocol
|
||||
}
|
||||
data = append(data, part.Data...)
|
||||
}
|
||||
var inspection deployment.RecoveryInspection
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.DisallowUnknownFields()
|
||||
if decoder.Decode(&inspection) != nil || !validInspectionPayload(&inspection) {
|
||||
return nil, ErrProtocol
|
||||
}
|
||||
var extra any
|
||||
if !errors.Is(decoder.Decode(&extra), io.EOF) {
|
||||
return nil, ErrProtocol
|
||||
}
|
||||
return &inspection, nil
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package localreset
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Scert/backend/internal/deployment"
|
||||
)
|
||||
|
||||
func longInspectionFixture() deployment.RecoveryInspection {
|
||||
value := deployment.RecoveryInspection{
|
||||
TransactionID: strings.Repeat("t", 160), ServerID: "server-1",
|
||||
Phase: "recovery_required", Status: "recovery_required", ObservedAt: time.Now().UTC(),
|
||||
}
|
||||
for _, name := range []string{"certificate", "chain", "key"} {
|
||||
path := "/" + strings.Repeat(strings.Repeat("<", 199)+"/", 20)
|
||||
path += strings.Repeat("&", 4096-len(path)-len(name)) + name
|
||||
for _, suffix := range []string{"", ".scert-tmp-" + value.TransactionID, ".scert-backup-" + value.TransactionID} {
|
||||
value.Files = append(value.Files, deployment.RecoveryFileObservation{
|
||||
Path: path + suffix, Kind: "target", Exists: true, Digest: strings.Repeat("a", 64), Comparison: "unproven",
|
||||
})
|
||||
}
|
||||
value.Locks = append(value.Locks, deployment.RecoveryLockObservation{Path: path + ".scert-lock", Exists: true, Ownership: "unproven"})
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func TestRecoveryInspectionLongPathsRoundTrip(t *testing.T) {
|
||||
inspection := longInspectionFixture()
|
||||
encoded, err := json.Marshal(inspection)
|
||||
if err != nil || len(encoded) <= 250<<10 || len(encoded) > maxInspectionBytes {
|
||||
t.Fatalf("fixture does not exercise escaped path bound: %d %v", len(encoded), err)
|
||||
}
|
||||
session := &recoveryTestSession{inspection: &inspection}
|
||||
_, conn, _ := recoveryConnection(t, session)
|
||||
client, err := Begin(t.Context(), conn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
before := client.nonce
|
||||
result, err := client.InspectRecovery(inspection.TransactionID)
|
||||
if err != nil || !reflect.DeepEqual(result, inspection) {
|
||||
t.Fatalf("long observation changed or failed: %v", err)
|
||||
}
|
||||
if client.nonce == before || session.inspects.Load() != 1 {
|
||||
t.Fatal("nonce unchanged or observation repeated")
|
||||
}
|
||||
if _, err := client.ListRecoveries(""); err != nil || session.lists.Load() != 1 {
|
||||
t.Fatalf("next request failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoveryInspectionFrameBounds(t *testing.T) {
|
||||
inspection := longInspectionFixture()
|
||||
response := Response{Version: Version, OK: true, Code: "CHALLENGE", Nonce: strings.Repeat("a", 64), Inspection: &inspection}
|
||||
var output bytes.Buffer
|
||||
if err := writeRecoveryResponse(t.Context(), &output, response); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lines := bytes.Split(bytes.TrimSuffix(output.Bytes(), []byte{'\n'}), []byte{'\n'})
|
||||
if len(lines) < 2 || len(lines) > maxInspectionParts {
|
||||
t.Fatalf("unexpected fragment count: %d", len(lines))
|
||||
}
|
||||
for index, line := range lines {
|
||||
var frame Response
|
||||
if len(line)+1 > MaxFrameSize || json.Unmarshal(line, &frame) != nil || frame.InspectionPart == nil ||
|
||||
frame.InspectionPart.Index != index || frame.InspectionPart.Total != len(lines) || frame.Nonce != response.Nonce {
|
||||
t.Fatal("frame exceeds limit or has invalid sequence")
|
||||
}
|
||||
}
|
||||
inspection.Status = strings.Repeat("x", maxInspectionBytes)
|
||||
output.Reset()
|
||||
if err := writeRecoveryResponse(t.Context(), &output, response); !errors.Is(err, ErrProtocol) || output.Len() != 0 {
|
||||
t.Fatal("oversized observation emitted partial frames")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRecoveryInspectionRejectsMalformedParts(t *testing.T) {
|
||||
for _, scenario := range []string{"first-index", "many-parts", "zero-total", "negative-total", "large-part", "short-part", "missing-last", "index", "total", "nonce", "mixed", "methods", "capability", "empty-part", "missing-part", "status", "unknown-field", "trailing-json", "partial-observation", "wrong-id"} {
|
||||
t.Run(scenario, func(t *testing.T) {
|
||||
serverConn, conn := net.Pipe()
|
||||
defer serverConn.Close()
|
||||
defer conn.Close()
|
||||
_ = serverConn.SetDeadline(time.Now().Add(2 * time.Second))
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
defer serverConn.Close()
|
||||
nonce := strings.Repeat("a", 64)
|
||||
if writeFrame(serverConn, Response{Version: Version, OK: true, Code: "CHALLENGE", Nonce: nonce,
|
||||
Methods: []Method{{ID: "email"}}, RecoveryAvailable: true}) != nil {
|
||||
return
|
||||
}
|
||||
var request Request
|
||||
if readFrame(bufio.NewReaderSize(serverConn, MaxFrameSize+1), &request) != nil {
|
||||
return
|
||||
}
|
||||
inspection := longInspectionFixture()
|
||||
if scenario == "partial-observation" {
|
||||
inspection.Files = inspection.Files[:8]
|
||||
} else if scenario == "wrong-id" {
|
||||
inspection.TransactionID = "other"
|
||||
}
|
||||
data, _ := json.Marshal(inspection)
|
||||
if scenario == "unknown-field" {
|
||||
data = append(data[:len(data)-1], []byte(`,"unexpected":true}`)...)
|
||||
} else if scenario == "trailing-json" {
|
||||
data = append(data, []byte(" {}")...)
|
||||
}
|
||||
total := (len(data) + inspectionPartSize - 1) / inspectionPartSize
|
||||
for index := 0; index < total; index++ {
|
||||
if scenario == "missing-last" && index == total-1 {
|
||||
return
|
||||
}
|
||||
part := &inspectionPart{Index: index, Total: total, Data: data[index*inspectionPartSize : min((index+1)*inspectionPartSize, len(data))]}
|
||||
response := Response{Version: Version, OK: true, Code: "CHALLENGE", Nonce: strings.Repeat("b", 64), InspectionPart: part}
|
||||
if index == 0 {
|
||||
switch scenario {
|
||||
case "first-index":
|
||||
part.Index = 1
|
||||
case "many-parts":
|
||||
part.Total = maxInspectionParts + 1
|
||||
case "zero-total":
|
||||
part.Total = 0
|
||||
case "negative-total":
|
||||
part.Total = -1
|
||||
case "large-part":
|
||||
part.Data = make([]byte, inspectionPartSize+1)
|
||||
case "short-part":
|
||||
part.Data = part.Data[:100]
|
||||
}
|
||||
}
|
||||
if index == 1 {
|
||||
switch scenario {
|
||||
case "index":
|
||||
part.Index = 0
|
||||
case "total":
|
||||
part.Total++
|
||||
case "nonce":
|
||||
response.Nonce = strings.Repeat("c", 64)
|
||||
case "mixed":
|
||||
response.RecoveryPage = &deployment.PendingRecoveryPage{}
|
||||
case "methods":
|
||||
response.Methods = []Method{{ID: "email"}}
|
||||
case "capability":
|
||||
response.RecoveryAvailable = true
|
||||
case "empty-part":
|
||||
part.Data = nil
|
||||
case "missing-part":
|
||||
response.InspectionPart = nil
|
||||
case "status":
|
||||
response.OK = false
|
||||
}
|
||||
}
|
||||
if writeFrame(serverConn, response) != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
client, err := Begin(t.Context(), conn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
result, err := client.InspectRecovery(strings.Repeat("t", 160))
|
||||
if err == nil || result.TransactionID != "" || len(result.Files) != 0 || client.stage != -1 {
|
||||
t.Fatal("invalid fragments returned partial observation or reusable connection")
|
||||
}
|
||||
<-done
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
type cancelInspectionWriter struct {
|
||||
output bytes.Buffer
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (w *cancelInspectionWriter) Write(data []byte) (int, error) {
|
||||
n, err := w.output.Write(data)
|
||||
w.cancel()
|
||||
return n, err
|
||||
}
|
||||
|
||||
func TestRecoveryInspectionCancellationStopsFragments(t *testing.T) {
|
||||
inspection := longInspectionFixture()
|
||||
ctx, cancel := context.WithCancel(t.Context())
|
||||
defer cancel()
|
||||
writer := &cancelInspectionWriter{cancel: cancel}
|
||||
err := writeRecoveryResponse(ctx, writer, Response{Version: Version, OK: true, Code: "CHALLENGE", Nonce: strings.Repeat("b", 64), Inspection: &inspection})
|
||||
if !errors.Is(err, context.Canceled) || bytes.Count(writer.output.Bytes(), []byte{'\n'}) != 1 {
|
||||
t.Fatalf("cancel did not stop after first frame: %v", err)
|
||||
}
|
||||
reader := bufio.NewReaderSize(&writer.output, MaxFrameSize+1)
|
||||
var first Response
|
||||
if readFrame(reader, &first) != nil {
|
||||
t.Fatal("first frame unreadable")
|
||||
}
|
||||
if result, err := readInspectionParts(reader, first); err == nil || result != nil {
|
||||
t.Fatal("canceled fragments accepted")
|
||||
}
|
||||
if err := writeRecoveryResponse(ctx, io.Discard, Response{Inspection: &inspection}); !errors.Is(err, context.Canceled) {
|
||||
t.Fatal("pre-canceled operation accepted")
|
||||
}
|
||||
}
|
||||
@@ -33,7 +33,7 @@ func (s *Server) handleRecovery(ctx context.Context, conn net.Conn, reader *bufi
|
||||
_ = writeFrame(conn, Response{Version: Version, Code: "REJECTED"})
|
||||
return
|
||||
}
|
||||
if writeFrame(conn, response) != nil {
|
||||
if writeRecoveryResponse(ctx, conn, response) != nil {
|
||||
return
|
||||
}
|
||||
request = Request{}
|
||||
|
||||
@@ -15,10 +15,11 @@ import (
|
||||
|
||||
type recoveryTestSession struct {
|
||||
testSession
|
||||
lists atomic.Int32
|
||||
inspects atomic.Int32
|
||||
fail bool
|
||||
oversized bool
|
||||
lists atomic.Int32
|
||||
inspects atomic.Int32
|
||||
fail bool
|
||||
oversized bool
|
||||
inspection *deployment.RecoveryInspection
|
||||
}
|
||||
|
||||
func (*recoveryTestSession) RecoveryAvailable() bool { return true }
|
||||
@@ -28,6 +29,9 @@ func (s *recoveryTestSession) ListRecoveries(context.Context, string) (deploymen
|
||||
}
|
||||
func (s *recoveryTestSession) InspectRecovery(_ context.Context, id string) (deployment.RecoveryInspection, error) {
|
||||
s.inspects.Add(1)
|
||||
if s.inspection != nil {
|
||||
return *s.inspection, nil
|
||||
}
|
||||
result := deployment.RecoveryInspection{TransactionID: id, ServerID: "s1", ObservedAt: time.Now(),
|
||||
Files: make([]deployment.RecoveryFileObservation, 9), Locks: make([]deployment.RecoveryLockObservation, 3)}
|
||||
if s.fail {
|
||||
|
||||
Reference in new issue
Block a user