Skip to content

Commit

Permalink
process unexpected response from solidityscan
Browse files Browse the repository at this point in the history
  • Loading branch information
isstuev committed Aug 8, 2024
1 parent cec56f1 commit 403355d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 31 deletions.
39 changes: 21 additions & 18 deletions types/api/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,29 +104,32 @@ export interface SmartContractVerificationError {
name?: Array<string>;
}

// it's an external API proxy, we can't guarantee the responce types
export type SolidityscanReport = {
scan_report: {
contractname: string;
scan_status: string;
scan_summary: {
issue_severity_distribution: {
critical: number;
gas: number;
high: number;
informational: number;
low: number;
medium: number;
};
lines_analyzed_count: number;
scan_time_taken: number;
score: string;
score_v2: string;
threat_score: string;
scan_report?: {
contractname?: string;
scan_status?: string;
scan_summary?: {
issue_severity_distribution?: SolidityscanReportSeverityDistribution;
lines_analyzed_count?: number;
scan_time_taken?: number;
score?: string;
score_v2?: string;
threat_score?: string;
};
scanner_reference_url: string;
scanner_reference_url?: string;
};
}

export type SolidityscanReportSeverityDistribution = {
critical?: number;
gas?: number;
high?: number;
informational?: number;
low?: number;
medium?: number;
};

type SmartContractSecurityAudit = {
audit_company_name: string;
audit_publish_date: string;
Expand Down
4 changes: 2 additions & 2 deletions types/client/marketplace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SolidityscanReport } from 'types/api/contract';
import type { SolidityscanReport, SolidityscanReportSeverityDistribution } from 'types/api/contract';

export type MarketplaceAppPreview = {
id: string;
Expand Down Expand Up @@ -54,7 +54,7 @@ export type MarketplaceAppSecurityReport = {
solidityScanContractsNumber: number;
securityScore: number;
totalIssues?: number;
issueSeverityDistribution: SolidityscanReport['scan_report']['scan_summary']['issue_severity_distribution'];
issueSeverityDistribution: SolidityscanReportSeverityDistribution;
};
contractsData: Array<{
address: string;
Expand Down
10 changes: 7 additions & 3 deletions ui/address/SolidityscanReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ const SolidityscanReport = ({ hash }: Props) => {
},
});

const score = Number(data?.scan_report.scan_summary.score_v2);
if (isError || !data?.scan_report?.scan_summary) {
return null;
}

const score = Number(data.scan_report.scan_summary.score_v2);

if (isError || !score) {
if (!score) {
return null;
}

const vulnerabilities = data?.scan_report.scan_summary.issue_severity_distribution;
const vulnerabilities = data.scan_report.scan_summary.issue_severity_distribution;
const vulnerabilitiesCounts = vulnerabilities ? Object.values(vulnerabilities) : [];
const vulnerabilitiesCount = vulnerabilitiesCounts.reduce((acc, val) => acc + val, 0);

Expand Down
2 changes: 1 addition & 1 deletion ui/marketplace/ContractListModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const ContractListModal = ({ onClose, onBack, type, contracts }: Props) => {
return contracts
.filter((contract) => Boolean(contract.solidityScanReport))
.sort((a, b) =>
(parseFloat(b.solidityScanReport?.scan_summary.score_v2 ?? '0')) - (parseFloat(a.solidityScanReport?.scan_summary.score_v2 ?? '0')),
(parseFloat(b.solidityScanReport?.scan_summary?.score_v2 ?? '0')) - (parseFloat(a.solidityScanReport?.scan_summary?.score_v2 ?? '0')),
);
case ContractListTypes.VERIFIED:
return contracts.filter((contract) => contract.isVerified);
Expand Down
2 changes: 1 addition & 1 deletion ui/marketplace/ContractSecurityReport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const ContractSecurityReport = ({ securityReport }: Props) => {
onToggle();
}, [ onToggle ]);

if (!securityReport) {
if (!securityReport?.scan_summary?.score_v2) {
return null;
}

Expand Down
17 changes: 11 additions & 6 deletions ui/shared/solidityscanReport/SolidityscanReportDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Box, Flex, Text, Grid, useColorModeValue, chakra } from '@chakra-ui/react';
import React from 'react';

import type { SolidityscanReport } from 'types/api/contract';
import type { SolidityscanReportSeverityDistribution } from 'types/api/contract';

type DistributionItem = {
id: keyof SolidityscanReport['scan_report']['scan_summary']['issue_severity_distribution'];
id: keyof SolidityscanReportSeverityDistribution;
name: string;
color: string;
}
Expand All @@ -19,29 +19,34 @@ const DISTRIBUTION_ITEMS: Array<DistributionItem> = [
];

interface Props {
vulnerabilities: SolidityscanReport['scan_report']['scan_summary']['issue_severity_distribution'];
vulnerabilities: SolidityscanReportSeverityDistribution;
vulnerabilitiesCount: number;
}

type ItemProps = {
item: DistributionItem;
vulnerabilities: SolidityscanReport['scan_report']['scan_summary']['issue_severity_distribution'];
vulnerabilities: SolidityscanReportSeverityDistribution;
vulnerabilitiesCount: number;
}

const SolidityScanReportItem = ({ item, vulnerabilities, vulnerabilitiesCount }: ItemProps) => {
const bgBar = useColorModeValue('blackAlpha.50', 'whiteAlpha.50');
const yetAnotherGrayColor = useColorModeValue('gray.400', 'gray.500');
const vulnerability = vulnerabilities[item.id];

if (vulnerability === undefined) {
return null;
}

return (
<>
<Box w={ 3 } h={ 3 } bg={ item.color } borderRadius="6px" mr={ 2 }></Box>
<Flex justifyContent="space-between" mr={ 3 }>
<Text>{ item.name }</Text>
<Text color={ vulnerabilities[item.id] > 0 ? 'text' : yetAnotherGrayColor }>{ vulnerabilities[item.id] }</Text>
<Text color={ vulnerability > 0 ? 'text' : yetAnotherGrayColor }>{ vulnerabilities[item.id] }</Text>
</Flex>
<Box bg={ bgBar } h="10px" borderRadius="8px">
<Box bg={ item.color } w={ vulnerabilities[item.id] / vulnerabilitiesCount } h="10px" borderRadius="8px"/>
<Box bg={ item.color } w={ vulnerability / vulnerabilitiesCount } h="10px" borderRadius="8px"/>
</Box>
</>
);
Expand Down

0 comments on commit 403355d

Please sign in to comment.