Skip to content

Commit cc48d5b

Browse files
Merge pull request #19 from patrickrobrecht/fix-csv-injection
Fix CSV injection, release 1.1.4
2 parents 2c5edc3 + 30f995d commit cc48d5b

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77

8+
## Unreleased
9+
10+
11+
## Version 1.1.4
12+
13+
## Security
14+
- Precede cell values starting with = or another spreadsheet meta-character with a single quote to avoid CSV injection of formulas within a user's display name, category names, and tag names
15+
16+
817
## Version 1.1.3
918

1019
### Changed

assets/functions.js

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
11
function posts_and_users_stats_export_table_to_csv(table, filename) {
2-
const tmpColDelim = String.fromCharCode(11), tmpRowDelim = String.fromCharCode(0), // Temporary delimiters unlikely to be typed by keyboard to avoid accidentally splitting the actual contents
3-
colDelim = '","', rowDelim = '"\r\n"', // actual delimiters for CSV
2+
// Temporary delimiters unlikely to be typed by keyboard to avoid accidentally splitting the actual contents
3+
const tmpColDelim = String.fromCharCode(11),
4+
tmpRowDelim = String.fromCharCode(0),
5+
// actual delimiters for CSV
6+
colDelim = '","',
7+
rowDelim = '"\r\n"',
8+
forbiddenStartCharacters = ['+', '-', '=', '@'],
49
rows = table.find('tr'),
5-
csv = '"' + rows.map(function (i, row) {
6-
const $row = jQuery(row), $cols = $row.find('td,th');
7-
return $cols.map(function (j, col) {
8-
const $col = jQuery(col), text = $col.text();
9-
return text.replace(/"/g, '""'); // escape double quotes
10-
}).get().join(tmpColDelim);
11-
}).get().join(tmpRowDelim).split(tmpRowDelim)
12-
.join(rowDelim).split(tmpColDelim)
10+
csv = '"' + rows
11+
.map(function (i, row) {
12+
const $row = jQuery(row),
13+
$cols = $row.find('td,th');
14+
return $cols
15+
.map(function (j, col) {
16+
const $col = jQuery(col);
17+
let text = $col.text();
18+
// Escape double quotes and trim result.
19+
text = text.replace(/"/g, '""').trim();
20+
// Prevent CSV injection.
21+
let startCharacter = text.substring(0, 1);
22+
if (forbiddenStartCharacters.includes(startCharacter)) {
23+
text = "'" + text;
24+
}
25+
return text;
26+
})
27+
.get()
28+
.join(tmpColDelim);
29+
}).get()
30+
.join(tmpRowDelim)
31+
.split(tmpRowDelim)
32+
.join(rowDelim)
33+
.split(tmpColDelim)
1334
.join(colDelim) + '"',
1435
csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv);
1536
jQuery(this).attr({

posts-and-users-stats.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Posts and Users Stats
44
* Plugin URI: https://patrick-robrecht.de/wordpress/
55
* Description: Statistics about the number of posts and users, provided as diagrams, tables and csv export.
6-
* Version: 1.1.3
6+
* Version: 1.1.4
77
* Author: Patrick Robrecht
88
* Author URI: https://patrick-robrecht.de/
99
* License: GPLv3
@@ -16,7 +16,7 @@
1616
// Exit if accessed directly.
1717
defined( 'ABSPATH' ) || exit;
1818

19-
define( 'POSTS_AND_USERS_STATS_VERSION', '1.1.3' );
19+
define( 'POSTS_AND_USERS_STATS_VERSION', '1.1.4' );
2020

2121
/**
2222
* Load text domain for translation.

readme.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
Contributors: patrickrobrecht
33
Tags: dashboard, statistics
44
Requires at least: 4.4
5-
Tested up to: 6.1
5+
Tested up to: 6.3
66
Requires PHP: 5.6
7-
Stable tag: 1.1.3
7+
Stable tag: 1.1.4
88
License: GPLv3
99
License URI: https://www.gnu.org/licenses/gpl-3.0.html
1010

@@ -49,6 +49,9 @@ After the installation you can find the statistics as subpages of *Tools*.
4949

5050
If interested, please check up the [changelog at GitHub](https://github.com/patrickrobrecht/posts-and-users-stats#changelog).
5151

52+
= 1.1.4 =
53+
* Security fix: Precede cell values starting with = or another spreadsheet meta-character with a single quote to avoid CSV injection of formulas within a user's display name, category names, and tag names
54+
5255
= 1.1.3 =
5356
* Enhancement: Compliance with latest WP coding guidelines and other code style improvements
5457

0 commit comments

Comments
 (0)