Skip to content
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

CLI: Disallow self-references for storage layout validations #1067

Merged
merged 11 commits into from
Aug 28, 2024
4 changes: 4 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- **Breaking change**: CLI: Disallow self-references for storage layout validations. ([#1067](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/1067))

## 1.36.0 (2024-08-21)

- Update dependency on Slang. ([#1059](https://github.com/OpenZeppelin/openzeppelin-upgrades/pull/1059))
Expand Down
16 changes: 16 additions & 0 deletions packages/core/contracts/test/cli/SelfReferences.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.8;

/// @custom:oz-upgrades-from SelfReference
contract SelfReference {
uint public x;
}

/// @custom:oz-upgrades-from contracts/test/cli/SelfReferences.sol:SelfReferenceFullyQualified
contract SelfReferenceFullyQualified {
uint public x;
}

contract NoAnnotation {
uint public x;
}
42 changes: 42 additions & 0 deletions packages/core/src/cli/cli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,3 +572,45 @@ test('validate - contract must not have build info dir name - fully qualified',
error?.message,
);
});

test('validate - self reference by annotation', async t => {
const temp = await getTempDir(t);
const buildInfo = await artifacts.getBuildInfo(`contracts/test/cli/SelfReferences.sol:SelfReference`);
await fs.writeFile(path.join(temp, 'validate.json'), JSON.stringify(buildInfo));

const error = await t.throwsAsync(execAsync(`${CLI} validate ${temp} --contract SelfReference`));
t.assert(error?.message.includes('must not use itself as a reference'), error?.message);
});

test('validate - self reference by fully qualified annotation', async t => {
const temp = await getTempDir(t);
const buildInfo = await artifacts.getBuildInfo(`contracts/test/cli/SelfReferences.sol:SelfReference`);
await fs.writeFile(path.join(temp, 'validate.json'), JSON.stringify(buildInfo));

const error = await t.throwsAsync(execAsync(`${CLI} validate ${temp} --contract SelfReferenceFullyQualified`));
t.assert(error?.message.includes('must not use itself as a reference'), error?.message);
});

test('validate - self reference by option', async t => {
const temp = await getTempDir(t);
const buildInfo = await artifacts.getBuildInfo(`contracts/test/cli/SelfReferences.sol:SelfReference`);
await fs.writeFile(path.join(temp, 'validate.json'), JSON.stringify(buildInfo));

const error = await t.throwsAsync(
execAsync(`${CLI} validate ${temp} --contract NoAnnotation --reference NoAnnotation`),
);
t.assert(error?.message.includes('must not use itself as a reference'), error?.message);
});

test('validate - self reference by fully qualified option', async t => {
const temp = await getTempDir(t);
const buildInfo = await artifacts.getBuildInfo(`contracts/test/cli/SelfReferences.sol:SelfReference`);
await fs.writeFile(path.join(temp, 'validate.json'), JSON.stringify(buildInfo));

const error = await t.throwsAsync(
execAsync(
`${CLI} validate ${temp} --contract NoAnnotation --reference contracts/test/cli/SelfReferences.sol:NoAnnotation`,
),
);
t.assert(error?.message.includes('must not use itself as a reference'), error?.message);
});
21 changes: 15 additions & 6 deletions packages/core/src/cli/validate/contract-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
getContractVersion,
getStorageLayout,
ValidationOptions,
withValidationDefaults,
Version,
ValidationData,
ValidateUpgradeSafetyOptions,
Expand All @@ -18,6 +17,7 @@ import { SourceContract } from './validations';
import { LayoutCompatibilityReport } from '../../storage/report';
import { indent } from '../../utils/indent';
import { BuildInfoDictionary, SpecifiedContracts } from './validate-upgrade-safety';
import { ValidateCommandError } from './error';

/**
* Report for an upgradeable contract.
Expand All @@ -29,7 +29,16 @@ export class UpgradeableContractReport implements Report {
readonly reference: string | undefined,
readonly standaloneReport: UpgradeableContractErrorReport,
readonly storageLayoutReport: LayoutCompatibilityReport | undefined,
) {}
) {
if (reference === contract) {
throw new ValidateCommandError(
`The contract ${contract} must not use itself as a reference for storage layout comparisons.`,
() => `\
If this is the first version of your contract, do not specify a reference.
If this is a subsequent version, keep the previous version of the contract in another file and specify that as the reference, or specify a reference from another build info directory containing the previous version. If you do not have the previous version available, you can skip the storage layout check using the \`unsafeSkipStorageCheck\` option, which is a dangerous option meant to be used as a last resort.`,
);
}
}

get ok(): boolean {
return this.standaloneReport.ok && (this.storageLayoutReport === undefined || this.storageLayoutReport.ok);
Expand Down Expand Up @@ -103,7 +112,7 @@ export function getContractReports(
function getUpgradeableContractReport(
contract: SourceContract,
referenceContract: SourceContract | undefined,
opts: ValidationOptions,
opts: Required<ValidationOptions>,
): UpgradeableContractReport | undefined {
let version;
try {
Expand Down Expand Up @@ -135,7 +144,7 @@ function getUpgradeableContractReport(
} else {
reference = referenceContract.fullyQualifiedName;
}
storageLayoutReport = getStorageUpgradeReport(referenceLayout, layout, withValidationDefaults(opts));
storageLayoutReport = getStorageUpgradeReport(referenceLayout, layout, opts);
}

return new UpgradeableContractReport(contract.fullyQualifiedName, reference, standaloneReport, storageLayoutReport);
Expand All @@ -144,8 +153,8 @@ function getUpgradeableContractReport(
function getStandaloneReport(
data: ValidationData,
version: Version,
opts: ValidationOptions,
opts: Required<ValidationOptions>,
): UpgradeableContractErrorReport {
const errors = getErrors(data, version, withValidationDefaults(opts));
const errors = getErrors(data, version, opts);
return new UpgradeableContractErrorReport(errors);
}
Loading