Skip to content

Commit 561165f

Browse files
author
David Haeffner
committed
Sanitize the project name when saving to file, to remove illegal filesystem characters or grab a URL hostname (if a URL was used for the project name). The project name in the SIDE file will be respected as entered by the user.
1 parent 2c710f7 commit 561165f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

packages/selenium-ide/src/neo/IO/filesystem.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import Manager from '../../plugin/manager'
3434
import chromeGetFile from './filesystem/chrome'
3535
import firefoxGetFile from './filesystem/firefox'
3636
import { userAgent as parsedUA } from '../../common/utils'
37+
import url from 'url'
3738

3839
export function getFile(path) {
3940
const browserName = parsedUA.browser.name
@@ -75,14 +76,23 @@ export function saveProject(_project) {
7576
UiState.saved()
7677
}
7778

79+
export function sanitizeProjectName(projectName) {
80+
let name = projectName
81+
if (name.startsWith('http')) {
82+
return url.parse(projectName).host
83+
} else {
84+
return name.replace(/([^a-z0-9 ._-]+)/gi, '')
85+
}
86+
}
87+
7888
function downloadProject(project) {
7989
return exportProject(project).then(snapshot => {
8090
if (snapshot) {
8191
project.snapshot = snapshot
8292
Object.assign(project, Manager.emitDependencies())
8393
}
8494
return browser.downloads.download({
85-
filename: project.name + '.side',
95+
filename: sanitizeProjectName(project.name) + '.side',
8696
url: createBlob(
8797
'application/json',
8898
beautify(JSON.stringify(project), { indent_size: 2 })
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { sanitizeProjectName } from '../../IO/filesystem'
2+
3+
describe('filesystem', () => {
4+
describe('save', () => {
5+
describe('filename', () => {
6+
it('allows alpha-numeric characters', () => {
7+
const input = 'asdf1234'
8+
expect(sanitizeProjectName(input)).toEqual(input)
9+
})
10+
it('allows limited special characters', () => {
11+
let input = 'asdf-1234'
12+
expect(sanitizeProjectName(input)).toEqual(input)
13+
input = 'asdf_1234'
14+
expect(sanitizeProjectName(input)).toEqual(input)
15+
input = 'asdf.1234'
16+
expect(sanitizeProjectName(input)).toEqual(input)
17+
input = 'asdf 1234'
18+
expect(sanitizeProjectName(input)).toEqual(input)
19+
})
20+
it('ignores illegal filesystem characters', () => {
21+
const input = 'blah:/blah'
22+
expect(sanitizeProjectName(input)).toEqual('blahblah')
23+
})
24+
it('with URI returns the root', () => {
25+
const input = 'http://a.b.com'
26+
expect(sanitizeProjectName(input)).toEqual('a.b.com')
27+
})
28+
})
29+
})
30+
})

0 commit comments

Comments
 (0)