package store import ( "bytes" "context" "crypto/sha256" "database/sql" "github.com/klauspost/compress/zstd" rvboxv1 "github.com/rvbox/rvbox/gen/go/rvbox/v1" "google.golang.org/protobuf/proto" ) // checkCommandPayloads verifies the inline compressed payload table and its // relationship to script descriptors. A valid SQLite page is not enough: a // short/changed zstd frame must make the owning scope dirty before dispatch. func checkCommandPayloads(ctx context.Context, database *sql.DB) error { rows, err := database.QueryContext(ctx, `SELECT issue_uuid, kind, raw_bytes, stored_bytes, compression, sha256, inline_data, segment_path FROM command_payloads`) if err != nil { return err } defer rows.Close() for rows.Next() { var issue, digest, stored []byte var kind string var rawBytes, storedBytes uint64 var compression uint32 var segmentPath sql.NullString if err := rows.Scan(&issue, &kind, &rawBytes, &storedBytes, &compression, &digest, &stored, &segmentPath); err != nil { return err } if len(issue) != 16 || kind != "script" || compression != 2 || rawBytes > maxStoredScriptBytes || storedBytes != uint64(len(stored)) || len(digest) != sha256.Size || segmentPath.Valid || len(stored) == 0 { return ErrCommittedRangeMissing } decoded, err := decodeCommandPayload(stored, rawBytes, maxStoredScriptBytes) if err != nil { return err } computed := sha256.Sum256(decoded) if !bytes.Equal(computed[:], digest) { return ErrCommittedRangeMissing } } if err := rows.Err(); err != nil { return err } // Join the descriptor and payload in one cursor. The store deliberately has // one SQLite connection; querying again while this cursor is open deadlocks. commands, err := database.QueryContext(ctx, `SELECT c.issue_uuid, c.execution_spec, c.execution_spec_raw_bytes, c.execution_spec_stored_bytes, c.execution_spec_compression, p.raw_bytes, p.compression, p.sha256, p.inline_data FROM commands c LEFT JOIN command_payloads p ON p.issue_uuid = c.issue_uuid AND p.kind = 'script'`) if err != nil { return err } defer commands.Close() for commands.Next() { var issue, encoded []byte var specRawBytes, specStoredBytes uint64 var specCompression uint32 var rawBytes, compression sql.NullInt64 var digest, stored []byte if err := commands.Scan(&issue, &encoded, &specRawBytes, &specStoredBytes, &specCompression, &rawBytes, &compression, &digest, &stored); err != nil { return err } if len(issue) != 16 || specStoredBytes != uint64(len(encoded)) { return ErrCommittedRangeMissing } switch specCompression { case 1: if specRawBytes != uint64(len(encoded)) || specRawBytes > maxStoredExecutionSpecBytes { return ErrCommittedRangeMissing } case 2: encoded, err = decompressCommandSpec(encoded, specRawBytes) if err != nil { return ErrCommittedRangeMissing } default: return ErrCommittedRangeMissing } var spec rvboxv1.ExecutionSpec if err := proto.Unmarshal(encoded, &spec); err != nil { // Some low-level store tests intentionally use opaque bytes. Such // records cannot own a script payload. Dispatch validation rejects // their execution spec before any user code runs. if rawBytes.Valid { return ErrCommittedRangeMissing } continue } descriptor := spec.GetScript() if descriptor == nil { if rawBytes.Valid { return ErrCommittedRangeMissing } continue } if !rawBytes.Valid || !compression.Valid || descriptor.GetSizeBytes() > maxStoredScriptBytes || len(descriptor.GetSha256()) != sha256.Size { return ErrCommittedRangeMissing } if uint64(rawBytes.Int64) != descriptor.GetSizeBytes() || compression.Int64 != 2 || len(digest) != sha256.Size || !bytes.Equal(digest, descriptor.GetSha256()) { return ErrCommittedRangeMissing } decoded, err := decodeCommandPayload(stored, uint64(rawBytes.Int64), maxStoredScriptBytes) computed := sha256.Sum256(decoded) if err != nil || !bytes.Equal(computed[:], descriptor.GetSha256()) { return ErrCommittedRangeMissing } } return commands.Err() } func decodeCommandPayload(stored []byte, expected, maximum uint64) ([]byte, error) { if expected > maximum { return nil, ErrCommittedRangeMissing } decoder, err := zstd.NewReader(nil, zstd.WithDecoderConcurrency(1), zstd.WithDecoderMaxMemory(maximum+1)) if err != nil { return nil, ErrCommittedRangeMissing } defer decoder.Close() decoded, err := decoder.DecodeAll(stored, nil) if err != nil || uint64(len(decoded)) != expected || uint64(len(decoded)) > maximum { return nil, ErrCommittedRangeMissing } return decoded, nil }