Skip to content

Commit 02fbc96

Browse files
Fix npm vulnerability (#355)
* Remove rewiremock * fix tests * remove test filter
1 parent 05a3079 commit 02fbc96

File tree

7 files changed

+260
-1610
lines changed

7 files changed

+260
-1610
lines changed

package-lock.json

+216-1,579
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-6
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,11 @@
517517
}
518518
],
519519
"debugVisualizers": [
520-
{
521-
"id": "inlineHexDecoder",
522-
"when": "debugConfigurationType == 'debugpy' && (variableType == 'float' || variableType == 'int')"
523-
}
524-
]
520+
{
521+
"id": "inlineHexDecoder",
522+
"when": "debugConfigurationType == 'debugpy' && (variableType == 'float' || variableType == 'int')"
523+
}
524+
]
525525
},
526526
"extensionDependencies": [
527527
"ms-python.python"
@@ -561,7 +561,6 @@
561561
"glob": "^8.0.3",
562562
"mocha": "^10.0.0",
563563
"prettier": "^3.0.3",
564-
"rewiremock": "^3.13.0",
565564
"semver": "^7.5.4",
566565
"sinon": "^15.0.2",
567566
"ts-loader": "^9.3.1",

src/extension/telemetry/index.ts

+3-17
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
import TelemetryReporter from '@vscode/extension-telemetry';
66

7-
import { AppinsightsKey, isTestExecution } from '../common/constants';
7+
import { isTestExecution } from '../common/constants';
88
import { StopWatch } from '../common/utils/stopWatch';
99
import { ConsoleType, TriggerType } from '../types';
1010
import { DebugConfigurationType } from '../debugger/types';
1111
import { EventName } from './constants';
1212
import { isPromise } from '../common/utils/async';
13+
import { getTelemetryReporter } from './reporter';
1314

1415
/**
1516
* Checks whether telemetry is supported.
@@ -31,21 +32,6 @@ function isTelemetrySupported(): boolean {
3132
const sharedProperties: Record<string, unknown> = {};
3233

3334
let telemetryReporter: TelemetryReporter | undefined;
34-
function getTelemetryReporter() {
35-
if (!isTestExecution() && telemetryReporter) {
36-
return telemetryReporter;
37-
}
38-
39-
// eslint-disable-next-line @typescript-eslint/naming-convention
40-
const Reporter = require('@vscode/extension-telemetry').default as typeof TelemetryReporter;
41-
telemetryReporter = new Reporter(AppinsightsKey, [
42-
{
43-
lookup: /(errorName|errorMessage|errorStack)/g,
44-
},
45-
]);
46-
47-
return telemetryReporter;
48-
}
4935

5036
export function clearTelemetryReporter(): void {
5137
telemetryReporter = undefined;
@@ -60,7 +46,7 @@ export function sendTelemetryEvent<P extends IEventNamePropertyMapping, E extend
6046
if (isTestExecution() || !isTelemetrySupported()) {
6147
return;
6248
}
63-
const reporter = getTelemetryReporter();
49+
const reporter = getTelemetryReporter(telemetryReporter);
6450
const measures =
6551
typeof measuresOrDurationMs === 'number'
6652
? { duration: measuresOrDurationMs }

src/extension/telemetry/reporter.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
import TelemetryReporter from '@vscode/extension-telemetry';
5+
import { AppinsightsKey, isTestExecution } from '../common/constants';
6+
7+
export function getTelemetryReporter(telemetryReporter: TelemetryReporter | undefined) {
8+
if (!isTestExecution() && telemetryReporter) {
9+
return telemetryReporter;
10+
}
11+
12+
// eslint-disable-next-line @typescript-eslint/naming-convention
13+
const Reporter = require('@vscode/extension-telemetry').default as typeof TelemetryReporter;
14+
telemetryReporter = new Reporter(AppinsightsKey, [
15+
{
16+
lookup: /(errorName|errorMessage|errorStack)/g,
17+
},
18+
]);
19+
20+
return telemetryReporter;
21+
}

src/test/unittest/adapter/factory.unit.test.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ import { expect, use } from 'chai';
99
import * as chaiAsPromised from 'chai-as-promised';
1010
import * as path from 'path';
1111
import * as sinon from 'sinon';
12-
import rewiremock from 'rewiremock';
1312
import { SemVer } from 'semver';
1413
import { instance, mock, when } from 'ts-mockito';
1514
import { DebugAdapterExecutable, DebugAdapterServer, DebugConfiguration, DebugSession, WorkspaceFolder } from 'vscode';
1615
import { IPersistentStateFactory } from '../../../extension/common/types';
1716
import { DebugAdapterDescriptorFactory, debugStateKeys } from '../../../extension/debugger/adapter/factory';
1817
import { IDebugAdapterDescriptorFactory } from '../../../extension/debugger/types';
19-
import { clearTelemetryReporter } from '../../../extension/telemetry';
2018
import { EventName } from '../../../extension/telemetry/constants';
2119
import { PersistentState, PersistentStateFactory } from '../../../extension/common/persistentState';
22-
import * as vscodeApi from '../../../extension/common/vscodeapi';
2320
import { EXTENSION_ROOT_DIR } from '../../../extension/common/constants';
2421
import { Architecture } from '../../../extension/common/platform';
2522
import * as pythonApi from '../../../extension/common/python';
23+
import * as telemetry from '../../../extension/telemetry';
24+
import * as telemetryReporter from '../../../extension/telemetry/reporter';
25+
import * as vscodeApi from '../../../extension/common/vscodeapi';
2626
import { DebugConfigStrings } from '../../../extension/common/utils/localize';
2727

2828
use(chaiAsPromised);
@@ -36,6 +36,8 @@ suite('Debugging - Adapter Factory', () => {
3636
let getInterpretersStub: sinon.SinonStub;
3737
let getInterpreterDetailsStub: sinon.SinonStub;
3838
let hasInterpretersStub: sinon.SinonStub;
39+
let getTelemetryReporterStub: sinon.SinonStub;
40+
let reporter: any;
3941

4042
const nodeExecutable = undefined;
4143
const debugAdapterPath = path.join(EXTENSION_ROOT_DIR, 'bundled', 'libs', 'debugpy', 'adapter');
@@ -65,22 +67,23 @@ suite('Debugging - Adapter Factory', () => {
6567
setup(() => {
6668
process.env.VSC_PYTHON_UNIT_TEST = undefined;
6769
process.env.VSC_PYTHON_CI_TEST = undefined;
68-
rewiremock.enable();
69-
rewiremock('@vscode/extension-telemetry').with({ default: Reporter });
70+
reporter = new Reporter();
71+
7072
stateFactory = mock(PersistentStateFactory);
7173
state = mock(PersistentState) as PersistentState<boolean | undefined>;
7274
showErrorMessageStub = sinon.stub(vscodeApi, 'showErrorMessage');
7375
resolveEnvironmentStub = sinon.stub(pythonApi, 'resolveEnvironment');
7476
getInterpretersStub = sinon.stub(pythonApi, 'getInterpreters');
7577
getInterpreterDetailsStub = sinon.stub(pythonApi, 'getInterpreterDetails');
7678
hasInterpretersStub = sinon.stub(pythonApi, 'hasInterpreters');
79+
getTelemetryReporterStub = sinon.stub(telemetryReporter, 'getTelemetryReporter');
7780

7881
when(
7982
stateFactory.createGlobalPersistentState<boolean | undefined>(debugStateKeys.doNotShowAgain, false),
8083
).thenReturn(instance(state));
81-
8284
getInterpretersStub.returns([interpreter]);
8385
hasInterpretersStub.returns(true);
86+
getTelemetryReporterStub.returns(reporter);
8487
factory = new DebugAdapterDescriptorFactory(instance(stateFactory));
8588
});
8689

@@ -90,8 +93,7 @@ suite('Debugging - Adapter Factory', () => {
9093
Reporter.properties = [];
9194
Reporter.eventNames = [];
9295
Reporter.measures = [];
93-
rewiremock.disable();
94-
clearTelemetryReporter();
96+
telemetry.clearTelemetryReporter();
9597
sinon.restore();
9698
});
9799

src/test/unittest/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export function run(): Promise<void> {
66
if ((Reflect as any).metadata === undefined) {
77
require('reflect-metadata');
88
}
9+
10+
process.env.VSC_PYTHON_UNIT_TEST = '1';
911
// Create the mocha test
1012
const mocha = new Mocha({
1113
ui: 'tdd',

tsconfig.json

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
],
1616
"rootDir": "src",
1717
"strict": true, /* enable all strict type-checking options */
18+
"types": [
19+
"chai-as-promised"
20+
],
1821
"experimentalDecorators": true,
1922
"allowSyntheticDefaultImports": true,
2023
"noImplicitAny": true,

0 commit comments

Comments
 (0)