-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix(nestjs): Check arguments before instrumenting with @Injectable
#13544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
a703dc4
7a154f9
7f7bac6
5233460
4f2d735
5f79bc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -8,7 +8,7 @@ import { | |||||
import { getActiveSpan, startInactiveSpan, startSpan, startSpanManual, withActiveSpan } from '@sentry/core'; | ||||||
import type { Span } from '@sentry/types'; | ||||||
import { SDK_VERSION, addNonEnumerableProperty, isThenable } from '@sentry/utils'; | ||||||
import { getMiddlewareSpanOptions, instrumentObservable, isPatched } from './helpers'; | ||||||
import { getMiddlewareSpanOptions, getNextProxy, instrumentObservable, isPatched } from './helpers'; | ||||||
import type { CallHandler, CatchTarget, InjectableTarget, MinimalNestJsExecutionContext, Observable } from './types'; | ||||||
|
||||||
const supportedVersions = ['>=8.0.0 <11']; | ||||||
|
@@ -100,24 +100,24 @@ export class SentryNestInstrumentation extends InstrumentationBase { | |||||
|
||||||
target.prototype.use = new Proxy(target.prototype.use, { | ||||||
apply: (originalUse, thisArgUse, argsUse) => { | ||||||
// Middlewares have a request, response and next argument. | ||||||
if (argsUse.length < 3) { | ||||||
return originalUse.apply(thisArgUse, argsUse); | ||||||
} | ||||||
|
||||||
const [req, res, next, ...args] = argsUse; | ||||||
const prevSpan = getActiveSpan(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably also move this line below all the guarding. |
||||||
|
||||||
return startSpanManual(getMiddlewareSpanOptions(target), (span: Span) => { | ||||||
const nextProxy = new Proxy(next, { | ||||||
apply: (originalNext, thisArgNext, argsNext) => { | ||||||
span.end(); | ||||||
|
||||||
if (prevSpan) { | ||||||
return withActiveSpan(prevSpan, () => { | ||||||
return Reflect.apply(originalNext, thisArgNext, argsNext); | ||||||
}); | ||||||
} else { | ||||||
return Reflect.apply(originalNext, thisArgNext, argsNext); | ||||||
} | ||||||
}, | ||||||
}); | ||||||
// Check that we can reasonably assume that the target is a middleware. | ||||||
// Without these guards, instrumentation will fail if a function named 'use' on a service, which is | ||||||
// decorated with @Injectable, is called. | ||||||
if (!req || !res || !next || !(typeof next === 'function')) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return originalUse.apply(thisArgUse, argsUse); | ||||||
} | ||||||
|
||||||
return startSpanManual(getMiddlewareSpanOptions(target), (span: Span) => { | ||||||
// proxy next to end span on call | ||||||
const nextProxy = getNextProxy(next, span, prevSpan); | ||||||
return originalUse.apply(thisArgUse, [req, res, nextProxy, args]); | ||||||
}); | ||||||
}, | ||||||
|
@@ -133,6 +133,17 @@ export class SentryNestInstrumentation extends InstrumentationBase { | |||||
|
||||||
target.prototype.canActivate = new Proxy(target.prototype.canActivate, { | ||||||
apply: (originalCanActivate, thisArgCanActivate, argsCanActivate) => { | ||||||
// Guards have a context argument. | ||||||
if (argsCanActivate.length == 0) { | ||||||
return originalCanActivate.apply(thisArgCanActivate, argsCanActivate); | ||||||
} | ||||||
|
||||||
const context: MinimalNestJsExecutionContext = argsCanActivate[0]; | ||||||
|
||||||
if (!context) { | ||||||
return originalCanActivate.apply(thisArgCanActivate, argsCanActivate); | ||||||
} | ||||||
|
||||||
return startSpan(getMiddlewareSpanOptions(target), () => { | ||||||
return originalCanActivate.apply(thisArgCanActivate, argsCanActivate); | ||||||
}); | ||||||
|
@@ -148,6 +159,18 @@ export class SentryNestInstrumentation extends InstrumentationBase { | |||||
|
||||||
target.prototype.transform = new Proxy(target.prototype.transform, { | ||||||
apply: (originalTransform, thisArgTransform, argsTransform) => { | ||||||
// Pipes have a value and metadata argument. | ||||||
if (argsTransform.length < 2) { | ||||||
return originalTransform.apply(thisArgTransform, argsTransform); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is technically redundant with the assertions we have below. |
||||||
|
||||||
const value = argsTransform[0]; | ||||||
const metadata = argsTransform[1]; | ||||||
|
||||||
if (!value || !metadata) { | ||||||
return originalTransform.apply(thisArgTransform, argsTransform); | ||||||
} | ||||||
|
||||||
return startSpan(getMiddlewareSpanOptions(target), () => { | ||||||
return originalTransform.apply(thisArgTransform, argsTransform); | ||||||
}); | ||||||
|
@@ -163,12 +186,22 @@ export class SentryNestInstrumentation extends InstrumentationBase { | |||||
|
||||||
target.prototype.intercept = new Proxy(target.prototype.intercept, { | ||||||
apply: (originalIntercept, thisArgIntercept, argsIntercept) => { | ||||||
// Interceptors have a context and next argument. | ||||||
if (argsIntercept.length < 2) { | ||||||
return originalIntercept.apply(thisArgIntercept, argsIntercept); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is technically redundant with the assertions we have below. |
||||||
|
||||||
const context: MinimalNestJsExecutionContext = argsIntercept[0]; | ||||||
const next: CallHandler = argsIntercept[1]; | ||||||
|
||||||
const parentSpan = getActiveSpan(); | ||||||
let afterSpan: Span; | ||||||
|
||||||
// Check that we can reasonably assume that the target is an interceptor. | ||||||
if (!context || !next || !(typeof next.handle === 'function')) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
return originalIntercept.apply(thisArgIntercept, argsIntercept); | ||||||
} | ||||||
|
||||||
return startSpanManual(getMiddlewareSpanOptions(target), (beforeSpan: Span) => { | ||||||
// eslint-disable-next-line @typescript-eslint/unbound-method | ||||||
next.handle = new Proxy(next.handle, { | ||||||
|
@@ -263,6 +296,17 @@ export class SentryNestInstrumentation extends InstrumentationBase { | |||||
|
||||||
target.prototype.catch = new Proxy(target.prototype.catch, { | ||||||
apply: (originalCatch, thisArgCatch, argsCatch) => { | ||||||
if (argsCatch.length < 2) { | ||||||
return originalCatch.apply(thisArgCatch, argsCatch); | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This check is technically redundant with the assertions we have below. |
||||||
|
||||||
const exception = argsCatch[0]; | ||||||
const host = argsCatch[1]; | ||||||
|
||||||
if (!exception || !host) { | ||||||
return originalCatch.apply(thisArgCatch, argsCatch); | ||||||
} | ||||||
|
||||||
return startSpan(getMiddlewareSpanOptions(target), () => { | ||||||
return originalCatch.apply(thisArgCatch, argsCatch); | ||||||
}); | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is technically redundant with the assertions we have below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it? What if the function has 0 arguments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then an empty array will be passed to
argsUse
and all the arguments we check down below will be falsy (or ratherundefined
).