Skip to content

Commit cbbc266

Browse files
plamentotevmichael-o
authored andcommitted
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. This closes #73
1 parent fab9b0a commit cbbc266

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

src/main/java/org/codehaus/plexus/archiver/zip/AbstractZipArchiver.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.io.InputStream;
2424
import java.io.OutputStream;
2525
import java.io.SequenceInputStream;
26+
import java.nio.ByteBuffer;
2627
import java.nio.charset.Charset;
2728
import java.util.Hashtable;
2829
import java.util.Stack;
@@ -502,8 +503,7 @@ protected void zipFile( InputStreamSupplier in, ConcurrentJarCreator zOut, Strin
502503
InputStream payload;
503504
if ( ze.isUnixSymlink() )
504505
{
505-
ZipEncoding enc = ZipEncodingHelper.getZipEncoding( getEncoding() );
506-
final byte[] bytes = enc.encode( symlinkDestination ).array();
506+
final byte[] bytes = encodeArchiveEntry( symlinkDestination, getEncoding() );
507507
payload = new ByteArrayInputStream( bytes );
508508
zOut.addArchiveEntry( ze, createInputStreamSupplier( payload ), true );
509509
}
@@ -644,14 +644,24 @@ protected void zipDir( PlexusIoResource dir, ConcurrentJarCreator zOut, String v
644644
else
645645
{
646646
String symlinkDestination = ( (SymlinkDestinationSupplier) dir ).getSymlinkDestination();
647-
ZipEncoding enc = ZipEncodingHelper.getZipEncoding( encodingToUse );
648-
final byte[] bytes = enc.encode( symlinkDestination ).array();
647+
final byte[] bytes = encodeArchiveEntry( symlinkDestination, encodingToUse );
649648
ze.setMethod( ZipArchiveEntry.DEFLATED );
650649
zOut.addArchiveEntry( ze, createInputStreamSupplier( new ByteArrayInputStream( bytes ) ), true );
651650
}
652651
}
653652
}
654653

654+
private byte[] encodeArchiveEntry( String payload, String encoding )
655+
throws IOException
656+
{
657+
ZipEncoding enc = ZipEncodingHelper.getZipEncoding( encoding );
658+
ByteBuffer encodedPayloadByteBuffer = enc.encode( payload );
659+
byte[] encodedPayloadBytes = new byte[encodedPayloadByteBuffer.limit()];
660+
encodedPayloadByteBuffer.get( encodedPayloadBytes );
661+
662+
return encodedPayloadBytes;
663+
}
664+
655665
protected InputStreamSupplier createInputStreamSupplier( final InputStream inputStream )
656666
{
657667
return new InputStreamSupplier()

0 commit comments

Comments
 (0)