Coze: MapItem is unsafe, and MapSlice does not have a well-defined order

indexCounter is accessed without synchronization, which produces data races that violate the memory model. As a proof of concept, build and run the following program with the -race flag.

package main

import (
	"encoding/json"
	"sync"

	"github.com/cyphrme/coze"
)

func main() {
	n := 10
	var wg sync.WaitGroup
	for i := 0; i < n; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			var item coze.MapItem
			json.Unmarshal([]byte(`{}`), &item)
		}()
	}
	wg.Wait()
}

You’ll see output like the following.

==================
WARNING: DATA RACE
Read at 0x00010041e838 by goroutine 13:
  github.com/cyphrme/coze.nextIndex()
      .../pkg/mod/github.com/cyphrme/coze@v0.0.3/mapslice.go:36 +0xf0
  github.com/cyphrme/coze.(*MapItem).UnmarshalJSON()
      .../pkg/mod/github.com/cyphrme/coze@v0.0.3/mapslice.go:118 +0x11c
  encoding/json.(*decodeState).object()
      ...

Previous write at 0x00010041e838 by goroutine 12:
  github.com/cyphrme/coze.nextIndex()
      .../pkg/mod/github.com/cyphrme/coze@v0.0.3/mapslice.go:36 +0x108
  github.com/cyphrme/coze.(*MapItem).UnmarshalJSON()
      .../pkg/mod/github.com/cyphrme/coze@v0.0.3/mapslice.go:118 +0x11c
  encoding/json.(*decodeState).object()
      ...

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 1
  • Comments: 53 (29 by maintainers)

Most upvoted comments

The Coze signing/verification step is not JSON aware, it’s just UTF-8. Non-JSON, UTF-8 octets are required for for signing and verification. On ingress, after verifying the UTF-8 payload, unmarshalling may result in an unordered JSON data structure. On egress, unordered JSON is marshaled into UTF-8 before signing by Coze.

Touching on any potential implementation concerns, It’s just a few lines of code to transform from the Coze form into the correct UTF-8 form. The resulting UTF-8 octets are valid JSON.

To summarize previous posts and to come back to address the origin of order information, this is my recommendation for transmitting order information in this ranking.

  1. Conveyed order by UTF-8. (Intermediaries should not modify pay and must leave it alone.)
  2. Use content-type:application/coze to instruct intermediaries to leave cozies alone.
  3. Passing can in the coze e.g. {"pay":...,"can":[...], "sig":...} and reserialize pay into the correct UTF-8 form needed for verification.
  4. Serialize the coze into a format that intermediaries leave alone.

If order was a practical problem, I’d expect to see issues caused by JOSE/JWS/JWT as the payload is JSON and must also be exact UTF-8 bytes.

https://github.com/Cyphrme/Coze/blob/a3fbc8a3086328d725e8de94df6e071f5d1a02ac/orderedmap.go#L108-L114

o.values is a map type, and iteration over a map is non-deterministic, so the order of the values in the v slice is also non-deterministic. But it seems like you expect a specific order in the tests:

https://github.com/Cyphrme/Coze/blob/a3fbc8a3086328d725e8de94df6e071f5d1a02ac/orderedmap_test.go#L93-L99

If values should be returned in key order, then you probably want something like

func (o *orderedMap) Values() []any {
	var values []any
	for _, k := range o.keys {
		if v, ok := o.values[k]; ok {
			values = append(values, v)
		}
	}
	return values
}

edit: Note that this example implementation does not provide stable order of values recursively, for example if a value were itself a JSON object (a map of key to value) then the order of the keys and/or values in that value would not be deterministic.

On the topic of MapSlice, this library actually does look like a suitable replacement for MapSlice: https://github.com/iancoleman/orderedmap

I don’t think it would take me too much time to implement.

I would vender that repo so Coze doesn’t have external dependencies.

indexCounter appears to be used to populate an index field for MapItem values, and that index field appears to be used to establish an order of items in a MapSlice value. That order is stable only within a specific instance of a program; a MapSlice produced by one PID has an order that is not necessarily equal to the order of the same MapSlice produced (or received) by a different PID. So MapSlice order is not deterministic, and does not provide “a given order for maps” beyond a process boundary.