Skip to content

Commit bc638ec

Browse files
committed
validate chunk sizes received from provider
1 parent 730f206 commit bc638ec

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"
@@ -1561,7 +1562,6 @@ func (p *GRPCProvider) ReadStateBytes(r providers.ReadStateBytesRequest) (resp p
15611562

15621563
buf := &bytes.Buffer{}
15631564
var expectedTotalLength int
1564-
// TODO: Send warning if client misbehaves and uses (lower) chunk size that we didn't agree on
15651565
for {
15661566
chunk, err := client.Recv()
15671567
if err == io.EOF {
@@ -1584,6 +1584,36 @@ func (p *GRPCProvider) ReadStateBytes(r providers.ReadStateBytesRequest) (resp p
15841584
}
15851585
logger.Trace("GRPCProvider.v6: ReadStateBytes: received chunk for range", chunk.Range)
15861586

1587+
// check the size of chunks matches to what was agreed
1588+
chunkSize, ok := p.stateChunkSize[r.TypeName]
1589+
if !ok {
1590+
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))
1591+
return resp
1592+
}
1593+
if chunk.Range.End < chunk.TotalLength {
1594+
// all but last chunk must match exactly
1595+
if len(chunk.Bytes) != chunkSize {
1596+
resp.Diagnostics = resp.Diagnostics.Append(&hcl.Diagnostic{
1597+
Severity: hcl.DiagWarning,
1598+
Summary: "Unexpected size of chunk received",
1599+
Detail: fmt.Sprintf("Unexpected chunk of size %d was received, expected %d; this is a bug in the provider %s - please report it there",
1600+
len(chunk.Bytes), chunkSize, r.TypeName),
1601+
})
1602+
return resp
1603+
}
1604+
} else {
1605+
// last chunk must be still within the agreed size
1606+
if len(chunk.Bytes) > chunkSize {
1607+
resp.Diagnostics = resp.Diagnostics.Append(&hcl.Diagnostic{
1608+
Severity: hcl.DiagWarning,
1609+
Summary: "Unexpected size of last chunk received",
1610+
Detail: fmt.Sprintf("Last chunk exceeded agreed size, expected %d, given %d; this is a bug in the provider %s - please report it there",
1611+
chunkSize, len(chunk.Bytes), r.TypeName),
1612+
})
1613+
return resp
1614+
}
1615+
}
1616+
15871617
n, err := buf.Write(chunk.Bytes)
15881618
if err != nil {
15891619
resp.Diagnostics = resp.Diagnostics.Append(err)

0 commit comments

Comments
 (0)