Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize values for CSV. #7697

Merged
merged 2 commits into from
Oct 17, 2022
Merged
Changes from 1 commit
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
17 changes: 10 additions & 7 deletions ui/util/parse-data.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// JSON parser
const parseJson = (data, filters = []) => {
const list = data.map(item => {
const list = data.map((item) => {
const temp = {};
// Apply filters
Object.entries(item).forEach(([key, value]) => {
Expand All @@ -17,7 +17,7 @@ const parseJson = (data, filters = []) => {
// https://gist.github.com/btzr-io/55c3450ea3d709fc57540e762899fb85
const parseCsv = (data, filters = []) => {
// Get items for header
const getHeaders = item => {
const getHeaders = (item) => {
const list = [];
// Apply filters
Object.entries(item).forEach(([key]) => {
Expand All @@ -28,13 +28,16 @@ const parseCsv = (data, filters = []) => {
};

// Get rows content
const getData = list =>
const getData = (list) =>
list
.map(item => {
.map((item) => {
const row = [];
// Apply filters
Object.entries(item).forEach(([key, value]) => {
if (!filters.includes(key)) row.push(value);
if (!filters.includes(key)) {
const sanitizedValue = '"' + String(value).replaceAll('"', '\\"') + '"';
jessopb marked this conversation as resolved.
Show resolved Hide resolved
row.push(sanitizedValue);
}
});
// return rows
return row.join(',');
Expand All @@ -50,8 +53,8 @@ const parseData = (data, format, filters = []) => {
const valid = data && data[0] && format;
// Pick a format
const formats = {
csv: list => parseCsv(list, filters),
json: list => parseJson(list, filters),
csv: (list) => parseCsv(list, filters),
json: (list) => parseJson(list, filters),
};

// Return parsed data: JSON || CSV
Expand Down