Skip to content

Commit 580b3e5

Browse files
committed
cli/context/store: extract importTLSEntry helper
Pulls the per-entry TLS decoding out of importZip so the outer loop stays under gocyclo's complexity limit. Pure refactor, no behavior change. Signed-off-by: texasich <texasich@users.noreply.github.com>
1 parent d2d8986 commit 580b3e5

1 file changed

Lines changed: 21 additions & 18 deletions

File tree

cli/context/store/store.go

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -474,24 +474,7 @@ func importZip(name string, s Writer, reader io.Reader) error {
474474
}
475475
importedMetaFile = true
476476
} else if strings.HasPrefix(zf.Name, "tls/") {
477-
// Reject entries whose advertised uncompressed size exceeds
478-
// the per-file cap without decompressing, to avoid allocating
479-
// gigabytes for a zip bomb (see #6917).
480-
if zf.UncompressedSize64 > uint64(maxAllowedFileSizeToImport) {
481-
return invalidParameter(fmt.Errorf("%s: tls file exceeds maximum allowed size", zf.Name))
482-
}
483-
f, err := zf.Open()
484-
if err != nil {
485-
return err
486-
}
487-
// Defense in depth in case the zip header is spoofed.
488-
data, err := io.ReadAll(&limitedReader{R: f, N: maxAllowedFileSizeToImport})
489-
defer f.Close()
490-
if err != nil {
491-
return err
492-
}
493-
err = importEndpointTLS(&tlsData, zf.Name, data)
494-
if err != nil {
477+
if err := importTLSEntry(zf, &tlsData); err != nil {
495478
return err
496479
}
497480
}
@@ -502,6 +485,26 @@ func importZip(name string, s Writer, reader io.Reader) error {
502485
return s.ResetTLSMaterial(name, &tlsData)
503486
}
504487

488+
func importTLSEntry(zf *zip.File, tlsData *ContextTLSData) error {
489+
// Reject entries whose advertised uncompressed size exceeds
490+
// the per-file cap without decompressing, to avoid allocating
491+
// gigabytes for a zip bomb (see #6917).
492+
if zf.UncompressedSize64 > uint64(maxAllowedFileSizeToImport) {
493+
return invalidParameter(fmt.Errorf("%s: tls file exceeds maximum allowed size", zf.Name))
494+
}
495+
f, err := zf.Open()
496+
if err != nil {
497+
return err
498+
}
499+
defer f.Close()
500+
// Defense in depth in case the zip header is spoofed.
501+
data, err := io.ReadAll(&limitedReader{R: f, N: maxAllowedFileSizeToImport})
502+
if err != nil {
503+
return err
504+
}
505+
return importEndpointTLS(tlsData, zf.Name, data)
506+
}
507+
505508
func parseMetadata(data []byte, name string) (Metadata, error) {
506509
var meta Metadata
507510
if err := json.Unmarshal(data, &meta); err != nil {

0 commit comments

Comments
 (0)