Skip to content

Commit

Permalink
Ignore unrecognized pax headers
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Dec 27, 2020
1 parent 77da2f6 commit 7685edc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
16 changes: 16 additions & 0 deletions lib/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,22 @@ const paxHeaderUname = 'uname';
const paxHeaderGname = 'gname';
const paxHeaderSize = 'size';

/// These are the pax headers considered when reading tar files.
///
/// Other pax headers are dropped in the reader to avoid memory-based DOS
/// attacks. We already limit the size of a headers file by default, but an
/// attacker could provide many small global header files with bogus keys, which
/// we'd all have to store.
/// With this approach, we can ensure that the reader's buffer will have an
/// upper bound of `(supportedPaxHeaders.length + 1) * maxHeaderSize`.
const supportedPaxHeaders = {
paxHeaderLinkName,
paxHeaderPath,
paxHeaderUname,
paxHeaderGname,
paxHeaderSize,
};

const defaultSpecialLength = blockSize * 2;

extension ToTyped on List<int> {
Expand Down
7 changes: 6 additions & 1 deletion lib/src/reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ class _BoundTarStream {
keyBuffer.writeCharCode(currentChar);
currentChar = _buffer[++offset];
}
final key = keyBuffer.toString();
// Skip over the equals sign
offset++;

Expand All @@ -358,7 +359,11 @@ class _BoundTarStream {
final lengthOfValue = length - 3 - keyBuffer.length - charsInLength;
final value =
utf8.decode(_buffer.sublist(offset, offset + lengthOfValue));
map[keyBuffer.toString()] = value;
// Ignore unrecognized headers to avoid unbounded growth of the global
// header map.
if (supportedPaxHeaders.contains(key)) {
map[key] = value;
}

// Skip over value and trailing newline
offset += lengthOfValue + 1;
Expand Down

0 comments on commit 7685edc

Please sign in to comment.