Skip to content

Commit

Permalink
Do content sensing to make sure the file is really not a jar
Browse files Browse the repository at this point in the history
Currently we rely on the file-ending to determine the type of file but
this can fail in cases a CacheManger returns a cached file without that
extension (for whatever reason).

This now adds another check for content sensing if the simple check
fails to ensure a zipped item is not parsed as plain text.
  • Loading branch information
laeubi committed Oct 21, 2023
1 parent 413c76a commit 158291b
Showing 1 changed file with 22 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public IMetadataRepository load(URI location, int flags, IProgressMonitor monito
JarInputStream jarStream = null;
try {
//if reading from a jar, obtain a stream on the entry with the actual contents
if (localFile.getAbsolutePath().endsWith(JAR_EXTENSION)) {
if (localFile.getAbsolutePath().endsWith(JAR_EXTENSION) || hasZipMagicHeader(inStream)) {
jarStream = new JarInputStream(inStream);
JarEntry jarEntry = jarStream.getNextJarEntry();
String entryName = URLMetadataRepository.CONTENT_FILENAME + URLMetadataRepository.XML_EXTENSION;
Expand Down Expand Up @@ -133,10 +133,30 @@ public IMetadataRepository load(URI location, int flags, IProgressMonitor monito
}
}

/**
* Check if given stream is a jar ...
*
* @param stream the stream
* @return <code>true</code> if the stream supports mark/reset and has the two
* magic bytes PK at its current position
* @throws IOException
*/
private static boolean hasZipMagicHeader(InputStream stream) throws IOException {
if (stream.markSupported()) {
stream.mark(2);
// 50 4B
int one = stream.read();
int two = stream.read();
stream.reset();
return ((one & 0xFF) == 0x50 && (two & 0xFF) == 0x4B);
}
return false;
}

/**
* Closes a stream, ignoring any secondary exceptions
*/
private void safeClose(InputStream stream) {
private static void safeClose(InputStream stream) {
if (stream == null)
return;
try {
Expand Down

0 comments on commit 158291b

Please sign in to comment.