security: hardening по результатам аудита безопасности

This commit is contained in:
2026-08-08 22:48:16 +03:00
parent 8c8631ac9c
commit bf2bceb520
18 changed files with 400 additions and 20 deletions
+24 -1
View File
@@ -74,6 +74,11 @@ var (
// ErrUnexpectedEOF indicates the chunk stream ended before any chunk
// with flags&0x01==1 (logical end-of-stream marker) was observed.
ErrUnexpectedEOF = errors.New("composite: unexpected end of stream")
// ErrPlaintextTooLarge indicates the decrypted payload would exceed the
// configured maximum plaintext size; decryption is aborted before the
// limit is crossed to prevent unbounded disk consumption.
ErrPlaintextTooLarge = errors.New("composite: plaintext size limit exceeded")
)
// Format constants.
@@ -89,6 +94,7 @@ const (
wrappedCekLen int = 48 // 32-byte CEK + 16-byte GCM tag
firstPayloadNonceLen int = 12
kekLen int = 32
maxPlaintextSize int64 = 1 << 40 // 1 TiB cap on decrypted output
infoPq string = "git.tswf.io/infra/go-synapse-backupper/v2/kek/pq"
infoComposite string = "git.tswf.io/infra/go-synapse-backupper/v2/kek/composite"
)
@@ -505,7 +511,24 @@ func (d *decryptor) Decrypt(
return err
}
return decryptChunks(src, plaintext, payloadGcm, firstPayloadNonce)
plaintextLimiter := &limitedWriter{writer: plaintext, remaining: maxPlaintextSize}
return decryptChunks(src, plaintextLimiter, payloadGcm, firstPayloadNonce)
}
// limitedWriter wraps an io.Writer and rejects writes that would exceed a
// maximum byte budget.
type limitedWriter struct {
writer io.Writer
remaining int64
}
func (lw *limitedWriter) Write(p []byte) (int, error) {
if int64(len(p)) > lw.remaining {
return 0, ErrPlaintextTooLarge
}
n, err := lw.writer.Write(p)
lw.remaining -= int64(n)
return n, err
}
// decryptChunks reads and decrypts chunk records until a final chunk