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
25 changes: 18 additions & 7 deletions packages/snaps-controllers/src/snaps/SnapController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3943,17 +3943,26 @@ export class SnapController extends BaseController<
const { conversions: requestedConversions } = requestedParams;

const filteredConversionRates = requestedConversions.reduce<
Record<CaipAssetType, Record<CaipAssetType, AssetConversion>>
Map<CaipAssetType, Map<CaipAssetType, AssetConversion>>
>((accumulator, conversion) => {
const rate = conversionRates[conversion.from]?.[conversion.to];
// Only include rates that were actually requested.
if (rate) {
accumulator[conversion.from] ??= {};
accumulator[conversion.from][conversion.to] = rate;
if (!accumulator.has(conversion.from)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The results processed in this function come from BaseSnapExecutor, where the result is passed through getSafeJson:

This function eliminates any disallowed properties like __proto__

accumulator.set(conversion.from, new Map());
}
accumulator.get(conversion.from).set(conversion.to, rate);
}
return accumulator;
}, {});
return { conversionRates: filteredConversionRates };
}, new Map());
// Convert nested Maps into plain objects for return value compatibility.
const conversionRatesObj = Object.fromEntries(
Array.from(filteredConversionRates.entries()).map(([fromKey, toMap]) => [
fromKey,
Object.fromEntries(toMap.entries()),
]),
);
return { conversionRates: conversionRatesObj };
}

/**
Expand All @@ -3980,11 +3989,13 @@ export class SnapController extends BaseController<
const result = marketData[assets.asset]?.[assets.unit];
// Only include rates that were actually requested.
if (result) {
accumulator[assets.asset] ??= {};
if (!accumulator[assets.asset]) {
accumulator[assets.asset] = Object.create(null);
}
accumulator[assets.asset][assets.unit] = result;
}
return accumulator;
}, {});
}, Object.create(null));
return { marketData: filteredMarketData };
}

Expand Down
10 changes: 8 additions & 2 deletions packages/snaps-rpc-methods/src/permitted/setState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,15 @@ export function set(

for (let i = 0; i < keys.length; i++) {
const currentKey = keys[i];
if (FORBIDDEN_KEYS.includes(currentKey)) {
// Explicitly block prototype pollution keys
if (
FORBIDDEN_KEYS.includes(currentKey) ||
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FORBIDDEN_KEYS already include __proto__ etc.

currentKey === '__proto__' ||
currentKey === 'constructor' ||
currentKey === 'prototype'
) {
throw rpcErrors.invalidParams(
'Invalid params: Key contains forbidden characters.',
'Invalid params: Key contains forbidden characters or is potentially prototype polluting.',
);
}

Expand Down