Skip to content

Commit

Permalink
fix: disallow anything but underscore when converting any string to a…
Browse files Browse the repository at this point in the history
… valid sketch folder name

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
  • Loading branch information
Akos Kitta committed Feb 13, 2023
1 parent 6ef41a8 commit ccbca88
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions arduino-ide-extension/src/common/protocol/sketches-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export namespace Sketch {
const validName = candidate
? candidate
.replace(/^[^0-9a-zA-Z]{1}/g, defaultFallbackFirstChar)
.replace(/[^0-9a-zA-Z_\.-]/g, defaultFallbackChar)
.replace(/[^0-9a-zA-Z_]/g, defaultFallbackChar)
.slice(0, 63)
: defaultSketchFolderName;
if (appendTimestampSuffix) {
Expand Down Expand Up @@ -233,7 +233,7 @@ export namespace Sketch {
return candidate
? candidate
.replace(/^[^0-9a-zA-Z]{1}/g, defaultFallbackFirstChar)
.replace(/[^0-9a-zA-Z_\.-]/g, defaultFallbackChar)
.replace(/[^0-9a-zA-Z_]/g, defaultFallbackChar)
.slice(0, 36)
: defaultSketchFolderName;
}
Expand Down
20 changes: 10 additions & 10 deletions arduino-ide-extension/src/test/common/sketches-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ describe('sketch', () => {
['foo bar', 'foo_bar' + epochSuffix],
['.foobar', '0foobar' + epochSuffix],
['-fooBar', '0fooBar' + epochSuffix],
['foobar.', 'foobar_' + epochSuffix],
['fooBar-', 'fooBar_' + epochSuffix],
['fooBar+', 'fooBar_' + epochSuffix],
['vAlid', 'vAlid' + epochSuffix],
].map(([input, expected]) =>
toMapIt(input, expected, (input: string) =>
Expand All @@ -118,23 +121,20 @@ describe('sketch', () => {
describe('toValidCloudSketchFolderName', () => {
[
['sketch', 'sketch'],
[
'slash-and-dot-is-ok+but+no+plus.ino',
'slash-and-dot-is-ok_but_no_plus.ino',
],
['only_underscore-is+ok.ino', 'only_underscore_is_ok_ino'],
['regex++', 'regex__'],
['dots...', 'dots...'],
['.dots...', '0dots...'],
['-dashes---', '0dashes---'],
['dots...', 'dots___'],
['.dots...', '0dots___'],
['-dashes---', '0dashes___'],
['_underscore___', '0underscore___'],
['No Spaces', 'No_Spaces'],
['_startsWithUnderscore', '0startsWithUnderscore'],
['Invalid+Char.ino', 'Invalid_Char.ino'],
['Invalid+Char.ino', 'Invalid_Char_ino'],
['', 'sketch'],
['/', '0'],
[
'//////////////////////////////////////-/',
'0___________________________________',
'/-1////////////////////+//////////////-/',
'0_1_________________________________',
],
['//trash/', '0_trash_'],
[
Expand Down

0 comments on commit ccbca88

Please sign in to comment.