Skip to content

Commit e44c2dc

Browse files
committed
feat(dlx): add sha256 option for binary download integrity verification
1 parent 9fa85fc commit e44c2dc

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [5.11.1](https://github.com/SocketDev/socket-lib/releases/tag/v5.11.1) - 2026-03-24
9+
10+
### Added
11+
12+
- **dlx/binary**: Added `sha256` option to `dlxBinary()`, `downloadBinary()`, and `downloadBinaryFile()`
13+
- Enables SHA-256 checksum verification for binary downloads via httpDownload
14+
- Verification happens during download (fails early if checksum mismatches)
15+
- Complements existing `integrity` option (SRI sha512 format, verified post-download)
16+
817
## [5.11.0](https://github.com/SocketDev/socket-lib/releases/tag/v5.11.0) - 2026-03-23
918

1019
### Added

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@socketsecurity/lib",
3-
"version": "5.11.0",
4-
"packageManager": "pnpm@10.32.1",
3+
"version": "5.11.1",
4+
"packageManager": "pnpm@10.33.0",
55
"license": "MIT",
66
"description": "Core utilities and infrastructure for Socket.dev security tools",
77
"keywords": [

src/dlx/binary.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ export interface DlxBinaryOptions {
8282
*/
8383
integrity?: string | undefined
8484

85+
/**
86+
* Expected SHA-256 hex checksum for verification.
87+
* Passed to httpDownload for inline verification during download.
88+
* This is more secure than post-download verification as it fails early.
89+
*/
90+
sha256?: string | undefined
91+
8592
/**
8693
* Cache TTL in milliseconds (default: 7 days).
8794
*/
@@ -263,6 +270,7 @@ export async function dlxBinary(
263270
force: userForce = false,
264271
integrity,
265272
name,
273+
sha256,
266274
spawnOptions,
267275
url,
268276
yes,
@@ -346,7 +354,12 @@ export async function dlxBinary(
346354
}
347355

348356
// Download the binary.
349-
computedIntegrity = await downloadBinaryFile(url, binaryPath, integrity)
357+
computedIntegrity = await downloadBinaryFile(
358+
url,
359+
binaryPath,
360+
integrity,
361+
sha256,
362+
)
350363

351364
// Get file size for metadata.
352365
const stats = await fs.promises.stat(binaryPath)
@@ -412,6 +425,7 @@ export async function downloadBinary(
412425
force = false,
413426
integrity,
414427
name,
428+
sha256,
415429
url,
416430
} = { __proto__: null, ...options } as DlxBinaryOptions
417431
const fs = getFs()
@@ -466,6 +480,7 @@ export async function downloadBinary(
466480
url,
467481
binaryPath,
468482
integrity,
483+
sha256,
469484
)
470485

471486
// Get file size for metadata.
@@ -490,11 +505,18 @@ export async function downloadBinary(
490505
* Download a file from a URL with integrity checking and concurrent download protection.
491506
* Uses processLock to prevent multiple processes from downloading the same binary simultaneously.
492507
* Internal helper function for downloading binary files.
508+
*
509+
* Supports two integrity verification methods:
510+
* - sha256: Hex SHA-256 checksum (verified inline during download via httpDownload)
511+
* - integrity: SRI format sha512-<base64> (verified post-download)
512+
*
513+
* The sha256 option is preferred as it fails early during download if the checksum doesn't match.
493514
*/
494515
export async function downloadBinaryFile(
495516
url: string,
496517
destPath: string,
497518
integrity?: string | undefined,
519+
sha256?: string | undefined,
498520
): Promise<string> {
499521
// Use process lock to prevent concurrent downloads.
500522
// Lock is placed in the cache entry directory as 'concurrency.lock'.
@@ -521,9 +543,11 @@ export async function downloadBinaryFile(
521543
}
522544
}
523545

524-
// Download the file.
546+
// Download the file with optional SHA-256 verification.
547+
// The sha256 option enables inline verification during download,
548+
// which is more secure as it fails early if the checksum doesn't match.
525549
try {
526-
await httpDownload(url, destPath)
550+
await httpDownload(url, destPath, sha256 ? { sha256 } : undefined)
527551
} catch (e) {
528552
throw new Error(
529553
`Failed to download binary from ${url}\n` +

0 commit comments

Comments
 (0)