|
| 1 | +import { Modal, Title, Content } from '@node-core/ui-components/Common/Modal'; |
| 2 | +import classNames from 'classnames'; |
| 3 | +import { useTranslations } from 'next-intl'; |
| 4 | +import type { FC } from 'react'; |
| 5 | + |
| 6 | +import VulnerabilityChip from '#site/components/EOL/VulnerabilityChips/Chip'; |
| 7 | +import LinkWithArrow from '#site/components/LinkWithArrow'; |
| 8 | +import type { ModalProps } from '#site/providers/modalProvider'; |
| 9 | +import type { NodeRelease } from '#site/types'; |
| 10 | +import type { Vulnerability } from '#site/types/vulnerabilities'; |
| 11 | + |
| 12 | +import { SEVERITY_ORDER } from './VulnerabilityChips'; |
| 13 | + |
| 14 | +type EOLModalData = { |
| 15 | + release: NodeRelease; |
| 16 | + vulnerabilities: Array<Vulnerability>; |
| 17 | +}; |
| 18 | + |
| 19 | +type KnownVulnerability = Vulnerability & { |
| 20 | + severity: (typeof SEVERITY_ORDER)[number]; |
| 21 | +}; |
| 22 | + |
| 23 | +const VulnerabilitiesTable: FC<{ |
| 24 | + vulnerabilities: Array<Vulnerability>; |
| 25 | + maxWidth?: string; |
| 26 | +}> = ({ vulnerabilities, maxWidth = 'max-w-2xs' }) => { |
| 27 | + const t = useTranslations('components.eolModal'); |
| 28 | + |
| 29 | + return ( |
| 30 | + <table className="w-full"> |
| 31 | + <thead> |
| 32 | + <tr> |
| 33 | + <th>{t('table.cves')}</th> |
| 34 | + <th>{t('table.severity')}</th> |
| 35 | + <th>{t('table.overview')}</th> |
| 36 | + <th>{t('table.details')}</th> |
| 37 | + </tr> |
| 38 | + </thead> |
| 39 | + <tbody> |
| 40 | + {vulnerabilities.map((vuln, i) => ( |
| 41 | + <tr key={i}> |
| 42 | + <td> |
| 43 | + {vuln.cve.length |
| 44 | + ? vuln.cve.map(cveId => ( |
| 45 | + <div key={cveId}> |
| 46 | + <LinkWithArrow |
| 47 | + href={`https://cve.mitre.org/cgi-bin/cvename.cgi?name=${cveId}`} |
| 48 | + target="_blank" |
| 49 | + rel="noopener noreferrer" |
| 50 | + > |
| 51 | + {cveId} |
| 52 | + </LinkWithArrow> |
| 53 | + </div> |
| 54 | + )) |
| 55 | + : '-'} |
| 56 | + </td> |
| 57 | + <td> |
| 58 | + <VulnerabilityChip severity={vuln.severity} /> |
| 59 | + </td> |
| 60 | + <td className={classNames(maxWidth, 'truncate')}> |
| 61 | + {vuln.description || vuln.overview || '-'} |
| 62 | + </td> |
| 63 | + <td> |
| 64 | + {vuln.ref ? ( |
| 65 | + <LinkWithArrow |
| 66 | + href={vuln.ref} |
| 67 | + target="_blank" |
| 68 | + rel="noopener noreferrer" |
| 69 | + > |
| 70 | + {t('blogLinkText')} |
| 71 | + </LinkWithArrow> |
| 72 | + ) : ( |
| 73 | + '—' |
| 74 | + )} |
| 75 | + </td> |
| 76 | + </tr> |
| 77 | + ))} |
| 78 | + </tbody> |
| 79 | + </table> |
| 80 | + ); |
| 81 | +}; |
| 82 | + |
| 83 | +const UnknownSeveritySection: FC<{ |
| 84 | + vulnerabilities: Array<Vulnerability>; |
| 85 | + hasKnownVulns: boolean; |
| 86 | +}> = ({ vulnerabilities, hasKnownVulns }) => { |
| 87 | + const t = useTranslations('components.eolModal'); |
| 88 | + |
| 89 | + if (!vulnerabilities.length) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + return ( |
| 94 | + <details open={!hasKnownVulns}> |
| 95 | + <summary className="cursor-pointer font-semibold"> |
| 96 | + {t('showUnknownSeverities')} ({vulnerabilities.length}) |
| 97 | + </summary> |
| 98 | + <div className="mt-4"> |
| 99 | + <VulnerabilitiesTable |
| 100 | + vulnerabilities={vulnerabilities} |
| 101 | + maxWidth={'max-w-3xs'} |
| 102 | + /> |
| 103 | + </div> |
| 104 | + </details> |
| 105 | + ); |
| 106 | +}; |
| 107 | + |
| 108 | +const EOLModal: FC<ModalProps> = ({ open, closeModal, data }) => { |
| 109 | + const { release, vulnerabilities } = data as EOLModalData; |
| 110 | + const t = useTranslations('components.eolModal'); |
| 111 | + |
| 112 | + const modalHeading = t(release.codename ? 'title' : 'titleWithoutCodename', { |
| 113 | + version: release.major, |
| 114 | + codename: release.codename ?? '', |
| 115 | + }); |
| 116 | + |
| 117 | + const [knownVulns, unknownVulns] = vulnerabilities.reduce( |
| 118 | + (acc, vuln) => { |
| 119 | + acc[vuln.severity === 'unknown' ? 1 : 0].push(vuln as KnownVulnerability); |
| 120 | + return acc; |
| 121 | + }, |
| 122 | + [[], []] as [Array<KnownVulnerability>, Array<Vulnerability>] |
| 123 | + ); |
| 124 | + |
| 125 | + knownVulns.sort( |
| 126 | + (a, b) => |
| 127 | + SEVERITY_ORDER.indexOf(a.severity) - SEVERITY_ORDER.indexOf(b.severity) |
| 128 | + ); |
| 129 | + |
| 130 | + const hasKnownVulns = knownVulns.length > 0; |
| 131 | + const hasAnyVulns = hasKnownVulns || unknownVulns.length > 0; |
| 132 | + |
| 133 | + return ( |
| 134 | + <Modal open={open} onOpenChange={closeModal}> |
| 135 | + <Title>{modalHeading}</Title> |
| 136 | + <Content> |
| 137 | + {vulnerabilities.length > 0 && ( |
| 138 | + <p className="m-1"> |
| 139 | + {t('vulnerabilitiesMessage', { count: vulnerabilities.length })} |
| 140 | + </p> |
| 141 | + )} |
| 142 | + |
| 143 | + {hasKnownVulns && <VulnerabilitiesTable vulnerabilities={knownVulns} />} |
| 144 | + |
| 145 | + <UnknownSeveritySection |
| 146 | + vulnerabilities={unknownVulns} |
| 147 | + hasKnownVulns={hasKnownVulns} |
| 148 | + /> |
| 149 | + |
| 150 | + {!hasAnyVulns && <p className="m-1">{t('noVulnerabilitiesMessage')}</p>} |
| 151 | + </Content> |
| 152 | + </Modal> |
| 153 | + ); |
| 154 | +}; |
| 155 | + |
| 156 | +export default EOLModal; |
0 commit comments