@@ -508,6 +508,39 @@ export function bindThemeChartRedraw() {
508508 }
509509}
510510
511+ /**
512+ * Validates an untrusted URL from the report payload before it reaches
513+ * window.open: only absolute http:/https: URLs pass, so injected schemes
514+ * (javascript:, data:, file:, …) and malformed values are rejected.
515+ *
516+ * @param {* } value - Candidate URL from the fetched report data.
517+ * @returns {boolean } True when value parses to an http: or https: URL.
518+ */
519+ export function isHttpUrl ( value ) {
520+ if ( typeof value !== 'string' ) return false ;
521+ let parsed ;
522+ try {
523+ parsed = new URL ( value ) ;
524+ } catch {
525+ return false ;
526+ }
527+ return parsed . protocol === 'http:' || parsed . protocol === 'https:' ;
528+ }
529+
530+ /**
531+ * Creates a disharmony bubble chart, replacing any chart on the canvas and
532+ * registering it for theme redraws. Clicking a bubble with a URL opens its
533+ * source in a new tab with noopener. Warns and returns if Chart.js is unavailable.
534+ *
535+ * @param {HTMLCanvasElement } canvas - Canvas on which to render the chart.
536+ * @param {string } title - Dataset label displayed in the legend.
537+ * @param {object } chartData - Report chart data with optional bubbles and axis labels.
538+ * @param {Array<object> } [chartData.bubbles] - Bubble coordinates, radius, colors,
539+ * label, priority, and optional source URL.
540+ * @param {string } [chartData.xaxisLabel] - Overrides the effort axis label.
541+ * @param {string } [chartData.yaxisLabel] - Overrides the relative churn axis label.
542+ * @returns {void }
543+ */
511544export function initBubbleChart ( canvas , title , chartData ) {
512545 const ChartLib = window . Chart ;
513546 if ( ! ChartLib ) {
@@ -532,6 +565,41 @@ export function initBubbleChart(canvas, title, chartData) {
532565 options : {
533566 responsive : true ,
534567 maintainAspectRatio : false ,
568+ // Clicking a bubble opens the source file the report links it
569+ // to (disharmonies[].chart.bubbles[].url) in a new tab, with
570+ // noopener so the opened page can't reach back into this one.
571+ onClick : ( event , elements , chart ) => {
572+ const hit = elements && elements [ 0 ] ;
573+ if ( ! hit || ! chart || ! chart . data || ! chart . data . datasets ) return ;
574+ const point = chart . data . datasets [ hit . datasetIndex ] ;
575+ const url = point && point . data && point . data [ hit . index ] && point . data [ hit . index ] . raw
576+ ? point . data [ hit . index ] . raw . url
577+ : null ;
578+ // The url comes from the fetched report payload, which is
579+ // untrusted: only parsed http(s) URLs may reach
580+ // window.open, so a malicious javascript:/data: value
581+ // can't execute when the bubble is clicked.
582+ if ( isHttpUrl ( url ) && typeof window !== 'undefined' && typeof window . open === 'function' ) {
583+ window . open ( url , '_blank' , 'noopener' ) ;
584+ }
585+ } ,
586+ // Pointer affordance while the cursor is over a bubble — only
587+ // when the hovered bubble carries a URL the click handler would
588+ // actually open (isHttpUrl), so the hover hint can never
589+ // promise a navigation the click refuses to perform.
590+ onHover : ( event , elements , chart ) => {
591+ if ( ! event || ! event . native || ! event . native . target ) return ;
592+ const hit = elements && elements [ 0 ] ;
593+ const dataset = hit && chart && chart . data && chart . data . datasets
594+ ? chart . data . datasets [ hit . datasetIndex ]
595+ : null ;
596+ const raw = dataset && dataset . data && dataset . data [ hit . index ]
597+ ? dataset . data [ hit . index ] . raw
598+ : null ;
599+ event . native . target . style . cursor = hit && isHttpUrl ( raw ? raw . url : null )
600+ ? 'pointer'
601+ : 'default' ;
602+ } ,
535603 plugins : {
536604 legend : {
537605 display : true ,
@@ -565,12 +633,18 @@ export function initBubbleChart(canvas, title, chartData) {
565633 callbacks : {
566634 label : context => {
567635 const raw = context . raw . raw ;
568- return [
636+ const lines = [
569637 `File: ${ raw . label } ` ,
570638 `Priority: ${ raw . priority } ` ,
571639 `Effort Rank: ${ raw . x } ` ,
572640 `Change Proneness Rank: ${ raw . y } `
573641 ] ;
642+ // Same gate as the click handler: the prompt
643+ // only appears when the url is actually
644+ // openable (isHttpUrl), never for scheme-
645+ // injected or malformed values.
646+ if ( isHttpUrl ( raw . url ) ) lines . push ( 'Click to open source file' ) ;
647+ return lines ;
574648 }
575649 }
576650 }
0 commit comments