Jackie-Jiang commented on code in PR #20424:
URL: https://github.com/apache/pinot/pull/20424#discussion_r4806969556


##########
pinot-common/src/main/java/org/apache/pinot/common/utils/TarCompressionUtils.java:
##########
@@ -269,6 +287,96 @@ public static List<File> untarWithRateLimiter(InputStream 
inputStream, File outp
     return untarredFiles;
   }
 
+  /// Enforces the path budget on extension headers that Commons Compress 
handles inside getNextEntry().
+  /// Like the underlying TAR reader, this stream is not thread-safe.
+  private static final class PathBudgetTarInputStream extends 
TarArchiveInputStream {
+    private final ArchivePathBudget _budget;
+    private TarArchiveEntry _checkedEntry;
+
+    private PathBudgetTarInputStream(InputStream inputStream, 
ArchivePathBudget budget) {
+      super(inputStream);
+      _budget = budget;
+    }
+
+    @Override
+    public int read(byte[] buffer, int offset, int length)
+        throws IOException {
+      TarArchiveEntry entry = getCurrentEntry();
+      if (entry != null && entry != _checkedEntry) {
+        _budget.chargeExtendedHeader(entry);
+        _checkedEntry = entry;
+      }
+      return super.read(buffer, offset, length);
+    }
+  }
+
+  /// A request-scoped path budget shared by sequential archive extractions 
within that request.
+  public static final class ArchivePathBudget {
+    private final int _maxDepth;
+    private final int _maxNameLength;
+    private long _remainingPathWork;
+
+    public ArchivePathBudget(int maxDepth, int maxNameLength, long 
maxPathWork) {
+      Preconditions.checkArgument(maxDepth > 0 && maxNameLength > 0 && 
maxPathWork > 0,
+          "Archive path limits must be positive");
+      _maxDepth = maxDepth;
+      _maxNameLength = maxNameLength;
+      _remainingPathWork = maxPathWork;
+    }
+
+    private void chargeExtendedHeader(TarArchiveEntry entry)
+        throws ArchivePathLimitException {
+      // Allow a multibyte name plus its terminator, and one TAR record of 
framing for PAX metadata.
+      long maxHeaderBytes = 4L * _maxNameLength + 1;
+      if (entry.isPaxHeader() || entry.isGlobalPaxHeader()) {
+        maxHeaderBytes += TarConstants.DEFAULT_RCDSIZE;
+      } else if (!entry.isGNULongNameEntry() && !entry.isGNULongLinkEntry()) {
+        return;
+      }
+      long size = entry.getSize();
+      if (size > maxHeaderBytes || size > _remainingPathWork) {
+        throw new ArchivePathLimitException("Archive extended header exceeds 
the path limit");
+      }
+      _remainingPathWork -= size;

Review Comment:
   [P2] Bound consecutive extension headers
   
   This charges only the extension payload size, so empty headers consume no 
budget. Commons Compress 1.28.0 recursively calls `getNextEntry()` from 
`getLongNameData()`, `paxHeaders()`, and `readGlobalPaxHeaders()`. A 
sufficiently long chain can therefore reach `StackOverflowError` before control 
returns to the final entry-name check, bypassing the intended 
`ArchivePathLimitException`/HTTP 413 response. Small nonzero payloads also 
permit excessive recursion under the default 64 MiB budget.
   
   Could we bound consecutive/nested extension headers before the recursive 
read and add a regression test asserting `ArchivePathLimitException`? Charging 
a minimum number of bytes per header alone would not establish a safe recursion 
limit.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to