Skip to content

Commit

Permalink
hotfix(builder): add preset length validation (#1705)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjlescano authored Feb 20, 2025
1 parent 714bd20 commit f6ffc9c
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 20 deletions.
2 changes: 1 addition & 1 deletion packages/builder/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default {
// moduleNameMapper: {},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
// modulePathIgnorePatterns: [],
modulePathIgnorePatterns: ['/dist/'],

// Activates notifications for test results
// notify: false,
Expand Down
33 changes: 21 additions & 12 deletions packages/builder/src/package-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export class PackageReference {
static DEFAULT_TAG = 'latest';
static DEFAULT_PRESET = 'main';
static PACKAGE_REGEX = /^(?<name>[a-z0-9][A-Za-z0-9-]{1,}[a-z0-9])(?::(?<version>[^@]+))?(@(?<preset>[^\s]+))?$/;

static VARIANT_REGEX = /^(?<chainId>\d+)-(?<preset>[^\s]+)$/;
/**
* Anything before the colon or an @ (if no version is present) is the package name.
*/
Expand Down Expand Up @@ -55,19 +55,23 @@ export class PackageReference {

const res: PartialRefValues = { name: match.groups.name };

const nameSize = res.name.length;
if (nameSize > 32) {
throw new Error(`Package reference "${ref}" is too long. Package name exceeds 32 bytes`);
if (res.name.length > 32) {
throw new Error(`Package name for "${ref}" is too long. Package name exceeds 32 characters`);
}

if (match.groups.version) res.version = match.groups.version;

const versionSize = res.version?.length || 0;
if (versionSize > 32) {
throw new Error(`Package reference "${ref}" is too long. Package version exceeds 32 bytes`);
if (match.groups.version) {
res.version = match.groups.version;
if (res.version.length > 32) {
throw new Error(`Package version for "${ref}" is too long. Package version exceeds 32 characters`);
}
}

if (match.groups.preset) res.preset = match.groups.preset;
if (match.groups.preset) {
res.preset = match.groups.preset;
if (res.preset.length > 24) {
throw new Error(`Package preset for "${ref}" is too long. Package preset exceeds 24 characters`);
}
}

return res;
}
Expand All @@ -93,8 +97,13 @@ export class PackageReference {
* @returns chainId and preset
*/
static parseVariant(variant: string): [number, string] {
const [chainId, preset] = variant.split(/-(.*)/s);
return [Number(chainId), preset];
const match = variant.match(PackageReference.VARIANT_REGEX);

if (!match || !match.groups?.chainId || !match.groups?.preset) {
throw new Error(`Invalid variant "${variant}". Should be of the format <chainId>-<preset>`);
}

return [Number(match.groups.chainId), match.groups.preset];
}

constructor(ref: string) {
Expand Down
6 changes: 6 additions & 0 deletions packages/builder/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,12 @@ export const chainDefinitionSchema = z
.refine((val) => !!val.match(RegExp(/[\w.]+/, 'gm')), {
message: 'Preset cannot contain any special characters',
})
.refine(
(val) => {
return new Blob([val]).size <= 24;
},
(val) => ({ message: `Package preset "${val}" is too long. Package preset exceeds 24 bytes` })
)
.describe(
'Preset of the package (Presets are useful for distinguishing multiple deployments of the same protocol on the same chain.) Defaults to "main".'
)
Expand Down
18 changes: 13 additions & 5 deletions packages/builder/src/steps/clone.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ describe('steps/clone.ts', () => {
{ source: 'undefined-deployment:1.0.0' },
{ ref: new PackageReference('package:1.0.0'), currentLabel: 'clone.whatever' }
)
).rejects.toThrowError('deployment not found');
).rejects.toThrow('deployment not found');
});

it('throws if source name is longer than 32 bytes', async () => {
Expand All @@ -119,7 +119,9 @@ describe('steps/clone.ts', () => {
{ source: 'package-name-longer-than-32bytes1337:1.0.0' },
{ ref: new PackageReference('package:1.0.0'), currentLabel: 'clone.whatever' }
)
).rejects.toThrowError('Package name exceeds 32 bytes');
).rejects.toThrow(
'Package name for "package-name-longer-than-32bytes1337:1.0.0" is too long. Package name exceeds 32 characters'
);
});

it('throws if source version is longer than 32 bytes', async () => {
Expand Down Expand Up @@ -172,7 +174,9 @@ describe('steps/clone.ts', () => {
{ source: 'package:package-version-longer-than-32bytes1337' },
{ ref: new PackageReference('package:1.0.0'), currentLabel: 'clone.whatever' }
)
).rejects.toThrowError('Package version exceeds 32 bytes');
).rejects.toThrow(
'Package version for "package:package-version-longer-than-32bytes1337" is too long. Package version exceeds 32 characters'
);
});

it('throws if target name is longer than 32 bytes', async () => {
Expand All @@ -183,7 +187,9 @@ describe('steps/clone.ts', () => {
{ source: 'package:1.0.0', target: 'package-name-longer-than-32bytes1337:1.0.0' },
{ ref: new PackageReference('package:1.0.0'), currentLabel: 'clone.whatever' }
)
).rejects.toThrowError('Package name exceeds 32 bytes');
).rejects.toThrow(
'Package name for "package-name-longer-than-32bytes1337:1.0.0" is too long. Package name exceeds 32 characters'
);
});

it('throws if target version is longer than 32 bytes', async () => {
Expand Down Expand Up @@ -236,7 +242,9 @@ describe('steps/clone.ts', () => {
{ source: 'package:1.0.0', target: 'package:package-version-longer-than-32bytes1337' },
{ ref: new PackageReference('package:1.0.0'), currentLabel: 'clone.whatever' }
)
).rejects.toThrowError('Package version exceeds 32 bytes');
).rejects.toThrow(
'Package version for "package:package-version-longer-than-32bytes1337" is too long. Package version exceeds 32 characters'
);
});

it('returns partial deployment if runtime becomes cancelled', async () => {
Expand Down
8 changes: 6 additions & 2 deletions packages/builder/src/steps/pull.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ describe('steps/pull.ts', () => {
{ source: 'package-name-longer-than-32bytes1337:1.0.0' },
{ ref: new PackageReference('package:1.0.0'), currentLabel: 'clone.whatever' }
)
).rejects.toThrowError('Package name exceeds 32 bytes');
).rejects.toThrow(
'Package name for "package-name-longer-than-32bytes1337:1.0.0" is too long. Package name exceeds 32 characters'
);
});

it('throws if target version is longer than 32 bytes', async () => {
Expand Down Expand Up @@ -175,7 +177,9 @@ describe('steps/pull.ts', () => {
{ source: 'package:package-version-longer-than-32bytes1337' },
{ ref: new PackageReference('package:1.0.0'), currentLabel: 'pull.whatever' }
)
).rejects.toThrowError('Package version exceeds 32 bytes');
).rejects.toThrow(
'Package version for "package:package-version-longer-than-32bytes1337" is too long. Package version exceeds 32 characters'
);
});

it('works properly', async () => {
Expand Down

0 comments on commit f6ffc9c

Please sign in to comment.