Skip to content

Fix detection of some PAX TAR formats #762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2025

Conversation

Borewit
Copy link
Collaborator

@Borewit Borewit commented May 23, 2025

It checks for 6-byte magic signature:

Resolves: #760

@Borewit Borewit self-assigned this May 23, 2025
@Borewit Borewit added the bug label May 23, 2025
@Borewit Borewit requested a review from Copilot May 23, 2025 11:32
Copilot

This comment was marked as resolved.

@Borewit
Copy link
Collaborator Author

Borewit commented May 23, 2025

Just in case we may conclude we need this in the future, this is an updated version the TAR checksum algorithm able to detect this kind of TAR files. This needs to read multiple blocks. Problem is that I have no way to do recursive peek reads yet, so cannot be used in a detection easily.

import { StringType } from 'token-types';
import { type ITokenizer } from 'strtok3';

export async function isTarHeaderChecksumMatches(tokenizer: ITokenizer): Promise<boolean> {
  const blockSize = 512;
  const typeflagOffset = 156;
  let blockIndex = 0;

  while (true) {
    const header = new Uint8Array(blockSize);
    const size = await tokenizer.peekBuffer(header, { mayBeLess: true });

    console.debug(`Reading block at index ${blockIndex}, bytes read: ${size}`);

    if (size < blockSize) {
      console.debug('Less than 512 bytes read — exiting.');
      break;
    }

    if (header.every(byte => byte === 0)) {
      console.debug('Encountered empty block — assuming end of archive.');
      break;
    }

    const typeflag = String.fromCharCode(header[typeflagOffset]);
    console.debug(`typeflag: "${typeflag}"`);

    const rawSumStr = new StringType(8, 'ascii').get(header, 148);
    const cleanedSumStr = rawSumStr.replace(/\0.*$/, '').trim();

    if (cleanedSumStr === '') {
      console.debug('Checksum field empty or spaces — skipping block.');
      await tokenizer.ignore(blockSize);
      blockIndex++;
      continue;
    }

    const readSum = Number.parseInt(cleanedSumStr, 8);

    console.debug(`Raw checksum string: "${rawSumStr}", cleaned: "${cleanedSumStr}", parsed: ${readSum}`);

    if (Number.isNaN(readSum)) {
      console.debug('Checksum is NaN — skipping block.');
      await tokenizer.ignore(blockSize);
      blockIndex++;
      continue;
    }

    let sum = 0;
    for (let i = 0; i < blockSize; i++) {
      sum += (i >= 148 && i < 156) ? 0x20 : header[i];
    }

    console.debug(`Calculated checksum: ${sum}`);

    if (readSum === sum) {
      console.debug('Checksum valid.');
      if (typeflag !== 'g' && typeflag !== 'x') {
        return true;  // Found valid regular header
      } else {
        console.debug(`Skipping extended header of type "${typeflag}".`);
      }
    } else {
      console.debug('Checksum mismatch — skipping block.');
    }

    await tokenizer.ignore(blockSize);
    blockIndex++;
  }

  return false;
}

@sindresorhus sindresorhus merged commit 574d0d6 into main May 23, 2025
6 checks passed
@sindresorhus sindresorhus deleted the detect-some-pax-tar-archives branch May 23, 2025 11:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Current tar file detection is unreliable
2 participants