Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process unexpected response from solidityscan #2148

Merged
merged 1 commit into from
Aug 13, 2024
Merged
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
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
Loading