Skip to content

Commit

Permalink
fix: Special characters being removed during collectio import
Browse files Browse the repository at this point in the history
Instead of using a strict sanitization regex in the frontend
the importer will now sanitize the filename before writing
it to disk.

Should fix:
- usebruno#1779
- usebruno#1587
- usebruno#671
- usebruno#1274
  • Loading branch information
Its-treason committed Mar 14, 2024
1 parent bf3d3ca commit eb638db
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 19 deletions.
12 changes: 0 additions & 12 deletions packages/bruno-app/src/utils/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,6 @@ export const safeParseXML = (str, options) => {
}
};

// Remove any characters that are not alphanumeric, spaces, hyphens, or underscores
export const normalizeFileName = (name) => {
if (!name) {
return name;
}

const validChars = /[^\w\s-]/g;
const formattedName = name.replace(validChars, '-');

return formattedName;
};

export const getContentType = (headers) => {
const headersArray = typeof headers === 'object' ? Object.entries(headers) : [];

Expand Down
7 changes: 5 additions & 2 deletions packages/bruno-app/src/utils/common/regex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// See https://github.com/usebruno/bruno/pull/349 for more info
// Scrict regex for validating directories. Covers most edge cases like windows device names
export const dirnameRegex = /^(?!CON|PRN|AUX|NUL|COM\d|LPT\d|^ |^\-)[^<>:"/\\|?*\x00-\x1F]+[^\. ]$/;
// Strict regex for validating directories. Covers most edge cases like windows device names
export const dirnameRegex = /^(?!CON|PRN|AUX|NUL|COM\d|LPT\d|^ |^-)[^<>:"/\\|?*\x00-\x1F]+[^\. ]$/;
// Not so strict Regex for filenames, because files normally get a extension e.g. ".bru" and are not affect by
// windows special names e.g. CON, PRN
export const filenameRegex = /[<>:"/\\|?*\x00-\x1F]/;

export const variableNameRegex = /^[\w-.]*$/;
4 changes: 1 addition & 3 deletions packages/bruno-app/src/utils/importers/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import each from 'lodash/each';
import get from 'lodash/get';

import cloneDeep from 'lodash/cloneDeep';
import { uuid, normalizeFileName } from 'utils/common';
import { uuid } from 'utils/common';
import { isItemARequest } from 'utils/collections';
import { collectionSchema } from '@usebruno/schema';
import { BrunoError } from 'utils/common/error';
Expand Down Expand Up @@ -62,8 +62,6 @@ export const updateUidsInCollection = (_collection) => {
export const transformItemsInCollection = (collection) => {
const transformItems = (items = []) => {
each(items, (item) => {
item.name = normalizeFileName(item.name);

if (['http', 'graphql'].includes(item.type)) {
item.type = `${item.type}-request`;
if (item.request.query) {
Expand Down
6 changes: 4 additions & 2 deletions packages/bruno-electron/src/ipc/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,11 +448,13 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection
items.forEach((item) => {
if (['http-request', 'graphql-request'].includes(item.type)) {
const content = jsonToBru(item);
const filePath = path.join(currentPath, `${item.name}.bru`);
const sanitizedFilename = sanitizeFilename(item.name);
const filePath = path.join(currentPath, `${sanitizedFilename}.bru`);
fs.writeFileSync(filePath, content);
}
if (item.type === 'folder') {
const folderPath = path.join(currentPath, item.name);
const sanitizedFolderName = sanitizeDirectoryName(item.name);
const folderPath = path.join(currentPath, sanitizedFolderName);
fs.mkdirSync(folderPath);

if (item.items && item.items.length) {
Expand Down

0 comments on commit eb638db

Please sign in to comment.