Skip to content

Commit

Permalink
more within try block. escape regex special chars (#14001)
Browse files Browse the repository at this point in the history
  • Loading branch information
amunger authored Jul 31, 2023
1 parent a9f4179 commit 10bddf8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/extension.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,14 @@ function addConsoleLogger() {
}
}

function escapeRegExp(text: string) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
}

function tryGetUsername() {
try {
return userInfo().username;
const username = escapeRegExp(userInfo().username);
return new RegExp(username, 'ig');
} catch (e) {
console.info(
`jupyter extension failed to get username info with ${e}\n username will not be obfuscated in local logs`
Expand All @@ -255,7 +260,8 @@ function tryGetUsername() {

function tryGetHomePath() {
try {
return getUserHomeDir().fsPath;
const homeDir = escapeRegExp(getUserHomeDir().fsPath);
return new RegExp(homeDir, 'ig');
} catch (e) {
console.info(
`jupyter extension failed to get home directory path with ${e}\n home Path will not be obfuscated in local logs`
Expand Down
2 changes: 1 addition & 1 deletion src/extension.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ function addConsoleLogger() {

function addOutputChannel(context: IExtensionContext, serviceManager: IServiceManager) {
const standardOutputChannel = window.createOutputChannel(OutputChannelNames.jupyter, 'log');
registerLogger(new OutputChannelLogger(standardOutputChannel, ''));
registerLogger(new OutputChannelLogger(standardOutputChannel));
serviceManager.addSingletonInstance<OutputChannel>(IOutputChannel, standardOutputChannel, STANDARD_OUTPUT_CHANNEL);
serviceManager.addSingletonInstance<OutputChannel>(
IOutputChannel,
Expand Down
6 changes: 3 additions & 3 deletions src/platform/logging/outputChannelLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ const format = require('format-util') as typeof import('format-util');
export class OutputChannelLogger implements ILogger {
private readonly homeReplaceRegEx?: RegExp;
private readonly userNameReplaceRegEx?: RegExp;
constructor(private readonly channel: OutputChannel, home?: string, userName?: string) {
this.homeReplaceRegEx = home ? new RegExp(home, 'ig') : undefined;
this.userNameReplaceRegEx = userName ? new RegExp(userName, 'ig') : undefined;
constructor(private readonly channel: OutputChannel, homeRegEx?: RegExp, userNameRegEx?: RegExp) {
this.homeReplaceRegEx = homeRegEx;
this.userNameReplaceRegEx = userNameRegEx;
}
private format(level: string | undefined, message: string, ...data: Arguments) {
let logMessage = level
Expand Down

0 comments on commit 10bddf8

Please sign in to comment.