[BUG] Standard Schema accepts values that don't match their field's type

Aperta
#218 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
51/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
typescript

Direzione di ricerca

Start by reading createStandardSchema and the code that constructs messages and runs buf.validate rules; the issue does not name specific files or tests. Reproduce the UInt32Value case with { value: 1.5 }, then trace how field types and paths are available during validation. Done when mismatched scalar values are reported at their field paths and valid results can be serialized.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Description

The Standard Schema from createStandardSchema returns values whose types don't match their fields as valid. Its input is unknown, which is typically form data, but create() doesn't check scalar types and the validator only evaluates the buf.validate rules. So a form with a number input for a uint32 field accepts 1.5 or -1, and the mistake only shows up later when the message fails to encode.

Steps to Reproduce

import { createStandardSchema } from "@bufbuild/protovalidate";
import { toBinary } from "@bufbuild/protobuf";
import {
  BoolValueSchema,
  Int32ValueSchema,
  StringValueSchema,
  UInt32ValueSchema,
} from "@bufbuild/protobuf/wkt";

const result = createStandardSchema(UInt32ValueSchema)["~standard"].validate({ value: 1.5 });
console.log(result); // { value: { $typeName: "google.protobuf.UInt32Value", value: 1.5 } }

toBinary(UInt32ValueSchema, result.value);
// Error: cannot encode field google.protobuf.UInt32Value.value to binary: invalid uint32: 1.5

Expected Behavior

A value that doesn't fit its field's type is reported as an issue at that field's path, as a rule violation would be, so the schema's output can always be serialized.

Actual Behavior

Every one of these is returned as valid:

Schema Input toBinary of the output
UInt32Value { value: 1.5 } throws invalid uint32: 1.5
UInt32Value { value: -1 } throws invalid uint32: -1
UInt32Value { value: 2 ** 32 } throws invalid uint32: 4294967296
Int32Value { value: 2 ** 31 } throws invalid int32: 2147483648
StringValue { value: 123 } encodes without an error
BoolValue { value: "yes" } encodes without an error

Rules on the field don't catch it either: a field declared as optional uint32 number = 2 [(buf.validate.field).required = true, (buf.validate.field).uint32.gte = 1, (buf.validate.field).uint32.lte = 1000000] accepts 1.5.

Environment

  • @bufbuild/protovalidate version: 1.3.0
  • @bufbuild/protobuf version: 2.17.0
  • @bufbuild/cel version: 0.6.1
  • @bufbuild/cel-spec version: 0.6.1
  • Node.js version: v26.13.0
  • Protobuf Compiler & Version: buf 1.75.0

Possible Solution

Before creating the message, createStandardSchema could check each field's value against the field's type and report any mismatch with that field's path: integers that aren't whole or fall outside a 32-bit range, and values of the wrong JS type for string, bool, bytes, 64-bit (bigint, or string/number with jstype) and enum fields. As far as I can tell, the input type MessageShape<Desc> / MessageInitShape<Desc> only describes the input statically. At runtime the input is unknown, so the schema is the only thing that can catch these.

Additional Context

We use the schema to validate forms against the same rules the server enforces. For now we work around this with our own Standard Schema that range-checks integer fields before calling validator.validate.

Lingua principale
TypeScript
Stelle
85
Fork
14
Merge medio
2m
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di bufbuild/protovalidate-es

Tutte le issue di bufbuild/protovalidate-es

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.