You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SecurityHotspotsApp.tsx 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { flatMap, range } from 'lodash';
  21. import * as React from 'react';
  22. import { getMeasures } from '../../api/measures';
  23. import { getSecurityHotspotList, getSecurityHotspots } from '../../api/security-hotspots';
  24. import withComponentContext from '../../app/components/componentContext/withComponentContext';
  25. import withCurrentUserContext from '../../app/components/current-user/withCurrentUserContext';
  26. import withIndexationGuard from '../../components/hoc/withIndexationGuard';
  27. import { Location, Router, withRouter } from '../../components/hoc/withRouter';
  28. import { getLeakValue } from '../../components/measure/utils';
  29. import { getBranchLikeQuery, isPullRequest, isSameBranchLike } from '../../helpers/branch-like';
  30. import { isInput } from '../../helpers/keyboardEventHelpers';
  31. import { KeyboardKeys } from '../../helpers/keycodes';
  32. import { getStandards } from '../../helpers/security-standard';
  33. import { withBranchLikes } from '../../queries/branch';
  34. import { BranchLike } from '../../types/branch-like';
  35. import { ComponentQualifier } from '../../types/component';
  36. import { MetricKey } from '../../types/metrics';
  37. import { SecurityStandard, Standards } from '../../types/security';
  38. import {
  39. HotspotFilters,
  40. HotspotResolution,
  41. HotspotStatus,
  42. HotspotStatusFilter,
  43. RawHotspot,
  44. } from '../../types/security-hotspots';
  45. import { Component, Dict } from '../../types/types';
  46. import { CurrentUser, isLoggedIn } from '../../types/users';
  47. import SecurityHotspotsAppRenderer from './SecurityHotspotsAppRenderer';
  48. import { SECURITY_STANDARDS, getLocations } from './utils';
  49. const PAGE_SIZE = 500;
  50. interface Props {
  51. branchLike?: BranchLike;
  52. currentUser: CurrentUser;
  53. component: Component;
  54. location: Location;
  55. router: Router;
  56. }
  57. interface State {
  58. filterByCategory?: { standard: SecurityStandard; category: string };
  59. filterByCWE?: string;
  60. filterByFile?: string;
  61. filters: HotspotFilters;
  62. hotspotKeys?: string[];
  63. hotspots: RawHotspot[];
  64. hotspotsPageIndex: number;
  65. hotspotsReviewedMeasure?: string;
  66. hotspotsTotal: number;
  67. loading: boolean;
  68. loadingMeasure: boolean;
  69. loadingMore: boolean;
  70. selectedHotspot?: RawHotspot;
  71. selectedHotspotLocationIndex?: number;
  72. standards: Standards;
  73. }
  74. export class SecurityHotspotsApp extends React.PureComponent<Props, State> {
  75. mounted = false;
  76. state: State;
  77. constructor(props: Props) {
  78. super(props);
  79. this.state = {
  80. filters: {
  81. ...this.constructFiltersFromProps(props),
  82. status: HotspotStatusFilter.TO_REVIEW,
  83. },
  84. hotspots: [],
  85. hotspotsPageIndex: 1,
  86. hotspotsTotal: 0,
  87. loading: true,
  88. loadingMeasure: false,
  89. loadingMore: false,
  90. selectedHotspot: undefined,
  91. standards: {
  92. [SecurityStandard.CWE]: {},
  93. [SecurityStandard.OWASP_ASVS_4_0]: {},
  94. [SecurityStandard.OWASP_TOP10_2021]: {},
  95. [SecurityStandard.OWASP_TOP10]: {},
  96. [SecurityStandard.PCI_DSS_3_2]: {},
  97. [SecurityStandard.PCI_DSS_4_0]: {},
  98. [SecurityStandard.SONARSOURCE]: {},
  99. },
  100. };
  101. }
  102. componentDidMount() {
  103. this.mounted = true;
  104. this.fetchInitialData();
  105. this.registerKeyboardEvents();
  106. }
  107. componentDidUpdate(previous: Props) {
  108. if (
  109. !isSameBranchLike(previous.branchLike, this.props.branchLike) ||
  110. this.props.component.key !== previous.component.key ||
  111. this.props.location.query.hotspots !== previous.location.query.hotspots ||
  112. SECURITY_STANDARDS.some((s) => this.props.location.query[s] !== previous.location.query[s]) ||
  113. this.props.location.query.files !== previous.location.query.files
  114. ) {
  115. this.fetchInitialData();
  116. }
  117. if (
  118. !isSameBranchLike(previous.branchLike, this.props.branchLike) ||
  119. isLoggedIn(this.props.currentUser) !== isLoggedIn(previous.currentUser) ||
  120. this.props.location.query.assignedToMe !== previous.location.query.assignedToMe ||
  121. this.props.location.query.inNewCodePeriod !== previous.location.query.inNewCodePeriod
  122. ) {
  123. this.setState(({ filters }) => ({
  124. filters: { ...this.constructFiltersFromProps, ...filters },
  125. }));
  126. }
  127. }
  128. componentWillUnmount() {
  129. this.unregisterKeyboardEvents();
  130. this.mounted = false;
  131. }
  132. registerKeyboardEvents() {
  133. document.addEventListener('keydown', this.handleKeyDown);
  134. }
  135. handleKeyDown = (event: KeyboardEvent) => {
  136. if (isInput(event)) {
  137. return;
  138. }
  139. if (event.key === KeyboardKeys.Alt) {
  140. event.preventDefault();
  141. return;
  142. }
  143. switch (event.key) {
  144. case KeyboardKeys.DownArrow: {
  145. event.preventDefault();
  146. if (event.altKey) {
  147. this.selectNextLocation();
  148. } else {
  149. this.selectNeighboringHotspot(+1);
  150. }
  151. break;
  152. }
  153. case KeyboardKeys.UpArrow: {
  154. event.preventDefault();
  155. if (event.altKey) {
  156. this.selectPreviousLocation();
  157. } else {
  158. this.selectNeighboringHotspot(-1);
  159. }
  160. break;
  161. }
  162. }
  163. };
  164. selectNextLocation = () => {
  165. const { selectedHotspotLocationIndex, selectedHotspot } = this.state;
  166. if (selectedHotspot === undefined) {
  167. return;
  168. }
  169. const locations = getLocations(selectedHotspot.flows, undefined);
  170. if (locations.length === 0) {
  171. return;
  172. }
  173. const lastIndex = locations.length - 1;
  174. let newIndex;
  175. if (selectedHotspotLocationIndex === undefined) {
  176. newIndex = 0;
  177. } else if (selectedHotspotLocationIndex === lastIndex) {
  178. newIndex = undefined;
  179. } else {
  180. newIndex = selectedHotspotLocationIndex + 1;
  181. }
  182. this.setState({ selectedHotspotLocationIndex: newIndex });
  183. };
  184. selectPreviousLocation = () => {
  185. const { selectedHotspotLocationIndex } = this.state;
  186. let newIndex;
  187. if (selectedHotspotLocationIndex === 0) {
  188. newIndex = undefined;
  189. } else if (selectedHotspotLocationIndex !== undefined) {
  190. newIndex = selectedHotspotLocationIndex - 1;
  191. }
  192. this.setState({ selectedHotspotLocationIndex: newIndex });
  193. };
  194. selectNeighboringHotspot = (shift: number) => {
  195. this.setState({ selectedHotspotLocationIndex: undefined });
  196. this.setState(({ hotspots, selectedHotspot }) => {
  197. const index = selectedHotspot && hotspots.findIndex((h) => h.key === selectedHotspot.key);
  198. if (index !== undefined && index > -1) {
  199. const newIndex = Math.max(0, Math.min(hotspots.length - 1, index + shift));
  200. return {
  201. selectedHotspot: hotspots[newIndex],
  202. };
  203. }
  204. return { selectedHotspot };
  205. });
  206. };
  207. unregisterKeyboardEvents() {
  208. document.removeEventListener('keydown', this.handleKeyDown);
  209. }
  210. constructFiltersFromProps(
  211. props: Props,
  212. ): Pick<HotspotFilters, 'assignedToMe' | 'inNewCodePeriod'> {
  213. return {
  214. assignedToMe: props.location.query.assignedToMe === 'true' && isLoggedIn(props.currentUser),
  215. inNewCodePeriod:
  216. isPullRequest(props.branchLike) || props.location.query.inNewCodePeriod === 'true',
  217. };
  218. }
  219. handleCallFailure = () => {
  220. if (this.mounted) {
  221. this.setState({ loading: false, loadingMore: false });
  222. }
  223. };
  224. fetchInitialData() {
  225. const { branchLike: previousBranch } = this.props;
  226. return Promise.all([
  227. getStandards(),
  228. this.fetchSecurityHotspots(),
  229. this.fetchSecurityHotspotsReviewed(),
  230. ])
  231. .then(([standards, { hotspots, paging }]) => {
  232. if (!this.mounted) {
  233. return;
  234. }
  235. const { branchLike } = this.props;
  236. if (isSameBranchLike(previousBranch, branchLike)) {
  237. const selectedHotspot = hotspots.length > 0 ? hotspots[0] : undefined;
  238. this.setState({
  239. hotspots,
  240. hotspotsTotal: paging.total,
  241. loading: false,
  242. selectedHotspot,
  243. standards,
  244. });
  245. }
  246. })
  247. .catch(this.handleCallFailure);
  248. }
  249. fetchSecurityHotspotsReviewed = () => {
  250. const { branchLike: previousBranch, component } = this.props;
  251. const { filters } = this.state;
  252. const reviewedHotspotsMetricKey = filters.inNewCodePeriod
  253. ? MetricKey.new_security_hotspots_reviewed
  254. : MetricKey.security_hotspots_reviewed;
  255. this.setState({ loadingMeasure: true });
  256. return getMeasures({
  257. component: component.key,
  258. metricKeys: reviewedHotspotsMetricKey,
  259. ...getBranchLikeQuery(previousBranch),
  260. })
  261. .then((measures) => {
  262. const { branchLike } = this.props;
  263. if (!this.mounted) {
  264. return;
  265. }
  266. if (isSameBranchLike(previousBranch, branchLike)) {
  267. const measure = measures && measures.length > 0 ? measures[0] : undefined;
  268. const hotspotsReviewedMeasure = filters.inNewCodePeriod
  269. ? getLeakValue(measure)
  270. : measure?.value;
  271. this.setState({ hotspotsReviewedMeasure, loadingMeasure: false });
  272. }
  273. })
  274. .catch(() => {
  275. if (this.mounted) {
  276. this.setState({ loadingMeasure: false });
  277. }
  278. });
  279. };
  280. fetchFilteredSecurityHotspots({
  281. filterByCategory,
  282. filterByCWE,
  283. filterByFile,
  284. page,
  285. }: {
  286. filterByCategory:
  287. | {
  288. standard: SecurityStandard;
  289. category: string;
  290. }
  291. | undefined;
  292. filterByCWE: string | undefined;
  293. filterByFile: string | undefined;
  294. page: number;
  295. }) {
  296. const { branchLike, component, location } = this.props;
  297. const { filters } = this.state;
  298. const hotspotFilters: Dict<string> = {};
  299. if (filterByCategory) {
  300. hotspotFilters[filterByCategory.standard] = filterByCategory.category;
  301. }
  302. if (filterByCWE) {
  303. hotspotFilters[SecurityStandard.CWE] = filterByCWE;
  304. }
  305. if (filterByFile) {
  306. hotspotFilters.files = filterByFile;
  307. }
  308. hotspotFilters['owaspAsvsLevel'] = location.query['owaspAsvsLevel'];
  309. return getSecurityHotspots(
  310. {
  311. ...hotspotFilters,
  312. inNewCodePeriod: filters.inNewCodePeriod && Boolean(filterByFile), // only add new code period when filtering by file
  313. p: page,
  314. project: component.key,
  315. ps: PAGE_SIZE,
  316. status: HotspotStatus.TO_REVIEW, // we're only interested in unresolved hotspots
  317. ...getBranchLikeQuery(branchLike),
  318. },
  319. component.needIssueSync,
  320. );
  321. }
  322. fetchSecurityHotspots(page = 1) {
  323. const { branchLike, component, location } = this.props;
  324. const { filters } = this.state;
  325. const hotspotKeys = location.query.hotspots
  326. ? (location.query.hotspots as string).split(',')
  327. : undefined;
  328. const standard = SECURITY_STANDARDS.find(
  329. (stnd) => stnd !== SecurityStandard.CWE && location.query[stnd] !== undefined,
  330. );
  331. const filterByCategory = standard
  332. ? { standard, category: location.query[standard] }
  333. : undefined;
  334. const filterByCWE: string | undefined = location.query.cwe;
  335. const filterByFile: string | undefined = location.query.files;
  336. this.setState({ filterByCategory, filterByCWE, filterByFile, hotspotKeys });
  337. if (hotspotKeys && hotspotKeys.length > 0) {
  338. return getSecurityHotspotList(
  339. hotspotKeys,
  340. {
  341. project: component.key,
  342. ...getBranchLikeQuery(branchLike),
  343. },
  344. component.needIssueSync,
  345. );
  346. }
  347. if (filterByCategory || filterByCWE || filterByFile) {
  348. return this.fetchFilteredSecurityHotspots({
  349. filterByCategory,
  350. filterByCWE,
  351. filterByFile,
  352. page,
  353. });
  354. }
  355. const status =
  356. filters.status === HotspotStatusFilter.TO_REVIEW
  357. ? HotspotStatus.TO_REVIEW
  358. : HotspotStatus.REVIEWED;
  359. const resolution =
  360. filters.status === HotspotStatusFilter.TO_REVIEW
  361. ? undefined
  362. : HotspotResolution[filters.status];
  363. return getSecurityHotspots(
  364. {
  365. inNewCodePeriod: filters.inNewCodePeriod,
  366. ...(component.needIssueSync ? {} : { onlyMine: filters.assignedToMe }),
  367. p: page,
  368. project: component.key,
  369. ps: PAGE_SIZE,
  370. resolution,
  371. status,
  372. ...getBranchLikeQuery(branchLike),
  373. },
  374. component.needIssueSync,
  375. );
  376. }
  377. reloadSecurityHotspotList = () => {
  378. this.setState({ loading: true });
  379. return this.fetchSecurityHotspots()
  380. .then(({ hotspots, paging }) => {
  381. if (!this.mounted) {
  382. return;
  383. }
  384. this.setState({
  385. hotspots,
  386. hotspotsPageIndex: 1,
  387. hotspotsTotal: paging.total,
  388. loading: false,
  389. selectedHotspot: hotspots.length > 0 ? hotspots[0] : undefined,
  390. });
  391. })
  392. .catch(this.handleCallFailure);
  393. };
  394. handleChangeFilters = (changes: Partial<HotspotFilters>) => {
  395. this.setState(
  396. ({ filters }) => ({ filters: { ...filters, ...changes } }),
  397. () => {
  398. this.reloadSecurityHotspotList();
  399. if (changes.inNewCodePeriod !== undefined) {
  400. this.fetchSecurityHotspotsReviewed();
  401. }
  402. },
  403. );
  404. };
  405. handleShowAllHotspots = () => {
  406. this.props.router.push({
  407. pathname: this.props.location.pathname,
  408. query: {
  409. assignedToMe: undefined,
  410. file: undefined,
  411. fileUuid: undefined,
  412. hotspots: [],
  413. id: this.props.component.key,
  414. sinceLeakPeriod: undefined,
  415. },
  416. });
  417. };
  418. handleChangeStatusFilter = (status: HotspotStatusFilter) => {
  419. this.handleChangeFilters({ status });
  420. };
  421. handleHotspotClick = (selectedHotspot: RawHotspot) =>
  422. this.setState({ selectedHotspot, selectedHotspotLocationIndex: undefined });
  423. handleHotspotUpdate = (hotspotKey: string) => {
  424. const { hotspots, hotspotsPageIndex } = this.state;
  425. const index = hotspots.findIndex((h) => h.key === hotspotKey);
  426. return Promise.all(
  427. range(hotspotsPageIndex).map((p) =>
  428. this.fetchSecurityHotspots(p + 1 /* pages are 1-indexed */),
  429. ),
  430. )
  431. .then((hotspotPages) => {
  432. const allHotspots = flatMap(hotspotPages, 'hotspots');
  433. const { paging } = hotspotPages[hotspotPages.length - 1];
  434. const nextHotspot = allHotspots[Math.min(index, allHotspots.length - 1)];
  435. this.setState(({ selectedHotspot }) => ({
  436. hotspots: allHotspots,
  437. hotspotsPageIndex: paging.pageIndex,
  438. hotspotsTotal: paging.total,
  439. selectedHotspot: selectedHotspot?.key === hotspotKey ? nextHotspot : selectedHotspot,
  440. }));
  441. })
  442. .then(this.fetchSecurityHotspotsReviewed);
  443. };
  444. handleLoadMore = () => {
  445. const { hotspots, hotspotsPageIndex: hotspotPages } = this.state;
  446. this.setState({ loadingMore: true });
  447. return this.fetchSecurityHotspots(hotspotPages + 1)
  448. .then(({ hotspots: additionalHotspots }) => {
  449. if (!this.mounted) {
  450. return;
  451. }
  452. this.setState({
  453. hotspots: [...hotspots, ...additionalHotspots],
  454. hotspotsPageIndex: hotspotPages + 1,
  455. loadingMore: false,
  456. });
  457. })
  458. .catch(this.handleCallFailure);
  459. };
  460. handleLocationClick = (locationIndex?: number) => {
  461. const { selectedHotspotLocationIndex } = this.state;
  462. if (locationIndex === undefined || locationIndex === selectedHotspotLocationIndex) {
  463. this.setState({
  464. selectedHotspotLocationIndex: undefined,
  465. });
  466. } else {
  467. this.setState({
  468. selectedHotspotLocationIndex: locationIndex,
  469. });
  470. }
  471. };
  472. render() {
  473. const { branchLike, component } = this.props;
  474. const {
  475. filterByCategory,
  476. filterByCWE,
  477. filterByFile,
  478. filters,
  479. hotspotKeys,
  480. hotspots,
  481. hotspotsReviewedMeasure,
  482. hotspotsTotal,
  483. loading,
  484. loadingMeasure,
  485. loadingMore,
  486. selectedHotspot,
  487. selectedHotspotLocationIndex,
  488. standards,
  489. } = this.state;
  490. return (
  491. <SecurityHotspotsAppRenderer
  492. branchLike={branchLike}
  493. component={component}
  494. filterByCategory={filterByCategory}
  495. filterByCWE={filterByCWE}
  496. filterByFile={filterByFile}
  497. filters={filters}
  498. hotspots={hotspots}
  499. hotspotsReviewedMeasure={hotspotsReviewedMeasure}
  500. hotspotsTotal={hotspotsTotal}
  501. isStaticListOfHotspots={Boolean(
  502. (hotspotKeys && hotspotKeys.length > 0) ||
  503. filterByCategory ||
  504. filterByCWE ||
  505. filterByFile,
  506. )}
  507. loading={loading}
  508. loadingMeasure={loadingMeasure}
  509. loadingMore={loadingMore}
  510. onChangeFilters={this.handleChangeFilters}
  511. onHotspotClick={this.handleHotspotClick}
  512. onLoadMore={this.handleLoadMore}
  513. onLocationClick={this.handleLocationClick}
  514. onShowAllHotspots={this.handleShowAllHotspots}
  515. onSwitchStatusFilter={this.handleChangeStatusFilter}
  516. onUpdateHotspot={this.handleHotspotUpdate}
  517. securityCategories={standards[SecurityStandard.SONARSOURCE]}
  518. selectedHotspot={selectedHotspot}
  519. selectedHotspotLocation={selectedHotspotLocationIndex}
  520. standards={standards}
  521. />
  522. );
  523. }
  524. }
  525. export default withRouter(
  526. withComponentContext(
  527. withCurrentUserContext(
  528. withBranchLikes(
  529. withIndexationGuard({
  530. Component: SecurityHotspotsApp,
  531. showIndexationMessage: ({ component }) =>
  532. !!(component.qualifier === ComponentQualifier.Application && component.needIssueSync),
  533. }),
  534. ),
  535. ),
  536. ),
  537. );