From bd53cbf168c097692d4872b5eed19086b6ed3d1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Z=20Sink=C3=B3?= Date: Sun, 8 Nov 2026 11:41:29 +0100 Subject: [PATCH] module: exact JSON equality (cic-canonical-json/1); Plan no longer calls different values a noop canonicalEqual re-encoded both sides through encoding/json, so every number became a float64: two different integers above 2^53 compared equal and Plan answered noop for a real change; a repeated object key was resolved to the last one; an invalid UTF-8 byte or a lone surrogate escape became U+FFFD. cic-canonical-json/1 is a strict parser and writer of its own: exact numbers (e, so 1, 1.0 and 1e0 are one value and 2^53+1 differs from 2^53), keys sorted by UTF-8 bytes, a repeated key or lone surrogate refused, only the quote, backslash and controls escaped, bounds on input size, nesting, digits and exponent. Its expected results are language-neutral vectors in testdata/canonical_json_vectors.json. Plan refuses desired.data and observed.data that are not canonicalizable (validation) and compares values by the canonical form. The noop reproduction, the vectors, the bounds and a deterministic mutation fuzz are tests. Plan digests (M4a) are separate. --- [signing-metadata] key = cic-my-sign-key signature = vault:v1:MEQCIHNLToZ7X+thijfXoqS2ALOfTHOeGvZVK7hCOj3Q0uX+AiBB1LDOLk9O4Ne22lgxHEWRyoZKIJNXFmYUfoj1gpN/gw== hash-algorithm = sha256 digest = 9q1QUR2K4a+XpIoRGaT2dmnSIHZppO5opzHywkMyEUM= [certificate] -----BEGIN CERTIFICATE----- MIICBjCCAaygAwIBAgIUSnRMR6RPnEbg296XWPOqq/u5PCwwCgYIKoZIzj0EAwIw QzELMAkGA1UEBhMCSFUxGTAXBgNVBAoMEENlbnRyYWxJbmZyYUNvcmUxGTAXBgNV BAMMEENJQyBEZXZlbG9wZXIgQ0EwHhcNMjYwMzIwMTMyMjU5WhcNMjYxMjMxMTMy MjU5WjBFMQswCQYDVQQGEwJIVTEZMBcGA1UECgwQQ2VudHJhbEluZnJhQ29yZTEb MBkGA1UEAwwSR2Fib3IgWm9sdGFuIFNpbmtvMFkwEwYHKoZIzj0CAQYIKoZIzj0D AQcDQgAEIG2CVmTfmLB9pLLclj7YmP2eedAjklpy4LGrU2ijoiy6Xqpuybv7OgJe i+ez31s65NEV8+X/ByeX1cstR988z6N8MHowCQYDVR0TBAIwADAdBgNVHQ4EFgQU yZN6AIX/TNnIJ9GwAa/NRN3ujHAwHwYDVR0jBBgwFoAUXn6CHYzPUqU4JVP8g+OS WeDYjhcwDgYDVR0PAQH/BAQDAgeAMB0GA1UdJQQWMBQGCCsGAQUFBwMCBggrBgEF BQcDBDAKBggqhkjOPQQDAgNIADBFAiEA+bFzXRoJ4PCQbhAAtpkcMjt0vNj5rEW0 lOMBGDNyaWkCIB1vmM7PcZzv/c9bIrxF5kqv6QXomouhByUfeNUTbpKW -----END CERTIFICATE----- --- MANIFEST.sha256 | 11 +- docs/design/README.md | 3 + docs/design/specs/canonical-json.md | 76 ++++ docs/design/specs/provider-abi.md | 9 + module/canonical_json.go | 419 ++++++++++++++++++++ module/canonical_json_test.go | 192 +++++++++ module/plan_noop_exactness_test.go | 96 +++++ module/provider.go | 32 +- module/testdata/canonical_json_vectors.json | 397 +++++++++++++++++++ 9 files changed, 1219 insertions(+), 16 deletions(-) create mode 100644 docs/design/specs/canonical-json.md create mode 100644 module/canonical_json.go create mode 100644 module/canonical_json_test.go create mode 100644 module/plan_noop_exactness_test.go create mode 100644 module/testdata/canonical_json_vectors.json diff --git a/MANIFEST.sha256 b/MANIFEST.sha256 index c103786..784c207 100644 --- a/MANIFEST.sha256 +++ b/MANIFEST.sha256 @@ -9,6 +9,7 @@ 1126b99a345f693bd4c98eedaa86397ad4251f34f89dc48079f33764af0804d8 docs/hu/makefile-cheatsheet.md 113f83b3ca0443799224e8f29396b90777539217771ecb6f1c932df1b5168389 LICENSE.meta.yaml 11a0fb2cf1ce82ed7df0fe05a6fceb8e20051c48c4e9c80925f34d68dace503d tools/schemalib/validator.py +1264e708b92df67154f5f50bf52d9e7c5b3edb586db583dc47fc10b59bc060ab module/canonical_json.go 134ea29c14da90cff091cb9bd5406522e182a07a86c6c4203e1436df8c26c4bc docs/en/concept/declarative_ecosystem_integration.meta.yaml 13b0b2a232d66df4fe26ec751b49b600fc63dbcde26b2bf17c675bfed78d30f8 module/testdata/vcn-v1/cases/partial-read/expected/observed.registry.json 15551f514d13c6d42283a2cf09355f900f862624a51f3e32b6675ef3a24fce4a tools/oci-extract/go.mod @@ -61,16 +62,16 @@ 41ab08a0eb4f0739f352e60b35512c5402644f16fc349552cd185c490cb4d5a3 module/contracts_test.go 439445a5ad606bc5cd95a94cb4fba54f3f4dca17ed402d28c45c355cb235f301 module/module_loadtest_test.go 441f681287c170544f90844b29e4a103f89e91ac23a6cc8e974635d8bcde5a2b docs/en/architecture.md -456ea91a25295d5fd5c05b614e467e571f7611760ce1e4ff1222a1b5c00af583 module/provider.go 46120946bf939cfeae2c4806a39aef398f904f89b61c670ee5dd0d3c769138c2 tools/oci-extract/resolve_test.go 49ea29dcc165db8a6b406e82492500fd4761a7121c2420116e31f8e58f865dec tools/oci-extract/resolve.go 4ac6d59c5e3cd88c9dde821511c970827ae56e40a89a308a210cd985ecfdc41e CLAUDE.md 4d7369474566105752ec28aefab51de748b237342c71d05b3189fa8c2ae1fdef Makefile 4fffcdac095c0623fefb8515ab80b3e9629c0febc454456cd08b3cdf3c941720 LICENSE.md -511b61d9cf52d8f761a5dc2991ec25ed973632bc0d1f1a0607166e68e3d84552 docs/design/specs/provider-abi.md 526ab17415fc9a42967d35159db9438c3e696bf5a246e57a9f68c2dd0e596837 module/testdata/vcn-v1/cases/drift/expected/observed.oci.json 53bc2df597bc242dd2f8d2176b3a68e3d0681079132431c8e9900e14d34314c7 module/provider_test.go 5432506f15d65fa59572362a70db4bc403c13b163ec961ecb2951d7f98d093d0 module/go.mod +546e7c533da4b86b8817ee426865df0d11e24d2aa1580647634345f452a5017e module/canonical_json_test.go +56934c1ce7b4de656087d0b2ddb9e9dcbf56dd0c541d9e279c19c7535e84445a module/provider.go 57980b8a84a13ffad62b7295e3095bd635f8894716665d0654dfb54eebbf10e5 docs/contracts/en/host-expectations.md 5c650f84f688d4d965721df74779228bb6fdfa47d5c08c46e8274e6d97b212c8 module/testdata/vcn-v1/lifecycle-map.json 5ca6da66b9de8a04ca71f1c18ba2026ad02b8c1c70c700a432058da267e6b27d module/correspondence/vcn.json @@ -89,6 +90,7 @@ 6f3655bf787efee284225bcb6096913d0ec556771dddb42845311e649c21e7ad docs/hu/concept/declarative_ecosystem_integration.md 7068014ecee4111cb4456d3d3328c33b53a79c7803d68f040036f34449423f65 tools/oci-extract/testdata/instance.go 7080b3423beded8b64891afdcf2e6f6d8bec6ebeaa88aecbb1b06e149b54ef86 module/module.wasm +70d185e08cc84d9c493f925bd3e4153c6a69078b7a1b560d290031e4ad602933 module/plan_noop_exactness_test.go 7136d8366bbd7c5ccc19a3d810a7f55c31541acec9b0f05bd84e45a39107ded7 tests/test_capability_manifest.py 7426752feb1140b2023192728df35248e4f802617c150bb0523ab97dc1638c0b tools/init-hooks.sh 74d93981f1935d21001ed0475ebb84132ab67c71840ca82ca0cbfac5aca29ea2 docs/contracts/en/wasm-abi.md @@ -104,6 +106,7 @@ 7efd45c44d808639d5fe607cf4182e930be6b8d82709e716114e63512f69b55f module/testdata/vcn-v1/cases/conformant/expected/verdict.fields.json 805c05942247d6327f0f006a8799119ace530c4ae288ad637d0f8faddca21d86 docs/hu/architecture.md 806851c10862d6900eb858a229e3ba7138943abe3fd75e5b1e8f8ca4b57d1d6f mk/golang.mk +81eab1d1dbbeffa23d53c33e39f0bf6977348178640c1032827651d27180ffa9 docs/design/specs/canonical-json.md 836f908676723485f9648e69e184c49e51304febd4bcdc500c54038522a6092d module/observe_derivation_test.go 8400c953622cd5ba5414898bbc85e51494eaa3dae6ac9051ab4ac67c4a6e3384 module/testdata/vcn-v1/cases/conformant/expected/evidence.claims.json 8400c953622cd5ba5414898bbc85e51494eaa3dae6ac9051ab4ac67c4a6e3384 module/testdata/vcn-v1/cases/desired-absent/expected/evidence.claims.json @@ -129,9 +132,9 @@ 976bee7ffb9a4a599d5aee841d941ea7d12fecb6fdbb05d44238754b67148cb1 module/envelope_test.go 97f3f81e90d839c74efbe7ad2e1732b08ac0612a38499d6ef5dedcc637c64eb6 module/testdata/vcn-v1/cases/malformed-cidr/oci/response.json 9b18a2109d250d26d57d3cec5ed9c67bd803d34e9592ab98fb1826a5dc457a8f module/cicflow_mock_test.go +9b6f006a0e21f7baaad8509b3323e4c7e5bc4d16ab8cc165a70af855f78da460 module/testdata/canonical_json_vectors.json 9ba2ca374fd1900e82a2b6a9c27233e1603dce89b1908592b2a6cc93313be84c docs/hu/wasm-module-authoring.md 9c11fd643268e578437b015dc5a4ea6e5a3f290f0303ac04ea4d429768208617 tools/schemalib/__init__.py -9d31cb8835fcbd2d1d68aaa21609ebf1668bb5182d78aa8087e86b8f3bee65a4 docs/design/README.md 9de6268f09ede92c5adcb82ceec4ecd66761ade6e6aa79131cfeb8cc9814f44d tools/wasm_assert_tests_passed.sh a03b3fccfc1f8eb7f76833fdff8a50ea84c80c039db87bc0e41109e96f64420b docs/en/concept/declarative_ecosystem_integration.md a1cba33d61cd932030481da78da553eb0f31b536a7c3879d5bdd9992c69d189a module/testdata/vcn-v1/cases/not-found/expected/observed.oci.json @@ -157,6 +160,7 @@ b8e7c23cfe46a0daf7d3441b15657e9d4b35c4bf50f9421c2841dfbf19cacc82 docs/en/archit b9325f58a95216b240f3715a773e34fe8cdfa4d5fe48a5e7b32002a40be5cc2e tests/test_tools/test_finalize_release.py bb8a1743ab9f77a309bd851b4a1660f6f2420900455a5a0dfb83f4e6905968ff module/abi_manifest_test.go bbe93c718d90a03de5aab2999031c7097bd63a8e40ec14589c8dbe4364a4ec52 renovate.json +bcb406d5d10094dafb465d317935b4b8656e895a3331278765065669c8681fcf docs/design/specs/provider-abi.md bef22a55d1559676a57b4a05b48efc7cd226d3d733a1c932e183666679046b53 module/testdata/vcn-v1/cases/drift/expected/evidence.claims.json bfccbd3cd778ac6f0458b7d45d7b25e2cbdc0a789f40e81c1de476f8aab44363 tools/schemalib/loader.py c31a1313ace9ae3da9483d7ac7bbc0938d5e2f6c7c0460d785d94db5bb931e85 docs/contracts/en/release-artifact.md @@ -187,6 +191,7 @@ d5daed86d55d8b22d307e8b4df9ab5af7cfa9c2647750d28c5344daeb40f92f8 module/testdat d73ece9aa8ca3e7e76655705dd0c074869a83fe66a8745212f95a6a4db8916cf tests/test_oci_sdk_lock.py d75063538a29c37caad5e4f1ba680c81bc4827ee935e78281ab168b72d5e53a2 tools/check_doc_links.py d8e2e8a0c3377de19d2e1f04a7bf7f4dacce1a3bcc25ac67884ac2dab4be1bdc docker-compose.yml +da89091049f1e5ad288de12df086c679803c8e56a86bf42f569256437e672571 docs/design/README.md dbe54644234ddff2e00a48a38e44b2b21fa4eeb3142f17872614ea656deb5836 tools/oci-extract/testdata/vcn_client.go dbfb640d40533eeb73ececd170189f951f16ad07c549a8321dbb12251e78d067 features/feature-001/spec.md dc0c98d1e3ca6fac0ef40227c6f8c026397180059c95188d9adb4567cef2e11d tests/test_tools/test_infra.py diff --git a/docs/design/README.md b/docs/design/README.md index 59bee3c..fe09dca 100644 --- a/docs/design/README.md +++ b/docs/design/README.md @@ -26,6 +26,9 @@ namespace it opens. Written 2026-07-18 from the design session recorded in extraction. - [specs/state-model.md](specs/state-model.md) — intent/state object-level correspondence and the `effective_config` projection. +- [specs/canonical-json.md](specs/canonical-json.md) — `cic-canonical-json/1`: the exact JSON + value-equality and canonical form `plan` decides on (and a later plan digest will be + computed over). ## Direction diff --git a/docs/design/specs/canonical-json.md b/docs/design/specs/canonical-json.md new file mode 100644 index 0000000..0aef6a0 --- /dev/null +++ b/docs/design/specs/canonical-json.md @@ -0,0 +1,76 @@ +# cic-canonical-json/1 + +The one definition this module uses for "these two JSON texts are the same value". +It decides whether a plan has any work to do (`plan`), and is the form a later digest +of a plan will be computed over. Go implements it (`module/canonical_json.go`); the +**normative expectations are the language-neutral vectors** in +`module/testdata/canonical_json_vectors.json`, so another implementation can be +checked against the same file. + +Status: `plan` uses it today (**implemented**). Digests over it (`plan_digest`, +`target_digest`, `execution_digest`) are **concept** (not yet built). + +## Why not the standard library + +`encoding/json` reads every number as a `float64`, so two different integers above +2^53 compare equal (and `0.1` equals `0.10000000000000001`); it keeps only the last of +a repeated object key; and it turns an invalid UTF-8 byte or a lone surrogate escape +into U+FFFD, so distinct texts can collapse into one. `plan` used that comparison to +call two different values a `noop`. + +## The form + +The input is one strict JSON text (RFC 8259). The output is deterministic: + +- **Whitespace** is dropped (only space, tab, CR and LF are whitespace on input). +- **Objects**: members sorted by the **decoded key's UTF-8 bytes**; the keys are + compared after escapes are decoded (`"a"` is `"a"`). A **repeated key is an + error**, never merged. +- **Arrays** keep their order. +- **Numbers** are exact, with no floating point: the value is written as + `e`, where `` are the significant digits (no leading and + no trailing zeros) and the exponent is the power of ten that scales them, with a + `-` in front for a negative value. `1`, `1.0`, `1e0` and `10e-1` are all `1e0`; + `100` and `1e2` are `1e2`; `0.5` is `5e-1`; zero in any spelling (`-0`, `0.0`, + `0e5`) is `0`. `9007199254740993` stays `9007199254740993e0` and differs from + `9007199254740992e0`. +- **Strings** are decoded (all escapes, surrogate pairs combined) and written with + only the quote, the backslash and the characters below U+0020 escaped, the latter + as `\u00xx` in lower case. Every other character is written as it is, with no HTML + escaping. A lone surrogate escape is an error. +- Literals `true`, `false`, `null` as written. + +## What is refused (an error, never a repair) + +Not valid UTF-8 (a byte order mark included); a value followed by anything but +whitespace; a leading zero, a `+`, a bare `.5` or `1.`, `NaN` or `Infinity`; trailing +commas, comments, single quotes, unquoted keys; a raw control character in a string; +a bad or short escape; a lone or mismatched surrogate escape; a repeated object key; +empty or whitespace-only input. + +## Bounds + +An input beyond a bound is refused, not truncated: + +| Bound | Limit | +|---|---| +| input text | 1 MiB (2^20 bytes) | +| nesting of arrays and objects | 64 | +| digits in a number's integer and fraction parts | 1000 | +| digits in a number's written exponent | 6 | +| exponent after normalization | -10000 to 10000 | + +An error names the byte offset and the reason, never the offending text. + +## Where it applies today + +`plan` refuses `desired.data` and `observed.data` that are not canonicalizable +(`validation`, `field_path: desired.data` / `observed.data`), and compares values +(whole payloads and field by field) by their canonical form. `validate` and the other +operations are unchanged. + +## What this is not + +Not JCS (RFC 8785): that requires ES6 floating-point number formatting, which is +exactly the loss this form exists to avoid. Not a signature or an approval: two texts +with the same canonical form are the same JSON value, and nothing more. diff --git a/docs/design/specs/provider-abi.md b/docs/design/specs/provider-abi.md index 18e753c..0f6afa9 100644 --- a/docs/design/specs/provider-abi.md +++ b/docs/design/specs/provider-abi.md @@ -151,6 +151,15 @@ execution-result: # as implemented no id should end as `accepted` with the id still pending, and what `poll` would have to return to deliver the created resource's identifier. +### What `plan` requires of its inputs + +`desired.data` and `observed.data` must be strict, bounded JSON with exact numbers and +no repeated object key, as defined in `canonical-json.md` (cic-canonical-json/1); +otherwise `plan` answers `validation` with `field_path: desired.data` or +`observed.data`. Values are compared by their canonical form, so key order, whitespace, +escape spelling and number spelling (`1`, `1.0`, `1e0`) do not make a difference, while +different values always do (two integers above 2^53 are never equal). + ### What `execute` accepts before it sends anything Two independent checks run over the **whole** plan before the first step is signed diff --git a/module/canonical_json.go b/module/canonical_json.go new file mode 100644 index 0000000..aeac47c --- /dev/null +++ b/module/canonical_json.go @@ -0,0 +1,419 @@ +package main + +// canonical_json.go implements cic-canonical-json/1, the one definition of "these two +// JSON texts are the same value" used by this module. The rules, limits and the +// reasons for them are in docs/design/specs/canonical-json.md; the expected results +// are in testdata/canonical_json_vectors.json. Go is one implementation of that +// specification, not the definition of it. +// +// Why not encoding/json: it reads every number as a float64 (two different integers +// above 2^53 compare equal), keeps only the last of a duplicated key, and turns an +// invalid UTF-8 byte or a lone surrogate escape into U+FFFD, so distinct texts could +// collapse into one. A decision about whether a plan has any work to do, or whether +// two configurations are the same, must not rest on that. + +import ( + "errors" + "fmt" + "sort" + "strconv" + "unicode/utf8" +) + +// Bounds. They keep the canonical form cheap and the recursion shallow whatever a +// caller sends; an input beyond them is refused, never truncated. +const ( + canonMaxInput = 1 << 20 // bytes of input text + canonMaxDepth = 64 // nesting of arrays and objects + canonMaxDigits = 1000 // digits in a number's integer and fraction parts + canonMaxExpLen = 6 // digits in a number's written exponent + canonMaxExp = 10000 // |exponent| of the normalized number +) + +type canonMember struct { + key string // decoded key, UTF-8 + val []byte // canonical form of the value +} + +type canonParser struct { + src []byte + pos int +} + +// canonicalJSON returns the cic-canonical-json/1 form of one JSON text, or an error +// when the text is not strict JSON (RFC 8259) or exceeds a bound. Errors carry the +// byte offset and a reason, never the offending text. +func canonicalJSON(raw []byte) ([]byte, error) { + if len(raw) > canonMaxInput { + return nil, fmt.Errorf("json text is %d bytes, over the %d byte limit", len(raw), canonMaxInput) + } + if !utf8.Valid(raw) { + return nil, errors.New("json text is not valid UTF-8") + } + p := &canonParser{src: raw} + p.skipSpace() + out, err := p.value(0) + if err != nil { + return nil, err + } + p.skipSpace() + if p.pos != len(p.src) { + return nil, p.fail("unexpected data after the value") + } + return out, nil +} + +func (p *canonParser) fail(why string) error { + return fmt.Errorf("json: %s at byte %d", why, p.pos) +} + +func (p *canonParser) skipSpace() { + for p.pos < len(p.src) { + switch p.src[p.pos] { + case ' ', '\t', '\n', '\r': + p.pos++ + default: + return + } + } +} + +func (p *canonParser) value(depth int) ([]byte, error) { + if p.pos >= len(p.src) { + return nil, p.fail("a value is missing") + } + switch c := p.src[p.pos]; { + case c == '{': + return p.object(depth + 1) + case c == '[': + return p.array(depth + 1) + case c == '"': + s, err := p.str() + if err != nil { + return nil, err + } + return canonString(nil, s), nil + case c == 't': + return p.literal("true") + case c == 'f': + return p.literal("false") + case c == 'n': + return p.literal("null") + case c == '-' || (c >= '0' && c <= '9'): + return p.number() + } + return nil, p.fail("an unexpected character") +} + +func (p *canonParser) literal(word string) ([]byte, error) { + if len(p.src)-p.pos < len(word) || string(p.src[p.pos:p.pos+len(word)]) != word { + return nil, p.fail("an invalid literal") + } + p.pos += len(word) + return []byte(word), nil +} + +func (p *canonParser) array(depth int) ([]byte, error) { + if depth > canonMaxDepth { + return nil, p.fail("nesting is deeper than the limit") + } + p.pos++ // [ + out := []byte{'['} + p.skipSpace() + if p.pos < len(p.src) && p.src[p.pos] == ']' { + p.pos++ + return append(out, ']'), nil + } + for first := true; ; first = false { + if !first { + out = append(out, ',') + } + p.skipSpace() + v, err := p.value(depth) + if err != nil { + return nil, err + } + out = append(out, v...) + p.skipSpace() + if p.pos >= len(p.src) { + return nil, p.fail("an array is not closed") + } + switch p.src[p.pos] { + case ',': + p.pos++ + case ']': + p.pos++ + return append(out, ']'), nil + default: + return nil, p.fail("expected , or ] in an array") + } + } +} + +func (p *canonParser) object(depth int) ([]byte, error) { + if depth > canonMaxDepth { + return nil, p.fail("nesting is deeper than the limit") + } + p.pos++ // { + var members []canonMember + p.skipSpace() + if p.pos < len(p.src) && p.src[p.pos] == '}' { + p.pos++ + return []byte("{}"), nil + } + for { + p.skipSpace() + if p.pos >= len(p.src) || p.src[p.pos] != '"' { + return nil, p.fail("an object key must be a string") + } + key, err := p.str() + if err != nil { + return nil, err + } + p.skipSpace() + if p.pos >= len(p.src) || p.src[p.pos] != ':' { + return nil, p.fail("expected : after an object key") + } + p.pos++ + p.skipSpace() + v, err := p.value(depth) + if err != nil { + return nil, err + } + members = append(members, canonMember{key: string(key), val: v}) + p.skipSpace() + if p.pos >= len(p.src) { + return nil, p.fail("an object is not closed") + } + if p.src[p.pos] == ',' { + p.pos++ + continue + } + if p.src[p.pos] != '}' { + return nil, p.fail("expected , or } in an object") + } + p.pos++ + break + } + // Byte order of the decoded keys; a repeated key is refused, never merged. + sort.Slice(members, func(i, j int) bool { return members[i].key < members[j].key }) + for i := 1; i < len(members); i++ { + if members[i].key == members[i-1].key { + return nil, errors.New("json: an object has a duplicate key") + } + } + out := []byte{'{'} + for i, m := range members { + if i > 0 { + out = append(out, ',') + } + out = canonString(out, []byte(m.key)) + out = append(out, ':') + out = append(out, m.val...) + } + return append(out, '}'), nil +} + +// str reads a JSON string at p.pos (on the opening quote) and returns it decoded. +func (p *canonParser) str() ([]byte, error) { + p.pos++ // opening quote + var out []byte + for { + if p.pos >= len(p.src) { + return nil, p.fail("a string is not closed") + } + c := p.src[p.pos] + switch { + case c == '"': + p.pos++ + return out, nil + case c < 0x20: + return nil, p.fail("a raw control character in a string") + case c != '\\': + out = append(out, c) + p.pos++ + continue + } + p.pos++ // backslash + if p.pos >= len(p.src) { + return nil, p.fail("a string is not closed") + } + e := p.src[p.pos] + p.pos++ + switch e { + case '"', '\\', '/': + out = append(out, e) + case 'b': + out = append(out, '\b') + case 'f': + out = append(out, '\f') + case 'n': + out = append(out, '\n') + case 'r': + out = append(out, '\r') + case 't': + out = append(out, '\t') + case 'u': + r, err := p.hex4() + if err != nil { + return nil, err + } + switch { + case r >= 0xD800 && r <= 0xDBFF: + if p.pos+1 >= len(p.src) || p.src[p.pos] != '\\' || p.src[p.pos+1] != 'u' { + return nil, p.fail("a high surrogate escape without a low surrogate") + } + p.pos += 2 + lo, err := p.hex4() + if err != nil { + return nil, err + } + if lo < 0xDC00 || lo > 0xDFFF { + return nil, p.fail("a high surrogate escape without a low surrogate") + } + r = 0x10000 + (r-0xD800)<<10 + (lo - 0xDC00) + case r >= 0xDC00 && r <= 0xDFFF: + return nil, p.fail("a lone low surrogate escape") + } + var buf [4]byte + n := utf8.EncodeRune(buf[:], r) + out = append(out, buf[:n]...) + default: + return nil, p.fail("an invalid escape in a string") + } + } +} + +func (p *canonParser) hex4() (rune, error) { + if p.pos+4 > len(p.src) { + return 0, p.fail("a short \\u escape") + } + var r rune + for i := 0; i < 4; i++ { + c := p.src[p.pos+i] + var d rune + switch { + case c >= '0' && c <= '9': + d = rune(c - '0') + case c >= 'a' && c <= 'f': + d = rune(c-'a') + 10 + case c >= 'A' && c <= 'F': + d = rune(c-'A') + 10 + default: + return 0, p.fail("a bad \\u escape") + } + r = r<<4 | d + } + p.pos += 4 + return r, nil +} + +// canonString appends s (UTF-8) as a JSON string: only the quote, the backslash and +// the characters below 0x20 are escaped (the latter as \u00xx in lower case); every +// other character is written as it is, with no HTML escaping. +func canonString(out []byte, s []byte) []byte { + const hexdigits = "0123456789abcdef" + out = append(out, '"') + for _, c := range s { + switch { + case c == '"': + out = append(out, '\\', '"') + case c == '\\': + out = append(out, '\\', '\\') + case c < 0x20: + out = append(out, '\\', 'u', '0', '0', hexdigits[c>>4], hexdigits[c&0xf]) + default: + out = append(out, c) + } + } + return append(out, '"') +} + +// number reads a JSON number (RFC 8259 grammar) and returns its exact value as +// e, with no float64 involved: the significant digits without +// leading or trailing zeros, and the power of ten that scales them. Zero, whatever +// its spelling (-0, 0.0, 0e5), is "0". +func (p *canonParser) number() ([]byte, error) { + neg := false + if p.src[p.pos] == '-' { + neg = true + p.pos++ + } + start := p.pos + if p.pos >= len(p.src) { + return nil, p.fail("a number without digits") + } + switch c := p.src[p.pos]; { + case c == '0': + p.pos++ + case c >= '1' && c <= '9': + for p.pos < len(p.src) && p.src[p.pos] >= '0' && p.src[p.pos] <= '9' { + p.pos++ + } + default: + return nil, p.fail("a number without digits") + } + intPart := p.src[start:p.pos] + var frac []byte + if p.pos < len(p.src) && p.src[p.pos] == '.' { + p.pos++ + fs := p.pos + for p.pos < len(p.src) && p.src[p.pos] >= '0' && p.src[p.pos] <= '9' { + p.pos++ + } + if p.pos == fs { + return nil, p.fail("a fraction without digits") + } + frac = p.src[fs:p.pos] + } + exp := 0 + if p.pos < len(p.src) && (p.src[p.pos] == 'e' || p.src[p.pos] == 'E') { + p.pos++ + eneg := false + if p.pos < len(p.src) && (p.src[p.pos] == '+' || p.src[p.pos] == '-') { + eneg = p.src[p.pos] == '-' + p.pos++ + } + es := p.pos + for p.pos < len(p.src) && p.src[p.pos] >= '0' && p.src[p.pos] <= '9' { + p.pos++ + } + if p.pos == es { + return nil, p.fail("an exponent without digits") + } + if p.pos-es > canonMaxExpLen { + return nil, p.fail("an exponent with too many digits") + } + exp, _ = strconv.Atoi(string(p.src[es:p.pos])) + if eneg { + exp = -exp + } + } + if len(intPart)+len(frac) > canonMaxDigits { + return nil, p.fail("a number with too many digits") + } + digits := append(append([]byte{}, intPart...), frac...) + exp -= len(frac) + i := 0 + for i < len(digits) && digits[i] == '0' { + i++ + } + digits = digits[i:] + if len(digits) == 0 { + return []byte("0"), nil + } + for len(digits) > 0 && digits[len(digits)-1] == '0' { + digits = digits[:len(digits)-1] + exp++ + } + if exp > canonMaxExp || exp < -canonMaxExp { + return nil, p.fail("a number outside the exponent limit") + } + out := make([]byte, 0, len(digits)+8) + if neg { + out = append(out, '-') + } + out = append(out, digits...) + out = append(out, 'e') + return strconv.AppendInt(out, int64(exp), 10), nil +} diff --git a/module/canonical_json_test.go b/module/canonical_json_test.go new file mode 100644 index 0000000..ab15992 --- /dev/null +++ b/module/canonical_json_test.go @@ -0,0 +1,192 @@ +//go:build !wasip1 + +package main + +// canonical_json_test.go pins cic-canonical-json/1 (docs/design/specs/canonical-json.md). +// The expectations live in testdata/canonical_json_vectors.json so that another +// implementation can be checked against the same file: Go is not the definition. + +import ( + "bytes" + "encoding/hex" + "encoding/json" + "math/rand" + "os" + "strings" + "testing" +) + +type canonVectors struct { + Format string `json:"format"` + Canonical []struct{ Name, Input, Output string } `json:"canonical"` + Equal []struct{ Name, A, B string } `json:"equal"` + Distinct []struct{ Name, A, B string } `json:"distinct"` + Reject []struct { + Name string `json:"name"` + Input string `json:"input"` + InputHex string `json:"input_hex"` + } `json:"reject"` +} + +func loadCanonVectors(t *testing.T) canonVectors { + t.Helper() + raw, err := os.ReadFile("testdata/canonical_json_vectors.json") + if err != nil { + t.Fatalf("vectors: %v", err) + } + var v canonVectors + if err := json.Unmarshal(raw, &v); err != nil { + t.Fatalf("vectors are not valid JSON: %v", err) + } + if v.Format != "cic-canonical-json/1" { + t.Fatalf("vector format = %q", v.Format) + } + if len(v.Canonical) == 0 || len(v.Equal) == 0 || len(v.Distinct) == 0 || len(v.Reject) == 0 { + t.Fatalf("a vector group is empty: the test would check nothing") + } + return v +} + +func TestCanonicalJSONProducesTheDocumentedForm(t *testing.T) { + for _, c := range loadCanonVectors(t).Canonical { + t.Run(c.Name, func(t *testing.T) { + got, err := canonicalJSON([]byte(c.Input)) + if err != nil { + t.Fatalf("refused %q: %v", c.Input, err) + } + if string(got) != c.Output { + t.Errorf("canonical form of %q = %q, want %q", c.Input, got, c.Output) + } + }) + } +} + +func TestEquivalentTextsShareOneCanonicalForm(t *testing.T) { + for _, c := range loadCanonVectors(t).Equal { + t.Run(c.Name, func(t *testing.T) { + if !canonicalEqual(json.RawMessage(c.A), json.RawMessage(c.B)) { + t.Errorf("%q and %q must be equal", c.A, c.B) + } + }) + } +} + +func TestDifferentValuesNeverShareACanonicalForm(t *testing.T) { + for _, c := range loadCanonVectors(t).Distinct { + t.Run(c.Name, func(t *testing.T) { + if canonicalEqual(json.RawMessage(c.A), json.RawMessage(c.B)) { + t.Errorf("%q and %q are different values and must not be equal", c.A, c.B) + } + }) + } +} + +func TestInputThatIsNotStrictJSONIsRefused(t *testing.T) { + for _, c := range loadCanonVectors(t).Reject { + t.Run(c.Name, func(t *testing.T) { + in := []byte(c.Input) + if c.InputHex != "" { + b, err := hex.DecodeString(c.InputHex) + if err != nil { + t.Fatalf("bad hex in the vectors: %v", err) + } + in = b + } + if out, err := canonicalJSON(in); err == nil { + t.Errorf("accepted %q, canonical %q", in, out) + } + // A refused input is never equal to anything, not even to itself. + if canonicalEqual(json.RawMessage(in), json.RawMessage(in)) { + t.Errorf("a refused input must not compare equal to itself") + } + }) + } +} + +func TestCanonicalFormIsDeterministicAndIdempotent(t *testing.T) { + v := loadCanonVectors(t) + var inputs []string + for _, c := range v.Canonical { + inputs = append(inputs, c.Input) + } + for _, c := range v.Equal { + inputs = append(inputs, c.A, c.B) + } + for _, in := range inputs { + a, err := canonicalJSON([]byte(in)) + if err != nil { + t.Fatalf("%q: %v", in, err) + } + b, _ := canonicalJSON([]byte(in)) + if !bytes.Equal(a, b) { + t.Errorf("%q: two runs differ", in) + } + again, err := canonicalJSON(a) + if err != nil || !bytes.Equal(again, a) { + t.Errorf("%q: canonical form %q is not a fixed point (%q, %v)", in, a, again, err) + } + } +} + +func TestCanonicalJSONBounds(t *testing.T) { + nest := func(n int) string { return strings.Repeat("[", n) + strings.Repeat("]", n) } + if _, err := canonicalJSON([]byte(nest(64))); err != nil { + t.Errorf("depth 64 must be accepted: %v", err) + } + if _, err := canonicalJSON([]byte(nest(65))); err == nil { + t.Errorf("depth 65 must be refused") + } + if _, err := canonicalJSON([]byte(nest(100000))); err == nil { + t.Errorf("a very deep input must be refused, not recursed into") + } + if _, err := canonicalJSON([]byte("1" + strings.Repeat("0", 999))); err != nil { + t.Errorf("a 1000-digit number must be accepted: %v", err) + } + if _, err := canonicalJSON([]byte("1" + strings.Repeat("0", 1000))); err == nil { + t.Errorf("a 1001-digit number must be refused") + } + big := `"` + strings.Repeat("a", 1<<20) + `"` + if _, err := canonicalJSON([]byte(big)); err == nil { + t.Errorf("an input over 1 MiB must be refused") + } + ok := `"` + strings.Repeat("a", (1<<20)-2) + `"` + if _, err := canonicalJSON([]byte(ok)); err != nil { + t.Errorf("an input of exactly 1 MiB must be accepted: %v", err) + } + if _, err := canonicalJSON([]byte("1e10000")); err != nil { + t.Errorf("exponent 10000 must be accepted: %v", err) + } +} + +// Mutated and random input must never panic, and whatever is accepted must be a +// fixed point of the canonical form. +func TestCanonicalJSONNeverPanicsOnMutatedInput(t *testing.T) { + seeds := []string{`{"a":[1,2.5e3,"xé",true,null],"b":{"c":"😀"}}`, `[-0.0,1E2,"\"\\"]`, `{"k":{"k":{"k":[]}}}`} + rng := rand.New(rand.NewSource(20261108)) + for i := 0; i < 4000; i++ { + b := []byte(seeds[i%len(seeds)]) + for j := 0; j < 1+rng.Intn(4); j++ { + switch rng.Intn(3) { + case 0: + b[rng.Intn(len(b))] = byte(rng.Intn(256)) + case 1: + p := rng.Intn(len(b)) + b = append(b[:p], b[p+1:]...) + default: + p := rng.Intn(len(b) + 1) + b = append(b[:p], append([]byte{byte(rng.Intn(256))}, b[p:]...)...) + } + if len(b) == 0 { + b = []byte("0") + } + } + out, err := canonicalJSON(b) + if err != nil { + continue + } + again, err := canonicalJSON(out) + if err != nil || !bytes.Equal(again, out) { + t.Fatalf("accepted %q but its canonical form %q is not a fixed point", b, out) + } + } +} diff --git a/module/plan_noop_exactness_test.go b/module/plan_noop_exactness_test.go new file mode 100644 index 0000000..2bf9ea4 --- /dev/null +++ b/module/plan_noop_exactness_test.go @@ -0,0 +1,96 @@ +//go:build !wasip1 + +package main + +// The noop decision of Plan rests on JSON equality. A float64 comparison called two +// different large integers equal and so planned "noop" for a real change; a duplicate +// key was silently resolved to its last value. These tests pin the exact behaviour. + +import ( + "encoding/json" + "testing" +) + +const instanceKind = "cic:compute:instance" + +func planInstance(t *testing.T, desired, observed string) (providerResult, executionPlan) { + t.Helper() + req := planRequest{ + Kind: instanceKind, + Desired: schemaPayload{SchemaID: "cic:compute:instance-config", SchemaVersion: "v0.1.0", SchemaHash: "abc123", Encoding: encCanonicalJSON, Data: json.RawMessage(desired)}, + Observed: schemaPayload{SchemaID: "cic:compute:instance-state", SchemaVersion: "v0.1.0", SchemaHash: "abc124", Encoding: encCanonicalJSON, Data: json.RawMessage(observed)}, + } + raw, _ := json.Marshal(req) + out, err := Plan(nil, raw) + if err != nil { + t.Fatalf("Plan transport error: %v", err) + } + res := decodeResult(t, out) + var plan executionPlan + if res.Status == "ok" { + if err := json.Unmarshal(res.Result, &plan); err != nil { + t.Fatalf("plan result: %v", err) + } + } + return res, plan +} + +func TestPlanDoesNotCallDifferentLargeIntegersANoop(t *testing.T) { + res, plan := planInstance(t, + `{"shapeConfig":{"ocpus":9007199254740993},"displayName":"x"}`, + `{"shapeConfig":{"ocpus":9007199254740992},"displayName":"x"}`) + if res.Status != "ok" { + t.Fatalf("status %q (%+v)", res.Status, res.Error) + } + if plan.Operation == "noop" || len(plan.ProviderOperations) == 0 { + t.Fatalf("plan = %q with %d operations: two different numbers were treated as equal", plan.Operation, len(plan.ProviderOperations)) + } +} + +func TestPlanStillCallsEquivalentJSONANoop(t *testing.T) { + for _, c := range []struct{ name, desired, observed string }{ + {"number spelling", `{"shapeConfig":{"ocpus":1},"displayName":"x"}`, `{"shapeConfig":{"ocpus":1.0},"displayName":"x"}`}, + {"exponent spelling", `{"shapeConfig":{"ocpus":100},"displayName":"x"}`, `{"shapeConfig":{"ocpus":1e2},"displayName":"x"}`}, + {"key order and whitespace", `{"shapeConfig":{"ocpus":2,"memoryInGBs":16},"displayName":"x"}`, ` { "displayName":"x", "shapeConfig":{ "memoryInGBs":16, "ocpus":2 } } `}, + {"escaped text", `{"displayName":"é"}`, "{\"displayName\":\"\xc3\xa9\"}"}, + } { + t.Run(c.name, func(t *testing.T) { + res, plan := planInstance(t, c.desired, c.observed) + if res.Status != "ok" { + t.Fatalf("status %q (%+v)", res.Status, res.Error) + } + if plan.Operation != "noop" || len(plan.ProviderOperations) != 0 { + t.Fatalf("plan = %q with %d operations, want noop for equivalent JSON", plan.Operation, len(plan.ProviderOperations)) + } + }) + } +} + +func TestPlanRefusesDataWithDuplicateKeysOrBeyondTheBounds(t *testing.T) { + good := `{"displayName":"x"}` + cases := []struct{ name, desired, observed, field string }{ + {"duplicate key in desired", `{"displayName":"a","displayName":"b"}`, good, "desired.data"}, + {"duplicate key in observed", good, `{"displayName":"a","displayName":"b"}`, "observed.data"}, + {"nested duplicate key in desired", `{"shapeConfig":{"ocpus":1,"ocpus":2}}`, good, "desired.data"}, + {"too deep in observed", good, `{"displayName":"x","a":` + repeatStr("[", 70) + repeatStr("]", 70) + `}`, "observed.data"}, + } + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + res, _ := planInstance(t, c.desired, c.observed) + if res.Status != "error" || res.Error == nil || res.Error.Class != classValidation { + t.Fatalf("status %q error %+v, want a validation error", res.Status, res.Error) + } + if res.Error.FieldPath != c.field { + t.Errorf("field_path = %q, want %q", res.Error.FieldPath, c.field) + } + }) + } +} + +func repeatStr(s string, n int) string { + out := make([]byte, 0, len(s)*n) + for i := 0; i < n; i++ { + out = append(out, s...) + } + return string(out) +} diff --git a/module/provider.go b/module/provider.go index 87e22b3..dc230f6 100644 --- a/module/provider.go +++ b/module/provider.go @@ -422,6 +422,19 @@ func Plan(auth, data []byte) ([]byte, error) { if envErrs := checkEnvelope("observed", req.Observed); len(envErrs) > 0 { return errResult(&envErrs[0]) } + // Plan decides "noop" and which fields changed by comparing JSON values, so both + // payloads must be strict, bounded JSON with exact numbers and no repeated key + // (cic-canonical-json/1). A payload that is not is refused here instead of being + // read with the last duplicate winning or a number rounded to a float64. + for _, pl := range []struct { + name string + data json.RawMessage + }{{"desired.data", req.Desired.Data}, {"observed.data", req.Observed.Data}} { + if _, err := canonicalJSON(pl.data); err != nil { + return errResult(&providerError{Class: classValidation, FieldPath: pl.name, + Message: pl.name + " is not canonicalizable JSON: " + err.Error()}) + } + } c, ok := resourceContracts()[req.Kind] if !ok { @@ -563,29 +576,22 @@ func planHash(kind string, desired, observed schemaPayload) string { return hex.EncodeToString(h.Sum(nil)) } -// canonicalEqual compares two JSON instances structurally (key order and -// insignificant whitespace ignored), by re-marshalling through Go's map/slice -// model. Adequate for the noop decision on canonical-json payloads. +// canonicalEqual reports whether two JSON texts are the same value, by their +// cic-canonical-json/1 form (canonical_json.go): exact numbers, key order and +// whitespace and escape spelling ignored. A text that is not strict JSON, or beyond +// the bounds, is equal to nothing, itself included. func canonicalEqual(a, b json.RawMessage) bool { - na, err := reencode(a) + na, err := canonicalJSON(a) if err != nil { return false } - nb, err := reencode(b) + nb, err := canonicalJSON(b) if err != nil { return false } return string(na) == string(nb) } -func reencode(raw json.RawMessage) ([]byte, error) { - var v interface{} - if err := json.Unmarshal(raw, &v); err != nil { - return nil, err - } - return json.Marshal(v) -} - // ---- sign+send ops (scaffold — host capability not yet available) ---- // Observe/Execute/Poll/Invoke/Destroy all require the relay trust-flow sign+send diff --git a/module/testdata/canonical_json_vectors.json b/module/testdata/canonical_json_vectors.json new file mode 100644 index 0000000..16ca795 --- /dev/null +++ b/module/testdata/canonical_json_vectors.json @@ -0,0 +1,397 @@ +{ + "format": "cic-canonical-json/1", + "about": "Language-neutral test vectors for the cic-canonical-json/1 canonical form (see docs/design/specs/canonical-json.md). canonical: the input text must canonicalize to exactly `output`. equal: both texts must have the same canonical form. distinct: the canonical forms must differ. reject: the input must be refused (a hex field is the raw input bytes).", + "canonical": [ + { + "name": "keys are sorted", + "input": "{\"b\":1,\"a\":2}", + "output": "{\"a\":2e0,\"b\":1e0}" + }, + { + "name": "whitespace is dropped", + "input": " \t\r\n{ \"a\" : [ 1 , 2 ] }\n", + "output": "{\"a\":[1e0,2e0]}" + }, + { + "name": "key order is by UTF-8 bytes", + "input": "{\"é\":1,\"z\":2}", + "output": "{\"z\":2e0,\"é\":1e0}" + }, + { + "name": "escaped key equals its literal", + "input": "{\"\\u0061\":1}", + "output": "{\"a\":1e0}" + }, + { + "name": "integer", + "input": "1", + "output": "1e0" + }, + { + "name": "integer with fraction zero", + "input": "1.0", + "output": "1e0" + }, + { + "name": "exponent zero", + "input": "1e0", + "output": "1e0" + }, + { + "name": "trailing zeros move into the exponent", + "input": "100", + "output": "1e2" + }, + { + "name": "upper-case exponent", + "input": "1E2", + "output": "1e2" + }, + { + "name": "fraction", + "input": "0.5", + "output": "5e-1" + }, + { + "name": "negative zero is zero", + "input": "-0", + "output": "0" + }, + { + "name": "zero with fraction is zero", + "input": "0.0", + "output": "0" + }, + { + "name": "negative fraction", + "input": "-1.50", + "output": "-15e-1" + }, + { + "name": "fraction with a negative exponent", + "input": "10.0e-1", + "output": "1e0" + }, + { + "name": "a number beyond float64 precision keeps every digit", + "input": "123456789012345678901234567890", + "output": "12345678901234567890123456789e1" + }, + { + "name": "2^53+1 is kept exactly", + "input": "9007199254740993", + "output": "9007199254740993e0" + }, + { + "name": "a non-ASCII escape is written as the character", + "input": "\"\\u00e9\"", + "output": "\"é\"" + }, + { + "name": "html characters are not escaped", + "input": "\"\\u003c\\u003e\\u0026\"", + "output": "\"<>&\"" + }, + { + "name": "an escaped slash is a slash", + "input": "\"\\/\"", + "output": "\"/\"" + }, + { + "name": "control characters become \\u00xx in lower case", + "input": "\"\\n\\u001F\\t\"", + "output": "\"\\u000a\\u001f\\u0009\"" + }, + { + "name": "a surrogate pair is written as the character", + "input": "\"\\ud83d\\ude00\"", + "output": "\"😀\"" + }, + { + "name": "quote and backslash are escaped", + "input": "\"\\\"\\\\\"", + "output": "\"\\\"\\\\\"" + }, + { + "name": "literals", + "input": "[true,false,null]", + "output": "[true,false,null]" + }, + { + "name": "empty containers", + "input": "{\"a\":{},\"b\":[]}", + "output": "{\"a\":{},\"b\":[]}" + }, + { + "name": "nested order is kept for arrays", + "input": "[{\"b\":1,\"a\":2},[3,1]]", + "output": "[{\"a\":2e0,\"b\":1e0},[3e0,1e0]]" + } + ], + "equal": [ + { + "name": "whitespace", + "a": "{\"a\":1}", + "b": " { \"a\" : 1 } " + }, + { + "name": "key order", + "a": "{\"a\":1,\"b\":2}", + "b": "{\"b\":2,\"a\":1}" + }, + { + "name": "1, 1.0, 1e0 and 10e-1", + "a": "1", + "b": "1.0" + }, + { + "name": "1e0", + "a": "1", + "b": "1e0" + }, + { + "name": "10e-1", + "a": "1", + "b": "10e-1" + }, + { + "name": "100 and 1e2", + "a": "100", + "b": "1e2" + }, + { + "name": "negative zero", + "a": "-0", + "b": "0" + }, + { + "name": "escape and character", + "a": "\"\\u00e9\"", + "b": "\"é\"" + }, + { + "name": "html escape", + "a": "\"\\u003c\"", + "b": "\"<\"" + } + ], + "distinct": [ + { + "name": "2^53+1 and 2^53", + "a": "{\"n\":9007199254740993}", + "b": "{\"n\":9007199254740992}" + }, + { + "name": "1 and 1.5", + "a": "1", + "b": "1.5" + }, + { + "name": "0.1 and a number that rounds to it in float64", + "a": "0.1", + "b": "0.10000000000000001" + }, + { + "name": "1e400 and 1e401 (both overflow float64)", + "a": "1e400", + "b": "1e401" + }, + { + "name": "case matters", + "a": "\"a\"", + "b": "\"A\"" + }, + { + "name": "array order matters", + "a": "[1,2]", + "b": "[2,1]" + }, + { + "name": "a number is not its string", + "a": "{\"a\":1}", + "b": "{\"a\":\"1\"}" + }, + { + "name": "null is not the string null", + "a": "null", + "b": "\"null\"" + }, + { + "name": "true is not the string true", + "a": "true", + "b": "\"true\"" + }, + { + "name": "an empty array is not an empty object", + "a": "[]", + "b": "{}" + }, + { + "name": "a present key is not an absent key", + "a": "{\"a\":1}", + "b": "{}" + } + ], + "reject": [ + { + "name": "duplicate key", + "input": "{\"a\":1,\"a\":2}" + }, + { + "name": "duplicate key through an escape", + "input": "{\"a\":1,\"\\u0061\":2}" + }, + { + "name": "duplicate key in a nested object", + "input": "{\"o\":{\"k\":1,\"k\":1}}" + }, + { + "name": "trailing comma in an object", + "input": "{\"a\":1,}" + }, + { + "name": "trailing comma in an array", + "input": "[1,]" + }, + { + "name": "leading zero", + "input": "01" + }, + { + "name": "plus sign", + "input": "+1" + }, + { + "name": "no integer part", + "input": ".5" + }, + { + "name": "no fraction digits", + "input": "1." + }, + { + "name": "lone minus", + "input": "-" + }, + { + "name": "NaN", + "input": "NaN" + }, + { + "name": "Infinity", + "input": "Infinity" + }, + { + "name": "exponent without digits", + "input": "1e" + }, + { + "name": "exponent sign without digits", + "input": "1e+" + }, + { + "name": "single quotes", + "input": "'a'" + }, + { + "name": "unquoted key", + "input": "{a:1}" + }, + { + "name": "comment", + "input": "1 // c" + }, + { + "name": "raw tab in a string", + "input": "\"a\tb\"" + }, + { + "name": "raw newline in a string", + "input": "\"a\nb\"" + }, + { + "name": "bad escape", + "input": "\"\\x\"" + }, + { + "name": "short unicode escape", + "input": "\"\\u12\"" + }, + { + "name": "lone high surrogate", + "input": "\"\\ud800\"" + }, + { + "name": "lone low surrogate", + "input": "\"\\udc00\"" + }, + { + "name": "high surrogate then a non-surrogate escape", + "input": "\"\\ud800\\u0041\"" + }, + { + "name": "empty input", + "input": "" + }, + { + "name": "whitespace only", + "input": " \n\t" + }, + { + "name": "trailing data", + "input": "{} x" + }, + { + "name": "two values", + "input": "{}{}" + }, + { + "name": "truncated literal", + "input": "tru" + }, + { + "name": "unterminated array", + "input": "[" + }, + { + "name": "unterminated object", + "input": "{\"a\"" + }, + { + "name": "missing value", + "input": "{\"a\":}" + }, + { + "name": "unterminated string", + "input": "\"abc" + }, + { + "name": "exponent beyond the limit", + "input": "1e10001" + }, + { + "name": "negative exponent beyond the limit", + "input": "1e-10001" + }, + { + "name": "exponent with too many digits", + "input": "0e99999999" + }, + { + "name": "byte order mark", + "input_hex": "efbbbf7b7d" + }, + { + "name": "invalid UTF-8 byte in a string", + "input_hex": "22ff22" + }, + { + "name": "overlong UTF-8 encoding in a string", + "input_hex": "22c08022" + }, + { + "name": "UTF-8 encoded surrogate in a string", + "input_hex": "22eda08022" + } + ] +}