mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-02-16 02:48:30 -05:00
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:
@@ -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 {
|
||||
|
||||
43
extensions/dacpac/src/wizard/api/utils.ts
Normal file
43
extensions/dacpac/src/wizard/api/utils.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user