Skip to content

Commit ac83419

Browse files
committed
validate chunk sizes received from provider
1 parent cd8cbe5 commit ac83419

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

internal/plugin6/grpc_provider.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"sync"
1313

1414
plugin "github.com/hashicorp/go-plugin"
15+
"github.com/hashicorp/hcl/v2"
1516
"github.com/zclconf/go-cty/cty"
1617
"github.com/zclconf/go-cty/cty/function"
1718
ctyjson "github.com/zclconf/go-cty/cty/json"
@@ -1574,7 +1575,6 @@ func (p *GRPCProvider) ReadStateBytes(r providers.ReadStateBytesRequest) (resp p
15741575

15751576
buf := &bytes.Buffer{}
15761577
var expectedTotalLength int
1577-
// TODO: Send warning if client misbehaves and uses (lower) chunk size that we didn't agree on
15781578
for {
15791579
chunk, err := client.Recv()
15801580
if err == io.EOF {
@@ -1597,6 +1597,36 @@ func (p *GRPCProvider) ReadStateBytes(r providers.ReadStateBytesRequest) (resp p
15971597
}
15981598
logger.Trace("GRPCProvider.v6: ReadStateBytes: received chunk for range", chunk.Range)
15991599

1600+
// check the size of chunks matches to what was agreed
1601+
chunkSize, ok := p.stateChunkSize[r.TypeName]
1602+
if !ok {
1603+
resp.Diagnostics = resp.Diagnostics.Append(fmt.Errorf("Unable to determine chunk size for provider %s; this is a bug in Terraform - please report it", r.TypeName))
1604+
return resp
1605+
}
1606+
if chunk.Range.End < chunk.TotalLength {
1607+
// all but last chunk must match exactly
1608+
if len(chunk.Bytes) != chunkSize {
1609+
resp.Diagnostics = resp.Diagnostics.Append(&hcl.Diagnostic{
1610+
Severity: hcl.DiagWarning,
1611+
Summary: "Unexpected size of chunk received",
1612+
Detail: fmt.Sprintf("Unexpected chunk of size %d was received, expected %d; this is a bug in the provider %s - please report it there",
1613+
len(chunk.Bytes), chunkSize, r.TypeName),
1614+
})
1615+
return resp
1616+
}
1617+
} else {
1618+
// last chunk must be still within the agreed size
1619+
if len(chunk.Bytes) > chunkSize {
1620+
resp.Diagnostics = resp.Diagnostics.Append(&hcl.Diagnostic{
1621+
Severity: hcl.DiagWarning,
1622+
Summary: "Unexpected size of last chunk received",
1623+
Detail: fmt.Sprintf("Last chunk exceeded agreed size, expected %d, given %d; this is a bug in the provider %s - please report it there",
1624+
chunkSize, len(chunk.Bytes), r.TypeName),
1625+
})
1626+
return resp
1627+
}
1628+
}
1629+
16001630
n, err := buf.Write(chunk.Bytes)
16011631
if err != nil {
16021632
resp.Diagnostics = resp.Diagnostics.Append(err)

0 commit comments

Comments
 (0)