Skip to content

Commit

Permalink
3cab48583da254adb44e6c00b7bcd13023563712 New: Add `escapeExcelFormula…
Browse files Browse the repository at this point in the history
…` to `-api buttons.exportData()` and enable it by default for the CSV export button to prevent possible formula injection.

https://datatables.net/forums/discussion/80069

Sync to source repo @3cab48583da254adb44e6c00b7bcd13023563712
  • Loading branch information
dtbuild committed Oct 17, 2024
1 parent 2138813 commit cdb39d4
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion datatables.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@
],
"src-repo": "http://github.com/DataTables/Buttons",
"last-tag": "3.1.2",
"last-sync": "fe105bc93f50921112e4f66a698b3312395c9a6a"
"last-sync": "3cab48583da254adb44e6c00b7bcd13023563712"
}
4 changes: 3 additions & 1 deletion js/buttons.html5.js
Original file line number Diff line number Diff line change
Expand Up @@ -1117,7 +1117,9 @@ DataTable.ext.buttons.csvHtml5 = {

extension: '.csv',

exportOptions: {},
exportOptions: {
escapeExcelFormula: true
},

fieldSeparator: ',',

Expand Down
2 changes: 1 addition & 1 deletion js/buttons.html5.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/buttons.html5.min.mjs

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion js/buttons.html5.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,7 +1074,9 @@ DataTable.ext.buttons.csvHtml5 = {

extension: '.csv',

exportOptions: {},
exportOptions: {
escapeExcelFormula: true
},

fieldSeparator: ',',

Expand Down
9 changes: 9 additions & 0 deletions js/dataTables.buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -2016,6 +2016,14 @@ Buttons.stripData = function (str, config) {
}
}

// Prevent Excel from running a formula
if (!config || config.escapeExcelFormula) {
if (str.match(/^[=+\-@\t\r]/)) {
console.log('matching and updateing');
str = "'" + str;
}
}

return str;
};

Expand Down Expand Up @@ -2738,6 +2746,7 @@ var _exportData = function (dt, inOpts) {
stripHtml: true,
stripNewlines: true,
decodeEntities: true,
escapeExcelFormula: false,
trim: true,
format: {
header: function (d) {
Expand Down
2 changes: 1 addition & 1 deletion js/dataTables.buttons.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/dataTables.buttons.min.mjs

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions js/dataTables.buttons.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,14 @@ Buttons.stripData = function (str, config) {
}
}

// Prevent Excel from running a formula
if (!config || config.escapeExcelFormula) {
if (str.match(/^[=+\-@\t\r]/)) {
console.log('matching and updateing');
str = "'" + str;
}
}

return str;
};

Expand Down Expand Up @@ -2698,6 +2706,7 @@ var _exportData = function (dt, inOpts) {
stripHtml: true,
stripNewlines: true,
decodeEntities: true,
escapeExcelFormula: false,
trim: true,
format: {
header: function (d) {
Expand Down

0 comments on commit cdb39d4

Please sign in to comment.