Skip to content
Merged
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
32 changes: 32 additions & 0 deletions assets/core/ts/utils/security.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,35 @@ export function escapeAttr(str: string): string {
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
}

/**
* Formula injection prevention prefixes (OWASP / CWE-1236)
*/
const FORMULA_INJECTION_PREFIXES = ['=', '+', '-', '@', '\t', '\r'] as const;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks unnecessary.

@b-l-i-n-d b-l-i-n-d Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's used inside sanitizeCsvCell. There is a small benefit to declaring it outside the function.

if (FORMULA_INJECTION_PREFIXES.some((prefix) => str.startsWith(prefix))) {
    str = `'${str}`;
  }


/**
* Sanitizes a single cell value for CSV export to prevent formula injection (OWASP / CWE-1236)
* and properly escape commas, double quotes, and newlines.
*
* @param rawVal - The raw value to sanitize
* @returns The sanitized cell string
*
* @since 4.2.0
*/
export function sanitizeCsvCell(rawVal: unknown): string {
if (rawVal === null || rawVal === undefined) {
return '';
}

let str = String(rawVal);

if (FORMULA_INJECTION_PREFIXES.some((prefix) => str.startsWith(prefix))) {
str = `'${str}`;
}

if (str.includes(',') || str.includes('"') || str.includes('\n') || str.includes('\r')) {
return `"${str.replace(/"/g, '""')}"`;
}

return str;
}
Loading