Skip to content

Commit

Permalink
msgpack: truncate prefix refinements
Browse files Browse the repository at this point in the history
Note that this does not work because it fails to round trip.
  • Loading branch information
kmoe committed Aug 23, 2023
1 parent 74eb76b commit e702e81
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions cty/msgpack/unknown.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/vmihailenco/msgpack/v5"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/ctystrings"
)

type unknownType struct{}
Expand Down Expand Up @@ -85,6 +86,16 @@ func marshalUnknownValue(rng cty.ValueRange, path cty.Path, enc *msgpack.Encoder
}
case rng.TypeConstraint() == cty.String:
if prefix := rng.StringPrefix(); prefix != "" {
// To ensure the total size of the refinements blob does not exceed
// the limit set by our decoder, truncate the prefix string.
// We could allow up to 1018 bytes here if we assume that this
// refinement will only ever be combined with NotNull(), but there
// is no need for such long prefix refinements at the moment.
maxPrefixLength := 256
if len(prefix) > maxPrefixLength {
prefix = prefix[:maxPrefixLength]
prefix = ctystrings.SafeKnownPrefix(prefix)
}
mapLen++
refnEnc.EncodeInt(int64(unknownValStringPrefix))
refnEnc.EncodeString(prefix)
Expand Down

0 comments on commit e702e81

Please sign in to comment.