mirror of
https://github.com/vxcontrol/pentagi.git
synced 2026-07-20 12:37:04 -04:00
[PR #129] [CLOSED] fix: prevent resource leaks and unbounded allocation in tools #178
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/vxcontrol/pentagi/pull/129
Author: @mason5052
Created: 2/22/2026
Status: ❌ Closed
Base:
feature/project_improvements← Head:fix/resource-cleanup-browser-terminal📝 Commits (2)
b6fba4efix: prevent resource leaks and unbounded allocation in tools78e7dfaMerge branch 'feature/project_improvements' into fix/resource-cleanup-browser-terminal📊 Changes
2 files changed (+11 additions, -7 deletions)
View changed files
📝
backend/pkg/tools/browser.go(+4 -2)📝
backend/pkg/tools/terminal.go(+7 -5)📄 Description
Description of the Change
Problem
Three resource management issues in the tools package:
Response body leak in
browser.go(callScraper): Thedefer resp.Body.Close()was placed after the HTTP status code check. When the server returns a non-200 status, the function returns early without closing the response body. This leaks the underlying TCP connection (held open until GC finalizer runs), which can exhaust connection pools under load.Unbounded memory allocation in
terminal.go(ReadFile): ThetarHeader.Sizefield from a DockerCopyFromContainerresponse is used directly inmake([]byte, tarHeader.Size)without validation. A corrupted or malicious tar archive with a multi-GBSizeheader field triggers an OOM crash. Negative sizes also cause a panic.Incomplete tar archive in
terminal.go(WriteFile): The tar writer is never explicitly closed after writing content. WithouttarWriter.Close(), the two 512-byte zero blocks that mark the end of a tar archive are not flushed. This produces a structurally invalid archive that may fail silently or cause extraction errors on the container side.Solution
Move
defer resp.Body.Close()immediately after the error check forclient.Get(), before the status code check. This ensures the body is always closed regardless of the response status.Add size validation before the
make()call: reject files over 50MB and reject negative sizes with clear error messages.Add explicit
tarWriter.Close()with error handling before passing the archive toCopyToContainer.Ref: #101
Type of Change
Areas Affected
Testing and Verification
Test Steps
defer resp.Body.Close()now executes on all pathstarWriter.Close()is called before archive is sent to containerSecurity Considerations
Checklist
Code Quality
go fmtandgo vet(for Go code)Security
Compatibility
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.