Skip to content

Commit

Permalink
Enforces sha512 in the cache (#7591)
Browse files Browse the repository at this point in the history
* Always store sha512 in the cache

* Bumps the cache

* Fixes error reporting
  • Loading branch information
arcanis committed Oct 2, 2019
1 parent 5119828 commit e7cc86b
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/constants.js
Expand Up @@ -28,7 +28,7 @@ export const YARN_INSTALLER_MSI = 'https://yarnpkg.com/latest.msi';
export const SELF_UPDATE_VERSION_URL = 'https://yarnpkg.com/latest-version';

// cache version, bump whenever we make backwards incompatible changes
export const CACHE_VERSION = 5;
export const CACHE_VERSION = 6;

// lockfile version, bump whenever we make backwards incompatible changes
export const LOCKFILE_VERSION = 1;
Expand Down
6 changes: 4 additions & 2 deletions src/fetchers/tarball-fetcher.js
Expand Up @@ -166,9 +166,11 @@ export default class TarballFetcher extends BaseFetcher {
this.remote.integrity !== this.validateIntegrity.toString()
) {
this.remote.integrity = this.validateIntegrity.toString();
} else if (this.validateIntegrity) {
this.remote.cacheIntegrity = this.validateIntegrity.toString();
}

if (integrityInfo.algorithms.length === 0) {
if (integrityInfo.integrity && Object.keys(integrityInfo.integrity).length === 0) {
return reject(
new SecurityError(
this.config.reporter.lang('fetchBadIntegrityAlgorithm', this.packageName, this.remote.reference),
Expand Down Expand Up @@ -350,7 +352,7 @@ export default class TarballFetcher extends BaseFetcher {
return {integrity: null, algorithms};
}

const algorithms = new Set();
const algorithms = new Set(['sha512']);
const integrity = {};
for (const algorithm of expectedIntegrityAlgorithms) {
if (isHashAlgorithmSupported(algorithm)) {
Expand Down
7 changes: 5 additions & 2 deletions src/package-fetcher.js
Expand Up @@ -20,15 +20,18 @@ async function fetchCache(
// $FlowFixMe: This error doesn't make sense
const {hash, package: pkg, remote: cacheRemote} = await config.readPackageMetadata(dest);

const cacheIntegrity = cacheRemote.cacheIntegrity || cacheRemote.integrity;
const cacheHash = cacheRemote.hash;

if (remote.integrity) {
if (!cacheRemote.integrity || !ssri.parse(remote.integrity).match(cacheRemote.integrity)) {
if (!cacheIntegrity || !ssri.parse(cacheIntegrity).match(remote.integrity)) {
// eslint-disable-next-line yarn-internal/warn-language
throw new MessageError('Incorrect integrity when fetching from the cache');
}
}

if (remote.hash) {
if (!cacheRemote.hash || cacheRemote.hash !== remote.hash) {
if (!cacheHash || cacheHash !== remote.hash) {
// eslint-disable-next-line yarn-internal/warn-language
throw new MessageError('Incorrect integrity when fetching from the cache');
}
Expand Down
1 change: 1 addition & 0 deletions src/types.js
Expand Up @@ -56,6 +56,7 @@ export type PackageRemote = {
resolved?: ?string,
hash: ?string,
integrity?: ?string,
cacheIntegrity?: ?string,
packageName?: string,
registryRemote?: ?PackageRemote,
};
Expand Down

0 comments on commit e7cc86b

Please sign in to comment.