Skip to content

Commit ab17c22

Browse files
fix(p1): use deterministic proxy checksum (#38)
1 parent 4deed69 commit ab17c22

File tree

1 file changed

+21
-29
lines changed

1 file changed

+21
-29
lines changed

src/core/updater.ts

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import crypto from 'crypto';
44
import path from 'path';
55
import { PATHS } from '../system/paths.js';
66
import { logger } from './logger.js';
7-
import { escapeRegExp } from './utils.js';
87

98
const GITHUB_REPO = 'GoogleCloudPlatform/cloud-sql-proxy';
109
const ASSET_NAME = 'cloud-sql-proxy.x64.exe';
@@ -39,23 +38,20 @@ export async function downloadProxy(version: string, targetPath: string = PATHS.
3938

4039
try {
4140
const releaseUrl = `https://api.github.com/repos/${GITHUB_REPO}/releases/tags/${version}`;
42-
const response = await axios.get(releaseUrl);
41+
await axios.get(releaseUrl);
4342

4443
// Google Cloud SQL Proxy v2 binaries are hosted on GCS
4544
downloadUrl = `https://storage.googleapis.com/cloud-sql-connectors/cloud-sql-proxy/${version}/${ASSET_NAME}`;
4645

47-
// Extract checksum from release body
48-
const { body } = response.data;
49-
// Regex to match: | [cloud-sql-proxy.x64.exe](...) | <hash> |
50-
const escapedAssetName = escapeRegExp(ASSET_NAME);
51-
const checksumRegex = new RegExp(`\\| \\[${escapedAssetName}\\]\\(.*?\\) \\| ([a-f0-9]{64}) \\|`);
52-
const match = body.match(checksumRegex);
53-
54-
if (match && match[1]) {
55-
expectedChecksum = match[1];
56-
} else {
57-
logger.warn(`Could not extract checksum for ${ASSET_NAME} from release notes.`);
46+
// Fetch checksum from deterministic GCS sidecar file
47+
const checksumUrl = `${downloadUrl}.sha256`;
48+
const checksumResponse = await axios.get(checksumUrl, { responseType: 'text' });
49+
const checksumText = String(checksumResponse.data).trim();
50+
const checksumMatch = checksumText.match(/[a-f0-9]{64}/i);
51+
if (!checksumMatch) {
52+
throw new Error(`Checksum file did not contain a valid SHA256 hash (${checksumUrl})`);
5853
}
54+
expectedChecksum = checksumMatch[0];
5955

6056
logger.info(`Downloading ${ASSET_NAME} from ${downloadUrl}...`);
6157

@@ -78,23 +74,19 @@ export async function downloadProxy(version: string, targetPath: string = PATHS.
7874

7975
logger.info('Download complete.');
8076

81-
if (expectedChecksum) {
82-
logger.info('Verifying checksum...');
83-
try {
84-
const isValid = await verifyChecksum(targetPath, expectedChecksum);
85-
86-
if (!isValid) {
87-
throw new Error('Checksum verification failed');
88-
}
89-
logger.info('Checksum verified.');
90-
} catch (err) {
91-
logger.warn('Failed to verify checksum', err);
92-
// If verification fails, we should probably remove the file
93-
await fs.remove(targetPath);
94-
throw err;
77+
logger.info('Verifying checksum...');
78+
try {
79+
const isValid = await verifyChecksum(targetPath, expectedChecksum);
80+
81+
if (!isValid) {
82+
throw new Error('Checksum verification failed');
9583
}
96-
} else {
97-
logger.warn('Skipping checksum verification (checksum not found).');
84+
logger.info('Checksum verified.');
85+
} catch (err) {
86+
logger.warn('Failed to verify checksum', err);
87+
// If verification fails, we should probably remove the file
88+
await fs.remove(targetPath);
89+
throw err;
9890
}
9991

10092
} catch (error) {

0 commit comments

Comments
 (0)