Skip to content

Commit

Permalink
Fix symbolic links not properly encoded in ZIP archives
Browse files Browse the repository at this point in the history
To encode the symbolic links `AbstractZipArchiver` uses
`ZipEncoding` but it does not use it properly.
It uses the whole backing array of the returned ByteBuffer
while it it should use it only up to the buffer limit.
This may lead to additional characters at the end of the
symbolic link.

This does not manifest as a bug because Common Compress up to 1.14
returns a ByteBuffer that wraps a byte array containing the
encoded payload. But Common Compress 1.15 returns new
implementation and we need to fix it before upgrading.
Actually that is how it was found.

This closes #73
  • Loading branch information
plamentotev authored and michael-o committed Oct 21, 2017
1 parent fab9b0a commit cbbc266
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.SequenceInputStream;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Hashtable;
import java.util.Stack;
Expand Down Expand Up @@ -502,8 +503,7 @@ protected void zipFile( InputStreamSupplier in, ConcurrentJarCreator zOut, Strin
InputStream payload;
if ( ze.isUnixSymlink() )
{
ZipEncoding enc = ZipEncodingHelper.getZipEncoding( getEncoding() );
final byte[] bytes = enc.encode( symlinkDestination ).array();
final byte[] bytes = encodeArchiveEntry( symlinkDestination, getEncoding() );
payload = new ByteArrayInputStream( bytes );
zOut.addArchiveEntry( ze, createInputStreamSupplier( payload ), true );
}
Expand Down Expand Up @@ -644,14 +644,24 @@ protected void zipDir( PlexusIoResource dir, ConcurrentJarCreator zOut, String v
else
{
String symlinkDestination = ( (SymlinkDestinationSupplier) dir ).getSymlinkDestination();
ZipEncoding enc = ZipEncodingHelper.getZipEncoding( encodingToUse );
final byte[] bytes = enc.encode( symlinkDestination ).array();
final byte[] bytes = encodeArchiveEntry( symlinkDestination, encodingToUse );
ze.setMethod( ZipArchiveEntry.DEFLATED );
zOut.addArchiveEntry( ze, createInputStreamSupplier( new ByteArrayInputStream( bytes ) ), true );
}
}
}

private byte[] encodeArchiveEntry( String payload, String encoding )
throws IOException
{
ZipEncoding enc = ZipEncodingHelper.getZipEncoding( encoding );
ByteBuffer encodedPayloadByteBuffer = enc.encode( payload );
byte[] encodedPayloadBytes = new byte[encodedPayloadByteBuffer.limit()];
encodedPayloadByteBuffer.get( encodedPayloadBytes );

return encodedPayloadBytes;
}

protected InputStreamSupplier createInputStreamSupplier( final InputStream inputStream )
{
return new InputStreamSupplier()
Expand Down

0 comments on commit cbbc266

Please sign in to comment.