Skip to content

ZipEndOfCentralDirectoryRecord finds the record signature inside the zip comment - #52255

Merged
wilkinsona merged 2 commits into
spring-projects:4.0.xfrom
zhoubiao188:fix-loader-eocd-signature-in-comment
Oct 29, 2026
Merged

wilkinsona merged 2 commits into
spring-projects:4.0.xfrom
zhoubiao188:fix-loader-eocd-signature-in-comment

Conversation

@zhoubiao188

Copy link
Copy Markdown

The end of central directory record is located by scanning backwards through the data for the record signature:

private static int findInBuffer(ByteBuffer buffer) {
	for (int pos = buffer.limit() - 4; pos >= 0; pos--) {
		buffer.position(pos);
		if (buffer.getInt() == SIGNATURE) {
			return pos;
		}
	}
	return -1;
}

The signature bytes are not unique. They can also occur in the file comment, which follows the record and is therefore closer to the end of the file, so the scan finds the comment occurrence first and reads the record from the wrong position:

java.nio.BufferUnderflowException
	at java.base/java.nio.HeapByteBuffer.getShort(HeapByteBuffer.java:365)
	at org.springframework.boot.loader.zip.ZipEndOfCentralDirectoryRecord.load(ZipEndOfCentralDirectoryRecord.java:97)

A candidate is now only accepted when the whole record is available in the buffer and the comment that it declares does not extend beyond the end of the data. Candidates that fail the check are skipped and the scan continues, so the actual record is found. The same rule is used by the JDK ZipFile, which also rejects a signature match when the comment length is not consistent with the file size.

The loadLocatesAndLoadsData and asByteArrayReturnsByteArray test data declared a 7 byte comment but did not provide the comment bytes, so they are not valid records. They now include the comment that they declare.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. label Oct 16, 2026
@wilkinsona

Copy link
Copy Markdown
Member

Thanks, @zhoubiao188, for all your recent contributions.

We only have a finite amount of time to work on the project. When it comes to bugs, that time is best spent identifying and fixing those that will bring the most benefit. The greater the severity of a bug or the more people it affects, the greater the benefit of fixing it.

Are you finding these problems based on your own real-word usage of Spring Boot or are you asking an AI to find bugs and help you fix them? The chances of a real-world zip file having a comment that contains PK\u0005\u0006 seems rather unlikely so I suspect that it's the latter.

while (offset >= 0) {
if (isValidLocation(dataBlock, buffer, startPos, offset)) {
debug.log("Found EndOfCentralDirectoryRecord at %s + %s", startPos, offset);
buffer.position(offset + Integer.BYTES);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the buffer's position now being set? It appears to be unnecessary and the tests all pass without it. If it is necessary, a test should be added that fails when it isn't set.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it is not necessary. findInBuffer positions the buffer and then reads the signature with a relative getInt(), so when it returns the buffer is already positioned just after the signature bytes. Reading the fields in load relies on exactly that, as the original code did, so nothing has to be repositioned before returning. I have removed the redundant call.

@wilkinsona wilkinsona added status: waiting-for-feedback We need additional information before we can continue type: bug A general bug and removed status: waiting-for-feedback We need additional information before we can continue status: waiting-for-triage An issue that has not yet been triaged. Contributions will not be accepted. labels Oct 28, 2026
@wilkinsona wilkinsona added this to the 4.0.x milestone Oct 29, 2026
wilkinsona pushed a commit to zhoubiao188/spring-boot that referenced this pull request Oct 29, 2026
…e comment

The 'end of central directory record' is located by scanning backwards
through the data for the record signature. The signature bytes are not
unique, they can also occur in other places, in particular in the file
comment that follows the record. When that happens the scan finds the
comment occurrence first (it is closer to the end) and the record is read
from the wrong position:

	java.nio.BufferUnderflowException

A candidate is now only accepted when the whole record is available and
the comment that it declares doesn't extend beyond the end of the data.
Candidates that fail the check are skipped and the scan continues, so the
actual record is found.

The scan leaves the buffer positioned after the signature bytes that it
matched, so the fields of the record can be read straight away without
repositioning the buffer.

The loadLocatesAndLoadsData and asByteArrayReturnsByteArray test data
declared a 7 byte comment without providing the comment bytes, so they
are not valid records. They now include the comment that they declare.

Signed-off-by: zhoubiao188 <32508238+zhoubiao188@users.noreply.github.com>

See spring-projectsgh-52255
wilkinsona added a commit to zhoubiao188/spring-boot that referenced this pull request Oct 29, 2026
…es in the comment"

See spring-projectsgh-52255

Signed-off-by: Andy Wilkinson <andy.wilkinson@broadcom.com>
@wilkinsona
wilkinsona force-pushed the fix-loader-eocd-signature-in-comment branch from 5e3cf85 to c15691d Compare October 29, 2026 08:41
zhoubiao188 and others added 2 commits October 29, 2026 08:51
The 'end of central directory record' is located by scanning backwards
through the data for the record signature. The signature bytes are not
unique, they can also occur in other places, in particular in the file
comment that follows the record. When that happens the scan finds the
comment occurrence first (it is closer to the end) and the record is read
from the wrong position:

	java.nio.BufferUnderflowException

A candidate is now only accepted when the whole record is available and
the comment that it declares doesn't extend beyond the end of the data.
Candidates that fail the check are skipped and the scan continues, so the
actual record is found.

The scan leaves the buffer positioned after the signature bytes that it
matched, so the fields of the record can be read straight away without
repositioning the buffer.

The loadLocatesAndLoadsData and asByteArrayReturnsByteArray test data
declared a 7 byte comment without providing the comment bytes, so they
are not valid records. They now include the comment that they declare.

Signed-off-by: zhoubiao188 <32508238+zhoubiao188@users.noreply.github.com>

See spring-projectsgh-52255
See spring-projectsgh-52255

Signed-off-by: Andy Wilkinson <andy.wilkinson@broadcom.com>
@wilkinsona
wilkinsona force-pushed the fix-loader-eocd-signature-in-comment branch from c15691d to e7ac487 Compare October 29, 2026 08:52
@wilkinsona wilkinsona modified the milestones: 4.0.x, 4.0.10 Oct 29, 2026
@wilkinsona
wilkinsona merged commit 7814f53 into spring-projects:4.0.x Oct 29, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants