Skip to content

Commit

Permalink
fix: check contract source whitelist before loading interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tadeuchi committed Nov 17, 2023
1 parent 9e8575b commit 62e1cff
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/contract/HandlerBasedContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ export class HandlerBasedContract<State> implements Contract<State> {
}
}

this.warp.executorFactory.checkWhiteListContractSources(contractDefinition, contractEvaluationOptions);

if (!remoteState && sortedInteractions.length == 0) {
sortedInteractions = await interactionsLoader.load(
contractTxId,
Expand Down
7 changes: 6 additions & 1 deletion src/core/modules/ExecutorFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContractDefinition } from '../../core/ContractDefinition';
import { ContractCache, ContractDefinition, SrcCache } from '../../core/ContractDefinition';
import { EvaluationOptions } from './StateEvaluator';
import { Warp } from '../Warp';
import { InteractionState } from '../../contract/states/InteractionState';
Expand All @@ -17,4 +17,9 @@ export interface ExecutorFactory<Api> {
warp: Warp,
interactionState: InteractionState
): Promise<Api>;

checkWhiteListContractSources<State>(
contractDefinition: SrcCache & ContractCache<State>,
evaluationOptions: EvaluationOptions
): void;
}
27 changes: 17 additions & 10 deletions src/core/modules/impl/HandlerExecutorFactory.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Arweave from 'arweave';
import { rustWasmImports, WarpContractsCrateVersion } from './wasm/rust-wasm-imports';
import { ContractDefinition } from '../../../core/ContractDefinition';
import { ContractCache, ContractDefinition, SrcCache } from '../../../core/ContractDefinition';
import { ExecutionContext } from '../../../core/ExecutionContext';
import { GQLNodeInterface } from '../../../legacy/gqlResult';
import { SmartWeaveGlobal } from '../../../legacy/smartweave-global';
Expand Down Expand Up @@ -54,15 +54,7 @@ export class HandlerExecutorFactory implements ExecutorFactory<HandlerApi<unknow
if (warp.hasPlugin('contract-blacklist')) {
await this.blacklistContracts<State>(warp, contractDefinition);
}

if (
evaluationOptions.whitelistSources.length > 0 &&
!evaluationOptions.whitelistSources.includes(contractDefinition.srcTxId)
) {
throw new NonWhitelistedSourceError(
`[NonWhitelistedSourceError] Contract source not part of whitelisted sources list: ${contractDefinition.srcTxId}.`
);
}
this.checkWhiteListContractSources(contractDefinition, evaluationOptions);

let kvStorage = null;

Expand Down Expand Up @@ -219,6 +211,21 @@ export class HandlerExecutorFactory implements ExecutorFactory<HandlerApi<unknow
}
}

checkWhiteListContractSources<State>(
contractDefinition: SrcCache & ContractCache<State>,
evaluationOptions: EvaluationOptions
) {
if (
evaluationOptions &&
evaluationOptions.whitelistSources.length > 0 &&
!evaluationOptions.whitelistSources.includes(contractDefinition.srcTxId)
) {
throw new NonWhitelistedSourceError(
`[NonWhitelistedSourceError] Contract source not part of whitelisted sources list: ${contractDefinition.srcTxId}.`
);
}
}

private async blacklistContracts<State>(warp: Warp, contractDefinition: ContractDefinition<State>) {
const blacklistPlugin = warp.loadPlugin<string, Promise<boolean>>('contract-blacklist');
let blacklisted = false;
Expand Down
9 changes: 8 additions & 1 deletion src/plugins/DebuggableExecutorFactor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ContractDefinition } from '../core/ContractDefinition';
import { ContractCache, ContractDefinition, SrcCache } from '../core/ContractDefinition';
import { ExecutorFactory } from '../core/modules/ExecutorFactory';
import { EvaluationOptions } from '../core/modules/StateEvaluator';
import { Warp } from '../core/Warp';
Expand Down Expand Up @@ -33,4 +33,11 @@ export class DebuggableExecutorFactory<Api> implements ExecutorFactory<Api> {

return await this.baseImplementation.create(contractDefinition, evaluationOptions, warp, interactionState);
}

checkWhiteListContractSources<State>(
contractDefinition: SrcCache & ContractCache<State>,
evaluationOptions: EvaluationOptions
): void {
return this.baseImplementation.checkWhiteListContractSources(contractDefinition, evaluationOptions);
}
}

0 comments on commit 62e1cff

Please sign in to comment.