Skip to content

Delete temporary file when ExportedImageTar construction fails#51117

Open
wantaekchoi wants to merge 1 commit into
spring-projects:4.0.xfrom
wantaekchoi:fix/exported-image-tar-temp-file
Open

Delete temporary file when ExportedImageTar construction fails#51117
wantaekchoi wants to merge 1 commit into
spring-projects:4.0.xfrom
wantaekchoi:fix/exported-image-tar-temp-file

Conversation

@wantaekchoi

@wantaekchoi wantaekchoi commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

ExportedImageTar creates a temporary file in its constructor, copies the exported image into it, and then builds the layer factory from it:

ExportedImageTar(ImageReference reference, InputStream inputStream) throws IOException {
	this.tarFile = Files.createTempFile("docker-layers-", null);
	Files.copy(inputStream, this.tarFile, StandardCopyOption.REPLACE_EXISTING);
	this.layerArchiveFactory = LayerArchiveFactory.create(reference, this.tarFile);
}

The only caller relies on try-with-resources to clean the file up:

try (ExportedImageTar exportedImageTar = new ExportedImageTar(reference, response.getContent())) {
	exportedImageTar.exportLayers(exports);
}

If Files.copy or LayerArchiveFactory.create throws, the constructor fails after the temporary file has been created, so the instance is never assigned and close() never runs. LayerArchiveFactory.create does throw on a tar that has neither index.json nor manifest.json (Assert.state(...)), which can happen for a malformed or unexpected image export, and the docker-layers- file is then left behind in the temp directory.

This wraps the constructor body so the temporary file is deleted if either step fails, matching the cleanup already done in close().

This mirrors #50919, which fixed the same kind of leak in the buildpack platform and included a temp-file test; the new test in ExportedImageTarTests uses the same approach.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 24, 2026
ExportedImageTar creates a temporary file in its constructor and then
copies the exported image into it and builds the layer factory. If
either step throws, the constructor fails before the instance is
assigned, so the try-with-resources statement that uses it never calls
close() and the docker-layers- temporary file is left behind.

Delete the temporary file when the constructor fails so its cleanup
matches close().

Signed-off-by: wantaek <wantaekchoi@gmail.com>
@wantaekchoi
wantaekchoi force-pushed the fix/exported-image-tar-temp-file branch from 5badd6c to f6480f7 Compare July 24, 2026 07:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: waiting-for-triage An issue we've not yet triaged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants