From 3a232d77c4350f996b04c98f5dcaa11a9d6fa44f Mon Sep 17 00:00:00 2001 From: Plamen Totev Date: Sat, 21 Oct 2017 10:13:09 +0300 Subject: [PATCH] Fix symbolic links not properly encoded in ZIP archives 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. --- .../archiver/zip/AbstractZipArchiver.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java b/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java index 501387d72..57799c683 100755 --- a/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java @@ -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; @@ -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 ); } @@ -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()