Sanitize db name for dacpac/bacpac file names (#5479)

* Sanitize db name for filename

* Add unit tests

* lower timeout to 60 seconds

* add extra coverageConfig.json and missing character check
This commit is contained in:
kisantia
2019-05-14 15:00:28 -07:00
committed by GitHub
parent be60ad6766
commit 48ba9ce175
12 changed files with 1496 additions and 6 deletions

View File

@@ -12,6 +12,7 @@ import * as path from 'path';
import { DataTierApplicationWizard } from '../dataTierApplicationWizard';
import { DacFxDataModel } from './models';
import { BasePage } from './basePage';
import { sanitizeStringForFilename } from './utils';
const localize = nls.loadMessageBundle();
@@ -147,7 +148,7 @@ export abstract class DacFxConfigPage extends BasePage {
}
protected generateFilePathFromDatabaseAndTimestamp(): string {
return path.join(this.getRootPath(), this.model.database + '-' + this.getDateTime() + this.fileExtension);
return path.join(this.getRootPath(), sanitizeStringForFilename(this.model.database) + '-' + this.getDateTime() + this.fileExtension);
}
protected getDateTime(): string {

View File

@@ -0,0 +1,43 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the Source EULA. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as os from 'os';
const INVALID_FILE_CHARS_Windows = /[\\/:\*\?"<>\|]/g;
const INVALID_FILE_CHARS = /[\\/]/g;
/**
* Determines if a given character is a valid filename character
* @param c Character to validate
*/
export function isValidFilenameCharacter(c: string): boolean {
// only a character should be passed
if (!c || c.length !== 1) {
return false;
}
let isWindows = os.platform() === 'win32';
INVALID_FILE_CHARS_Windows.lastIndex = 0;
INVALID_FILE_CHARS.lastIndex = 0;
if (isWindows && INVALID_FILE_CHARS_Windows.test(c)) {
return false;
} else if (!isWindows && INVALID_FILE_CHARS.test(c)) {
return false;
}
return true;
}
/**
* Replaces invalid filename characters in a string with underscores
* @param s The string to be sanitized for a filename
*/
export function sanitizeStringForFilename(s: string): string {
// replace invalid characters with an underscore
let result = '';
for (let i = 0; i < s.length; ++i) {
result += this.isValidFilenameCharacter(s[i]) ? s[i] : '_';
}
return result;
}