Unverified Commit 27f2c628 authored by Bruno Borges's avatar Bruno Borges Committed by GitHub
Browse files

Verify JDK downloads with vendor checksums (#1167)

* Verify JDK downloads with vendor checksums

Co-authored-by: default avatarCopilot App <[email protected]>

Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d

* Handle missing vendor checksum values

Co-authored-by: default avatarCopilot App <[email protected]>

Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d

* Preserve checksum error during cleanup failure

Co-authored-by: default avatarCopilot App <[email protected]>

Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d

* Validate checksum metadata value types

Co-authored-by: default avatarCopilot App <[email protected]>

Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d

* Clarify checksum documentation

Co-authored-by: default avatarCopilot App <[email protected]>

Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d

* Expand vendor checksum verification

Co-authored-by: default avatarCopilot App <[email protected]>

Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d

* Accept SHA-256 or SHA-512 for JetBrains checksum sibling

JetBrains publishes a single, generically-named ".checksum" sibling
whose digest algorithm isn't disclosed by the filename. Older JBR 11
builds (e.g. jbrsdk_nomod-11_0_16-*-b2043.64.tar.gz) publish a SHA-256
digest there, while newer builds publish SHA-512. The JetBrains
installer previously assumed SHA-512 unconditionally, so verification
failed with "Malformed sha512 checksum metadata ... expected a
128-character hexadecimal digest" for those older builds, breaking the
jetbrains 11 e2e job on macOS and Windows.

fetchChecksum now accepts a list of candidate algorithms and infers
the actual algorithm from the returned digest's length, preferring the
strongest match. The JetBrains installer passes ['sha512', 'sha256'];
all other callers are unaffected since they already pass a single,
vendor-disclosed algorithm.

Co-authored-by: default avatarCopilot App <[email protected]>
Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d

* Use SapMachine archive checksum files

Co-authored-by: default avatarCopilot App <[email protected]>

Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d

---------

Co-authored-by: default avatarCopilot App <[email protected]>
Copilot-Session: a800a031-600e-4d28-b23e-be309555d38d
parent 19c23b37
Loading
Loading
Loading
Loading
+19
Changes for .github/workflows/e2e-versions.yml: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -125,6 +125,25 @@ jobs:
        run: bash __tests__/verify-java.sh "$JAVA_VERSION" "$JAVA_PATH"
        shell: bash

  setup-java-checksum-verification:
    name: Corretto checksum verification - ubuntu-latest
    runs-on: ubuntu-latest
    steps:
      - *checkout_step
      - name: setup-java with forced download
        uses: ./
        id: setup-java
        with:
          java-version: '21'
          distribution: corretto
          force-download: true
      - name: Verify Java
        env:
          JAVA_VERSION: '21'
          JAVA_PATH: ${{ steps.setup-java.outputs.path }}
        run: bash __tests__/verify-java.sh "$JAVA_VERSION" "$JAVA_PATH"
        shell: bash

  setup-java-alpine-linux:
    name: ${{ matrix.distribution }} ${{ matrix.version }} (jdk-${{ contains(matrix.os, 'macos') && !contains(matrix.os, 'intel') && 'arm64' || 'x64' }}) - alpine-linux - ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
+8
Changes for README.md: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -95,6 +95,14 @@ For more details, see the full release notes on the [releases page](https://git

  - `show-download-progress`: Set to `true` to keep Maven artifact download and transfer progress in build logs. Default value: `false`. By default, the action adds `-ntp` (`--no-transfer-progress`) to `MAVEN_ARGS`. This input has no effect on non-Maven builds. See [Maven transfer progress](docs/advanced-usage.md#maven-transfer-progress-download-logs) for more details.

### Download integrity verification

When a selected distribution publishes an authoritative checksum for an archive, `setup-java` automatically verifies each downloaded JDK, JRE, or JMOD archive before extraction and caching. No input is required. Automatic checksum verification is currently available for `temurin`, `semeru`, `adopt`, `corretto`, `dragonwell`, `kona`, `sapmachine`, `graalvm`, `graalvm-community`, `zulu`, `oracle`, `oracle-openjdk`, `microsoft`, and `jetbrains`.

Distributions or individual releases without an authoritative checksum continue to install normally, with the omission reported only in debug logs. Archives resolved directly from the runner tool cache are not downloaded again and therefore are not reverified.

Checksums detect corrupted or unexpectedly modified downloads before they are persisted in the runner tool cache.

### Basic Configuration

#### Eclipse Temurin
+130
Changes for __tests__/checksum.test.ts: 130 added lines, 0 removed lines.
Original line number Diff line number Diff line
import {afterEach, describe, expect, it, jest} from '@jest/globals';
import {createHash} from 'crypto';
import fs from 'fs';
import os from 'os';
import path from 'path';

import {calculateChecksum, verifyChecksum} from '../src/checksum.js';
import type {ChecksumMetadata} from '../src/distributions/base-models.js';

const temporaryPaths: string[] = [];

async function temporaryFile(contents: string): Promise<string> {
  const directory = await fs.promises.mkdtemp(
    path.join(os.tmpdir(), 'setup-java-checksum-')
  );
  const file = path.join(directory, 'archive');
  await fs.promises.writeFile(file, contents);
  temporaryPaths.push(directory);
  return file;
}

afterEach(async () => {
  await Promise.all(
    temporaryPaths
      .splice(0)
      .map(item => fs.promises.rm(item, {recursive: true, force: true}))
  );
  jest.restoreAllMocks();
});

describe('verifyChecksum', () => {
  it.each(['sha256', 'sha512'] as const)(
    'verifies a matching %s digest',
    async algorithm => {
      const contents = `jdk archive for ${algorithm}`;
      const file = await temporaryFile(contents);
      const value = createHash(algorithm).update(contents).digest('hex');

      await expect(
        verifyChecksum(
          file,
          {algorithm, value: value.toUpperCase()},
          {distribution: 'Test', version: '21.0.1'}
        )
      ).resolves.toBeUndefined();
    }
  );

  it('reports mismatch context and both digests', async () => {
    const file = await temporaryFile('corrupt archive');
    const expected = 'a'.repeat(64);
    const actual = await calculateChecksum(file, 'sha256');

    await expect(
      verifyChecksum(
        file,
        {algorithm: 'sha256', value: expected},
        {distribution: 'Corretto', version: '21.0.8'}
      )
    ).rejects.toThrow(
      `Checksum verification failed for Corretto version 21.0.8: sha256 expected ${expected}, actual ${actual}.`
    );
  });

  it('rejects malformed digest metadata before reading the file', async () => {
    await expect(
      verifyChecksum(
        '/missing/archive',
        {algorithm: 'sha512', value: 'not-a-digest'},
        {distribution: 'Test', version: '17'}
      )
    ).rejects.toThrow(
      'Malformed sha512 checksum metadata: expected a 128-character hexadecimal digest.'
    );
  });

  it.each([undefined, null, 123])(
    'reports a malformed digest when the value is %p',
    async value => {
      const checksum = {
        algorithm: 'sha256',
        value
      } as unknown as ChecksumMetadata;

      await expect(
        verifyChecksum('/missing/archive', checksum, {
          distribution: 'Test',
          version: '17'
        })
      ).rejects.toThrow(
        'Malformed sha256 checksum metadata: expected a 64-character hexadecimal digest.'
      );
    }
  );

  it('rejects unsupported algorithms without leaking source query parameters', async () => {
    const checksum = {
      algorithm: 'md5',
      value: 'a'.repeat(32),
      source: 'https://vendor.example/checksum.txt?token=secret-value#private'
    } as unknown as ChecksumMetadata;

    let message = '';
    try {
      await verifyChecksum('/missing/archive', checksum, {
        distribution: 'Test',
        version: '17'
      });
    } catch (error) {
      message = (error as Error).message;
    }

    expect(message).toContain(
      "Unsupported checksum algorithm 'md5' from https://vendor.example/checksum.txt"
    );
    expect(message).not.toContain('secret-value');
    expect(message).not.toContain('token=');
    expect(message).not.toContain('#private');
  });

  it('surfaces file read errors', async () => {
    await expect(
      verifyChecksum(
        '/missing/archive',
        {algorithm: 'sha256', value: 'a'.repeat(64)},
        {distribution: 'Test', version: '17'}
      )
    ).rejects.toMatchObject({code: 'ENOENT'});
  });
});
+8
Changes for __tests__/distributors/adopt-installer.test.ts: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -357,6 +357,14 @@ describe('findPackageForDownload', () => {
    distribution['getAvailableVersions'] = async () => manifestData as any;
    const resolvedVersion = await distribution['findPackageForDownload'](input);
    expect(resolvedVersion.version).toBe(expected);
    const vendorPackage = (manifestData as any[]).find(
      item => item.version_data.semver === expected
    ).binaries[0].package;
    expect(resolvedVersion.checksum).toEqual({
      algorithm: 'sha256',
      value: vendorPackage.checksum,
      source: vendorPackage.checksum_link
    });
  });

  it('version is found but binaries list is empty', async () => {
+314
Changes for __tests__/distributors/base-installer.test.ts: 314 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@ import type {

import path from 'path';
import * as semver from 'semver';
import fs from 'fs';
import {createHash} from 'crypto';
import {HttpClient} from '@actions/http-client';

import os from 'os';

@@ -117,6 +120,17 @@ class EmptyJavaBase extends JavaBase {
      url: `some/random_url/java/${availableVersion}`
    };
  }

  public downloadRelease(javaRelease: JavaDownloadRelease): Promise<string> {
    return this.downloadAndVerify(javaRelease);
  }

  public fetchChecksumForTest(
    checksumUrl: string,
    algorithm: 'sha256' | 'sha512' | ('sha256' | 'sha512')[]
  ) {
    return this.fetchChecksum(checksumUrl, algorithm);
  }
}

describe('findInToolcache', () => {
@@ -817,6 +831,306 @@ describe('setupJava', () => {
  });
});

describe('downloadAndVerify', () => {
  const options: JavaInstallerOptions = {
    version: '21',
    architecture: 'x64',
    packageType: 'jdk',
    checkLatest: false
  };
  let temporaryDirectory: string;
  let archivePath: string;

  beforeEach(async () => {
    temporaryDirectory = await fs.promises.mkdtemp(
      path.join(os.tmpdir(), 'setup-java-base-')
    );
    archivePath = path.join(temporaryDirectory, 'archive');
    await fs.promises.writeFile(archivePath, 'downloaded archive');
    (tc.downloadTool as jest.Mock<any>).mockResolvedValue(archivePath);
  });

  afterEach(async () => {
    await fs.promises.rm(temporaryDirectory, {recursive: true, force: true});
    jest.resetAllMocks();
  });

  it('returns a download after successful verification', async () => {
    const distribution = new EmptyJavaBase(options);
    const result = await distribution.downloadRelease({
      version: '21.0.8',
      url: 'https://vendor.example/jdk.tar.gz',
      checksum: {
        algorithm: 'sha256',
        value: createHash('sha256').update('downloaded archive').digest('hex')
      }
    });

    expect(result).toBe(archivePath);
    expect(fs.existsSync(archivePath)).toBe(true);
    expect(core.debug).toHaveBeenCalledWith(
      'Verified sha256 checksum for Empty version 21.0.8.'
    );
  });

  it('removes the download after verification failure', async () => {
    const distribution = new EmptyJavaBase(options);

    await expect(
      distribution.downloadRelease({
        version: '21.0.8',
        url: 'https://vendor.example/jdk.tar.gz?token=secret',
        checksum: {algorithm: 'sha256', value: 'a'.repeat(64)}
      })
    ).rejects.toThrow('Checksum verification failed for Empty version 21.0.8');

    expect(fs.existsSync(archivePath)).toBe(false);
  });

  it('preserves the verification error when removing the download fails', async () => {
    const distribution = new EmptyJavaBase(options);
    const cleanupError = new Error('cleanup failed');
    jest.spyOn(fs.promises, 'rm').mockRejectedValueOnce(cleanupError);

    const result = distribution.downloadRelease({
      version: '21.0.8',
      url: 'https://vendor.example/jdk.tar.gz',
      checksum: {algorithm: 'sha256', value: 'a'.repeat(64)}
    });

    await expect(result).rejects.toMatchObject({
      message: expect.stringContaining(
        'Failed to remove the downloaded archive after verification failure: cleanup failed'
      ),
      cause: expect.objectContaining({
        message: expect.stringContaining(
          'Checksum verification failed for Empty version 21.0.8'
        )
      })
    });
  });

  it('logs when authoritative checksum metadata is unavailable', async () => {
    const distribution = new EmptyJavaBase(options);

    await expect(
      distribution.downloadRelease({
        version: '21.0.8',
        url: 'https://vendor.example/jdk.tar.gz'
      })
    ).resolves.toBe(archivePath);

    expect(core.debug).toHaveBeenCalledWith(
      'No authoritative checksum is available for Empty version 21.0.8; skipping checksum verification.'
    );
  });

  it.each([undefined, '', '   '])(
    'skips verification when the vendor digest is %p',
    async value => {
      const distribution = new EmptyJavaBase(options);

      await expect(
        distribution.downloadRelease({
          version: '21.0.8',
          url: 'https://vendor.example/jdk.tar.gz',
          checksum: {
            algorithm: 'sha256',
            value
          } as JavaDownloadRelease['checksum']
        })
      ).resolves.toBe(archivePath);

      expect(core.debug).toHaveBeenCalledWith(
        'No authoritative checksum is available for Empty version 21.0.8; skipping checksum verification.'
      );
    }
  );
});

describe('fetchChecksum', () => {
  const options: JavaInstallerOptions = {
    version: '21',
    architecture: 'x64',
    packageType: 'jdk',
    checkLatest: false
  };

  afterEach(() => {
    jest.restoreAllMocks();
  });

  function mockGet(statusCode: number, body: string) {
    return jest.spyOn(HttpClient.prototype, 'get').mockResolvedValue({
      message: {statusCode},
      readBody: async () => body
    } as any);
  }

  it('parses a bare hex digest', async () => {
    const digest = 'a'.repeat(64);
    const spy = mockGet(200, digest);
    const distribution = new EmptyJavaBase(options);

    const checksum = await distribution.fetchChecksumForTest(
      'https://vendor.example/jdk.tar.gz.sha256',
      'sha256'
    );

    expect(spy).toHaveBeenCalledWith(
      'https://vendor.example/jdk.tar.gz.sha256'
    );
    expect(checksum).toEqual({
      algorithm: 'sha256',
      value: digest,
      source: 'https://vendor.example/jdk.tar.gz.sha256'
    });
  });

  it('parses only the first token of a GNU-style checksum file', async () => {
    const digest = 'b'.repeat(128);
    mockGet(200, `${digest}  jbrsdk-21.0.3-linux-x64-b465.3.tar.gz\n`);
    const distribution = new EmptyJavaBase(options);

    const checksum = await distribution.fetchChecksumForTest(
      'https://vendor.example/jdk.tar.gz.checksum',
      'sha512'
    );

    expect(checksum).toEqual({
      algorithm: 'sha512',
      value: digest,
      source: 'https://vendor.example/jdk.tar.gz.checksum'
    });
  });

  it('trims surrounding whitespace and newlines', async () => {
    const digest = 'c'.repeat(64);
    mockGet(200, `\n  ${digest}  \n`);
    const distribution = new EmptyJavaBase(options);

    const checksum = await distribution.fetchChecksumForTest(
      'https://vendor.example/jdk.tar.gz.sha256',
      'sha256'
    );

    expect(checksum.value).toBe(digest);
  });

  it('skips verification when the sibling checksum is not published', async () => {
    mockGet(404, 'Not Found');
    const distribution = new EmptyJavaBase(options);

    await expect(
      distribution.fetchChecksumForTest(
        'https://vendor.example/jdk.tar.gz.sha256',
        'sha256'
      )
    ).resolves.toBeUndefined();
    expect(core.debug).toHaveBeenCalledWith(
      'No authoritative sha256 checksum is available for Empty from https://vendor.example/jdk.tar.gz.sha256; skipping checksum verification.'
    );
  });

  it('surfaces unexpected HTTP failures without query parameters', async () => {
    mockGet(500, 'Server Error');
    const distribution = new EmptyJavaBase(options);

    await expect(
      distribution.fetchChecksumForTest(
        'https://vendor.example/jdk.tar.gz.sha256?token=secret',
        'sha256'
      )
    ).rejects.toThrow(
      'Failed to fetch the authoritative sha256 checksum for Empty from https://vendor.example/jdk.tar.gz.sha256 (HTTP 500).'
    );
  });

  it('rejects an empty successful checksum response', async () => {
    mockGet(200, '  \n');
    const distribution = new EmptyJavaBase(options);

    await expect(
      distribution.fetchChecksumForTest(
        'https://vendor.example/jdk.tar.gz.sha256',
        'sha256'
      )
    ).rejects.toThrow(
      'Received an empty authoritative sha256 checksum for Empty from https://vendor.example/jdk.tar.gz.sha256.'
    );
  });

  describe('with a list of candidate algorithms', () => {
    it('infers sha512 when the digest is 128 hex characters', async () => {
      const digest = 'd'.repeat(128);
      mockGet(200, `${digest}  jbrsdk.tar.gz\n`);
      const distribution = new EmptyJavaBase(options);

      const checksum = await distribution.fetchChecksumForTest(
        'https://vendor.example/jbrsdk.tar.gz.checksum',
        ['sha512', 'sha256']
      );

      expect(checksum).toEqual({
        algorithm: 'sha512',
        value: digest,
        source: 'https://vendor.example/jbrsdk.tar.gz.checksum'
      });
    });

    it('infers sha256 when the digest is 64 hex characters, even though sha512 was preferred', async () => {
      // Reproduces older JetBrains JBR builds (e.g. JBR 11), which publish a
      // SHA-256 digest at the generic `.checksum` sibling instead of SHA-512.
      const digest = 'e'.repeat(64);
      mockGet(200, `${digest}  jbrsdk_nomod-11_0_16-osx-x64-b2043.64.tar.gz\n`);
      const distribution = new EmptyJavaBase(options);

      const checksum = await distribution.fetchChecksumForTest(
        'https://vendor.example/jbrsdk_nomod-11_0_16-osx-x64-b2043.64.tar.gz.checksum',
        ['sha512', 'sha256']
      );

      expect(checksum).toEqual({
        algorithm: 'sha256',
        value: digest,
        source:
          'https://vendor.example/jbrsdk_nomod-11_0_16-osx-x64-b2043.64.tar.gz.checksum'
      });
    });

    it('falls back to the first candidate algorithm when the digest length matches none of them', async () => {
      const digest = 'f'.repeat(40); // e.g. sha1, not supported
      mockGet(200, `${digest}  jbrsdk.tar.gz\n`);
      const distribution = new EmptyJavaBase(options);

      const checksum = await distribution.fetchChecksumForTest(
        'https://vendor.example/jbrsdk.tar.gz.checksum',
        ['sha512', 'sha256']
      );

      // No candidate algorithm matches, so the first-listed one is kept;
      // downstream verification will reject it as malformed.
      expect(checksum.algorithm).toBe('sha512');
      expect(checksum.value).toBe(digest);
    });

    it('reports the checksum as unavailable using a combined algorithm label on 404', async () => {
      mockGet(404, 'Not Found');
      const distribution = new EmptyJavaBase(options);

      await expect(
        distribution.fetchChecksumForTest(
          'https://vendor.example/jbrsdk.tar.gz.checksum',
          ['sha512', 'sha256']
        )
      ).resolves.toBeUndefined();
      expect(core.debug).toHaveBeenCalledWith(
        'No authoritative sha512 or sha256 checksum is available for Empty from https://vendor.example/jbrsdk.tar.gz.checksum; skipping checksum verification.'
      );
    });
  });
});

describe('normalizeVersion', () => {
  const DummyJavaBase = JavaBase as any;

Loading