Skip to content

Commit b1fe2c8

Browse files
authored
Prevent loading plugins for incorrect module open-telemetry#626 (open-telemetry#653)
* fix: do not load plugin when they patch a different module than defined in config open-telemetry#626
1 parent 56a9745 commit b1fe2c8

11 files changed

Lines changed: 46 additions & 2 deletions

File tree

packages/opentelemetry-api/src/trace/instrumentation/Plugin.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export interface Plugin<T = any> {
2828
*/
2929
supportedVersions?: string[];
3030

31+
/**
32+
* Name of the module that the plugin instrument.
33+
*/
34+
moduleName: string;
35+
3136
/**
3237
* Method that enables the instrumentation patch.
3338
* @param moduleExports The value of the `module.exports` property that would

packages/opentelemetry-core/src/trace/instrumentation/BasePlugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import * as path from 'path';
2929
/** This class represent the base to patch plugin. */
3030
export abstract class BasePlugin<T> implements Plugin<T> {
3131
supportedVersions?: string[];
32-
readonly moduleName?: string; // required for internalFilesExports
32+
abstract readonly moduleName: string; // required for internalFilesExports
3333
readonly version?: string; // required for internalFilesExports
3434
protected readonly _basedir?: string; // required for internalFilesExports
3535

packages/opentelemetry-node/src/instrumentation/PluginLoader.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,16 @@ export class PluginLoader {
132132
// Expecting a plugin from module;
133133
try {
134134
const plugin: Plugin = require(modulePath).plugin;
135-
136135
if (!utils.isSupportedVersion(version, plugin.supportedVersions)) {
136+
this.logger.error(
137+
`PluginLoader#load: Plugin ${name} only supports module ${plugin.moduleName} with the versions: ${plugin.supportedVersions}`
138+
);
139+
return exports;
140+
}
141+
if (plugin.moduleName !== name) {
142+
this.logger.error(
143+
`PluginLoader#load: Entry ${name} use a plugin that instruments ${plugin.moduleName}`
144+
);
137145
return exports;
138146
}
139147

packages/opentelemetry-node/test/instrumentation/PluginLoader.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,13 @@ const alreadyRequiredPlugins: Plugins = {
9393
},
9494
};
9595

96+
const differentNamePlugins: Plugins = {
97+
'random-module': {
98+
enabled: true,
99+
path: '@opentelemetry/plugin-http-module',
100+
},
101+
};
102+
96103
describe('PluginLoader', () => {
97104
const provider = new NoopTracerProvider();
98105
const logger = new NoopLogger();
@@ -243,6 +250,16 @@ describe('PluginLoader', () => {
243250
pluginLoader.load(alreadyRequiredPlugins);
244251
pluginLoader.unload();
245252
});
253+
254+
it('should not load a plugin that patches a different module that the one configured', () => {
255+
const pluginLoader = new PluginLoader(provider, logger);
256+
assert.strictEqual(pluginLoader['_plugins'].length, 0);
257+
pluginLoader.load(differentNamePlugins);
258+
// @ts-ignore only to trigger the loading of the plugin
259+
const randomModule = require('random-module');
260+
assert.strictEqual(pluginLoader['_plugins'].length, 0);
261+
pluginLoader.unload();
262+
});
246263
});
247264

248265
describe('.unload()', () => {

packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-http-module/http-module.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-notsupported-module/simple-module.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-simple-module/simple-module.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opentelemetry-node/test/instrumentation/node_modules/@opentelemetry/plugin-supported-module/simple-module.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opentelemetry-node/test/instrumentation/node_modules/random-module/index.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/opentelemetry-node/test/instrumentation/node_modules/random-module/package.json

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)