diff --git a/src/services/analytics.csvExport.test.js b/src/services/analytics.csvExport.test.js new file mode 100644 index 00000000..43c61ec5 --- /dev/null +++ b/src/services/analytics.csvExport.test.js @@ -0,0 +1,168 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { + exportReposCSV, + exportContributorsCSV, + exportTrendsCSV, +} from './analytics' + +let downloadedContent + +beforeEach(() => { + downloadedContent = '' + + vi.stubGlobal('Blob', class MockBlob { + constructor(parts, options) { + this.content = parts.join('') + this.type = options?.type + } + }) + + vi.stubGlobal('URL', { + createObjectURL: vi.fn(blob => { + downloadedContent = blob.content + return 'blob:mock' + }), + revokeObjectURL: vi.fn(), + }) + + vi.spyOn(HTMLAnchorElement.prototype, 'click') + .mockImplementation(() => { }) +}) + +afterEach(() => { + vi.restoreAllMocks() + vi.unstubAllGlobals() +}) + +describe('CSV exports', () => { + it('neutralizes formula-like repository names', () => { + exportReposCSV([{ + name: '=1+1', + orgLogin: 'example-org', + stargazers_count: 10, + forks_count: 2, + open_issues_count: 1, + healthScore: 80, + activityClassification: 'Active', + language: 'JavaScript', + pushed_at: '2026-10-22T00:00:00Z', + }]) + + expect(downloadedContent).toContain("'=1+1,example-org") + }) + + it('neutralizes formula-like contributor logins', () => { + exportContributorsCSV([{ + login: '@SUM(1,2)', + totalContribs: 10, + repos: [], + orgs: [], + lastActive: null, + isConnector: false, + isCrossOrg: false, + }]) + + expect(downloadedContent).toContain('"\'@SUM(1,2)"') + }) + + it('neutralizes formula-like trend dates', () => { + exportTrendsCSV([{ + date: '=1+1', + prs_created: 1, + prs_merged: 0, + prs_closed: 0, + issues_created: 2, + issues_closed: 1, + }]) + + expect(downloadedContent).toContain("'=1+1,1,0,0,2,1") + }) + + it('escapes commas, quotes and newlines in CSV fields', () => { + exportReposCSV([{ + name: 'repo, "example"\nsecond line', + orgLogin: 'example-org', + stargazers_count: 10, + forks_count: 2, + open_issues_count: 1, + healthScore: 80, + activityClassification: 'Active', + language: 'JavaScript', + pushed_at: '2026-10-22T00:00:00Z', + }]) + + expect(downloadedContent).toContain('"repo, ""example""\nsecond line"') + }) + + it('preserves ordinary text and numeric metrics', () => { + exportReposCSV([{ + name: 'orgexplorer', + orgLogin: 'AOSSIE-Org', + stargazers_count: 42, + forks_count: 5, + open_issues_count: 3, + healthScore: 90, + activityClassification: 'Active', + language: 'JavaScript', + pushed_at: '2026-10-22T00:00:00Z', + }]) + + expect(downloadedContent).toContain( + 'orgexplorer,AOSSIE-Org,42,5,3,90,Active,JavaScript,2026-10-22', + ) + }) + + it.each(['=1+1', '+1+1', '-1+1', '@SUM(1,2)'])( + 'neutralizes formula prefix %s', + prefix => { + exportReposCSV([{ + name: prefix, + orgLogin: 'example-org', + stargazers_count: 10, + forks_count: 2, + open_issues_count: 1, + healthScore: 80, + activityClassification: 'Active', + language: 'JavaScript', + pushed_at: '2026-10-22T00:00:00Z', + }]) + + expect(downloadedContent).toContain(`'${prefix}`) + }, + ) + + it('neutralizes formulas preceded by whitespace or control characters', () => { + for (const name of [' =1+1', '\t=1+1', '\n=1+1']) { + exportReposCSV([{ + name, + orgLogin: 'example-org', + stargazers_count: 10, + forks_count: 2, + open_issues_count: 1, + healthScore: 80, + activityClassification: 'Active', + language: 'JavaScript', + pushed_at: '2026-10-22T00:00:00Z', + }]) + + expect(downloadedContent).toContain(`'${name}`) + } + }) + + it('neutralizes formula-like organization names as well as repository names', () => { + exportReposCSV([{ + name: 'normal-repo', + orgLogin: '=1+1', + stargazers_count: 10, + forks_count: 2, + open_issues_count: 1, + healthScore: 80, + activityClassification: 'Active', + language: 'JavaScript', + pushed_at: '2026-10-22T00:00:00Z', + }]) + + expect(downloadedContent).toContain("normal-repo,'=1+1") + }) + +}) diff --git a/src/services/analytics.js b/src/services/analytics.js index 058549bd..8dde0928 100644 --- a/src/services/analytics.js +++ b/src/services/analytics.js @@ -155,6 +155,32 @@ export function buildTimeSeries(issues = [], granularity = 'monthly') { } // CSV Export +function serializeCSVCell(value) { + if (value == null) return '' + + let cell = String(value) + + // Neutralize formula-like untrusted text before spreadsheet applications + // can interpret it as a formula. + if ( + typeof value === 'string' && + /^[\u0000-\u0020]*[=+\-@\t\r\n]/.test(value) + ) { + cell = `'${cell}` + } + + // Escape CSV delimiters, quotes and line breaks. + if (/[",\r\n]/.test(cell)) { + cell = `"${cell.replace(/"/g, '""')}"` + } + + return cell +} + +function serializeCSV(rows) { + return rows.map(row => row.map(serializeCSVCell).join(',')).join('\r\n') +} + function download(content, filename, type = 'text/csv') { const blob = new Blob([content], { type }) const url = URL.createObjectURL(blob) @@ -166,19 +192,19 @@ function download(content, filename, type = 'text/csv') { export function exportReposCSV(repos) { const header = ['Repository', 'Org', 'Stars', 'Forks', 'Open Issues', 'Health Score', 'Activity Classification', 'Language', 'Last Active'] const rows = repos.map(r => [r.name, r.orgLogin, r.stargazers_count, r.forks_count, r.open_issues_count, r.healthScore, r.activityClassification, r.language || 'N/A', r.pushed_at?.slice(0, 10)]) - download([header, ...rows].map(r => r.join(',')).join('\n'), 'orgexplorer-repos.csv') + download(serializeCSV([header, ...rows]), 'orgexplorer-repos.csv') } export function exportContributorsCSV(contributors) { const header = ['Login', 'Total Contributions', 'Repos', 'Orgs', 'Last Active', 'Connector', 'Cross-Org'] const rows = contributors.map(c => [c.login, c.totalContribs, c.repos.length, c.orgs.length, c.lastActive?.slice(0, 10) || '', c.isConnector, c.isCrossOrg]) - download([header, ...rows].map(r => r.join(',')).join('\n'), 'orgexplorer-contributors.csv') + download(serializeCSV([header, ...rows]), 'orgexplorer-contributors.csv') } export function exportTrendsCSV(series) { const header = ['Date', 'PRs Created', 'PRs Merged', 'PRs Closed', 'Issues Created', 'Issues Closed'] const rows = series.map(s => [s.date, s.prs_created, s.prs_merged, s.prs_closed, s.issues_created, s.issues_closed]) - download([header, ...rows].map(r => r.join(',')).join('\n'), 'orgexplorer-trends.csv') + download(serializeCSV([header, ...rows]), 'orgexplorer-trends.csv') } export function getTopRepositories(repos, limit = 10) {