Skip to content

Commit 1d58276

Browse files
committed
Revert "Revert "PLXCOMP-220: defensive checks to avoid passing negative uid/gid into TarEntry or encoding into tar output stream""
This reverts commit 580c425.
1 parent 63d7ac0 commit 1d58276

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/main/java/org/codehaus/plexus/archiver/tar/PosixTarEntry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ public void writeEntryHeader( byte[] outbuf )
121121

122122
offset = TarUtils.getNameBytes( this.name, outbuf, offset, NAMELEN );
123123
offset = TarUtils.getOctalBytes( this.mode, outbuf, offset, MODELEN );
124-
offset = TarUtils.getOctalBytes( this.userId, outbuf, offset, UIDLEN );
125-
offset = TarUtils.getOctalBytes( this.groupId, outbuf, offset, GIDLEN );
124+
offset = TarUtils.getOctalBytes( ( this.userId >= 0 ? this.userId : 0 ), outbuf, offset, UIDLEN );
125+
offset = TarUtils.getOctalBytes( ( this.groupId >= 0 ? this.groupId : 0 ), outbuf, offset, GIDLEN );
126126
offset = TarUtils.getLongOctalBytes( this.size, outbuf, offset, SIZELEN );
127127
offset = TarUtils.getLongOctalBytes( this.modTime, outbuf, offset, MODTIMELEN );
128128

src/main/java/org/codehaus/plexus/archiver/tar/TarArchiver.java

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@
1717
* limitations under the License.
1818
*/
1919

20-
import java.io.BufferedOutputStream;
21-
import java.io.File;
22-
import java.io.FileOutputStream;
23-
import java.io.IOException;
24-
import java.io.InputStream;
25-
import java.io.OutputStream;
26-
import java.util.zip.GZIPOutputStream;
27-
2820
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream;
2921
import org.codehaus.plexus.archiver.AbstractArchiver;
3022
import org.codehaus.plexus.archiver.ArchiveEntry;
@@ -38,6 +30,14 @@
3830
import org.codehaus.plexus.util.IOUtil;
3931
import org.codehaus.plexus.util.StringUtils;
4032

33+
import java.io.BufferedOutputStream;
34+
import java.io.File;
35+
import java.io.FileOutputStream;
36+
import java.io.IOException;
37+
import java.io.InputStream;
38+
import java.io.OutputStream;
39+
import java.util.zip.GZIPOutputStream;
40+
4141
/**
4242
* @author <a href="mailto:[email protected]">Emmanuel Venisse</a>
4343
* @version $Revision$ $Date$
@@ -249,7 +249,7 @@ protected void tarFile( ArchiveEntry entry, TarOutputStream tOut, String vPath )
249249
int pathLength = vPath.length();
250250
try
251251
{
252-
TarEntry te = null;
252+
final TarEntry te;
253253
if ( !longFileMode.isGnuMode() && pathLength >= TarConstants.NAMELEN )
254254
{
255255
int maxPosixPathLen = TarConstants.NAMELEN + TarConstants.POSIX_PREFIXLEN;
@@ -337,10 +337,20 @@ else if ( longFileMode.isFailMode() )
337337
: options.getUserName() );
338338
te.setGroupName( ( attributes != null && attributes.getGroupName() != null ) ? attributes.getGroupName()
339339
: options.getGroup() );
340-
te.setUserId( ( attributes != null && attributes.getUserId() != null) ? attributes.getUserId()
341-
: options.getUid() );
342-
te.setGroupId( ( attributes != null && attributes.getGroupId() != null) ? attributes.getGroupId()
343-
: options.getGid() );
340+
341+
final int userId =
342+
( attributes != null && attributes.getUserId() != null ) ? attributes.getUserId() : options.getUid();
343+
if ( userId > 0 )
344+
{
345+
te.setUserId( userId );
346+
}
347+
348+
final int groupId =
349+
( attributes != null && attributes.getGroupId() != null ) ? attributes.getGroupId() : options.getGid();
350+
if ( groupId > 0 )
351+
{
352+
te.setGroupId( groupId );
353+
}
344354

345355
tOut.putNextEntry( te );
346356

src/main/java/org/codehaus/plexus/archiver/tar/TarEntry.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
* ([email protected]) to whom the Ant project is very grateful for his great code.
2323
*/
2424

25+
import org.codehaus.plexus.archiver.ArchiveFile;
26+
2527
import java.io.File;
2628
import java.util.Date;
2729
import java.util.Locale;
2830

29-
import org.codehaus.plexus.archiver.ArchiveFile;
30-
3131
/**
3232
* This class represents an entry in a Tar archive. It consists
3333
* of the entry's header, as well as the entry's File. Entries
@@ -670,8 +670,8 @@ public void writeEntryHeader( byte[] outbuf )
670670

671671
offset = TarUtils.getNameBytes( this.name, outbuf, offset, NAMELEN );
672672
offset = TarUtils.getOctalBytes( this.mode, outbuf, offset, MODELEN );
673-
offset = TarUtils.getOctalBytes( this.userId, outbuf, offset, UIDLEN );
674-
offset = TarUtils.getOctalBytes( this.groupId, outbuf, offset, GIDLEN );
673+
offset = TarUtils.getOctalBytes( ( this.userId >= 0 ? this.userId : 0 ), outbuf, offset, UIDLEN );
674+
offset = TarUtils.getOctalBytes( ( this.groupId >= 0 ? this.groupId : 0 ), outbuf, offset, GIDLEN );
675675
offset = TarUtils.getLongOctalBytes( this.size, outbuf, offset, SIZELEN );
676676
offset = TarUtils.getLongOctalBytes( this.modTime, outbuf, offset, MODTIMELEN );
677677

0 commit comments

Comments
 (0)