Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/scanner-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"dependencies": {
"@veridion/logger": "workspace:*",
"@veridion/plugin-unchecked-return": "workspace:*",
"@veridion/scanner-types": "workspace:*",
"@veridion/shared": "workspace:*"
},
Expand Down
46 changes: 43 additions & 3 deletions packages/scanner-core/src/plugin-registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import type { IRulePlugin, PluginMetadata } from '@veridion/scanner-types';
import { FindingSeverity } from '@veridion/shared';
import { beforeEach, describe, expect, it } from 'vitest';

import { PluginRegistry } from './plugin-registry';
import { createDefaultRegistry, PluginRegistry } from './plugin-registry';
import { Scanner } from './scanner';

function createMockPlugin(
id: string,
Expand Down Expand Up @@ -67,8 +68,7 @@ describe('PluginRegistry', () => {
});

expect(matching).toHaveLength(1);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(matching[0]!.metadata.id).toBe('eth');
expect(matching[0]?.metadata.id).toBe('eth');
});

it('should unregister a plugin', () => {
Expand All @@ -85,3 +85,43 @@ describe('PluginRegistry', () => {
expect(allMeta).toHaveLength(2);
});
});

describe('createDefaultRegistry', () => {
it('should register unchecked-return plugin by default', () => {
const registry = createDefaultRegistry();
expect(registry.size).toBe(1);
const plugin = registry.get('unchecked-return');
expect(plugin).toBeDefined();
expect(plugin?.metadata.id).toBe('unchecked-return');
expect(plugin?.metadata.category).toBe('UNCHECKED_RETURN');
expect(plugin?.metadata.severity).toBe(FindingSeverity.HIGH);
});

it('should list unchecked-return in all metadata', () => {
const registry = createDefaultRegistry();
const ids = registry.getAllMetadata().map((m) => m.id);
expect(ids).toContain('unchecked-return');
});

it('should execute end-to-end scan through Scanner', async () => {
const registry = createDefaultRegistry();
const scanner = new Scanner(registry);
const result = await scanner.scan({
contractName: 'TestVault',
sourceCode: `
contract TestVault {
function withdraw(address payable recipient) public {
recipient.call("");
}
}`,
chain: 'ethereum',
language: 'solidity',
compilerVersion: '0.8.20',
metadata: {},
});

expect(result.findings).toHaveLength(1);
expect(result.findings[0]?.pluginId).toBe('unchecked-return');
expect(result.findings[0]?.lineStart).toBe(4);
});
});
13 changes: 13 additions & 0 deletions packages/scanner-core/src/plugin-registry.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { logger } from '@veridion/logger';
import { UncheckedReturnPlugin } from '@veridion/plugin-unchecked-return';
import type { AnalysisContext, IRulePlugin, PluginMetadata } from '@veridion/scanner-types';

export const defaultPlugins: IRulePlugin[] = [new UncheckedReturnPlugin()];

export class PluginRegistry {
private plugins = new Map<string, IRulePlugin>();

Expand All @@ -21,6 +24,10 @@ export class PluginRegistry {
}
}

registerDefaultPlugins(): void {
this.registerAll(defaultPlugins);
}

unregister(pluginId: string): boolean {
return this.plugins.delete(pluginId);
}
Expand Down Expand Up @@ -70,3 +77,9 @@ export class PluginRegistry {
return this.plugins.size;
}
}

export function createDefaultRegistry(): PluginRegistry {
const registry = new PluginRegistry();
registry.registerDefaultPlugins();
return registry;
}
4 changes: 4 additions & 0 deletions plugins/unchecked-return/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { createConfig } = require('@veridion/eslint-config/base');

/** @type {import('eslint').Linter.Config} */
module.exports = createConfig(__dirname);
28 changes: 28 additions & 0 deletions plugins/unchecked-return/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "@veridion/plugin-unchecked-return",
"version": "0.1.0",
"private": true,
"description": "Unchecked return value detection plugin for Veridion scanner",
"main": "./src/index.ts",
"types": "./src/index.ts",
"scripts": {
"build": "tsc",
"typecheck": "tsc --noEmit",
"lint": "eslint src/ --max-warnings 0",
"test": "vitest run",
"test:watch": "vitest",
"clean": "rm -rf dist"
},
"dependencies": {
"@veridion/logger": "workspace:*",
"@veridion/scanner-types": "workspace:*",
"@veridion/shared": "workspace:*"
},
"devDependencies": {
"@veridion/eslint-config": "workspace:*",
"@veridion/tsconfig": "workspace:*",
"eslint": "^8.57.0",
"typescript": "^5.4.5",
"vitest": "^1.6.0"
}
}
Loading
Loading