Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Sentry = require('@sentry/aws-serverless');

exports.handler = async event => {
if (event.tag) {
Sentry.setTag('invocation_tag', event.tag);
}

Sentry.captureException(new Error(event.errorMessage));
};
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,33 @@ test.describe('Lambda layer', () => {
expect(transactionEvent.contexts?.trace?.parent_span_id).toEqual(parentSpanId);
});

test('does not leak isolation scope data between invocations on a warm container', async ({ lambdaClient }) => {
const firstErrorEventPromise = waitForError('aws-serverless-layer', errorEvent => {
return errorEvent?.exception?.values?.[0]?.value === 'first invocation error';
});
const secondErrorEventPromise = waitForError('aws-serverless-layer', errorEvent => {
return errorEvent?.exception?.values?.[0]?.value === 'second invocation error';
});

await lambdaClient.send(
new InvokeCommand({
FunctionName: 'LayerIsolationScope',
Payload: JSON.stringify({ tag: 'first-invocation', errorMessage: 'first invocation error' }),
}),
);
await lambdaClient.send(
new InvokeCommand({
FunctionName: 'LayerIsolationScope',
Payload: JSON.stringify({ errorMessage: 'second invocation error' }),
}),
);

const [firstErrorEvent, secondErrorEvent] = await Promise.all([firstErrorEventPromise, secondErrorEventPromise]);

expect(firstErrorEvent.tags?.invocation_tag).toBe('first-invocation');
expect(secondErrorEvent.tags?.invocation_tag).toBeUndefined();
});

test('extension tunnel validates DSN allowlist and rejects invalid envelopes', async ({ lambdaClient }) => {
const matchingMarker = `extension-tunnel-matching-${Date.now()}`;
const matchingRequestPromise = waitForRequest('aws-serverless-layer', requestData => {
Expand Down
16 changes: 5 additions & 11 deletions packages/aws-serverless/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Scope } from '@sentry/core';
import { debug } from '@sentry/core';
import { captureException, captureMessage, flush, getCurrentScope, withScope } from '@sentry/node';
import { captureException, captureMessage, flush, getCurrentScope, withIsolationScope, withScope } from '@sentry/node';
import type { Context, Handler, StreamifyHandler } from 'aws-lambda';
import { performance } from 'perf_hooks';
import { types } from 'util';
Expand Down Expand Up @@ -164,8 +164,6 @@ export function wrapHandler<TEvent, TResult>(
return wrapStreamingHandler(handler, options, START_TIME);
}

let timeoutWarningTimer: NodeJS.Timeout | undefined;

// AWSLambda is like Express. It makes a distinction about handlers based on its last argument
// async (event) => async handler
// async (event, context) => async handler
Expand Down Expand Up @@ -194,9 +192,8 @@ export function wrapHandler<TEvent, TResult>(
return async (event: TEvent, context: Context) => {
context.callbackWaitsForEmptyEventLoop = options.callbackWaitsForEmptyEventLoop;

timeoutWarningTimer = setupTimeoutWarning(context, options);

async function processResult(): Promise<TResult> {
const timeoutWarningTimer = setupTimeoutWarning(context, options);
const scope = getCurrentScope();

let rv: TResult;
Expand Down Expand Up @@ -226,7 +223,7 @@ export function wrapHandler<TEvent, TResult>(
return rv;
}

return withScope(async () => {
return withIsolationScope(async () => {
return processResult();
});
};
Expand All @@ -237,18 +234,15 @@ function wrapStreamingHandler<TEvent, TResult>(
options: WrapperOptions,
startTime: number,
): StreamifyHandler<TEvent, TResult> {
let timeoutWarningTimer: NodeJS.Timeout | undefined;

const wrappedHandler = async (
event: TEvent,
responseStream: Parameters<StreamifyHandler<TEvent, TResult>>[1],
context: Context,
): Promise<TResult> => {
context.callbackWaitsForEmptyEventLoop = options.callbackWaitsForEmptyEventLoop;

timeoutWarningTimer = setupTimeoutWarning(context, options);

async function processStreamingResult(): Promise<TResult> {
const timeoutWarningTimer = setupTimeoutWarning(context, options);
const scope = getCurrentScope();

try {
Expand All @@ -274,7 +268,7 @@ function wrapStreamingHandler<TEvent, TResult>(
}
}

return withScope(() => processStreamingResult());
return withIsolationScope(() => processStreamingResult());
};

const handlerWithSymbols = handler as unknown as Record<symbol, unknown>;
Expand Down
87 changes: 87 additions & 0 deletions packages/aws-serverless/test/isolation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import type { Event } from '@sentry/core';
import { addBreadcrumb, captureException, setUser } from '@sentry/node';
import type { Context, Handler, StreamifyHandler } from 'aws-lambda';
import { PassThrough } from 'stream';
import { beforeEach, describe, expect, test } from 'vitest';
import { init } from '../src/init';
import { AWS_HANDLER_STREAMING_RESPONSE, AWS_HANDLER_STREAMING_SYMBOL, wrapHandler } from '../src/sdk';

const events: Event[] = [];

init({
dsn: 'https://public@example.com/1',
beforeSend(event) {
events.push(event);
return null;
},
});

function createContext(awsRequestId: string, remainingTimeInMillis = 3000): Context {
return {
awsRequestId,
functionName: 'functionName',
functionVersion: '$LATEST',
invokedFunctionArn: 'arn:aws:lambda:us-east-1:123456789012:function:functionName',
logGroupName: 'logGroupName',
logStreamName: 'logStreamName',
getRemainingTimeInMillis: () => remainingTimeInMillis,
} as Context;
}

function handleRequest(event: { user?: string }): void {
if (event.user) {
setUser({ id: event.user });
addBreadcrumb({ message: `handled request for ${event.user}` });
} else {
captureException(new Error('anonymous request failed'));
}
}

function expectNoLeakedData(event: Event | undefined, awsRequestId: string): void {
expect(event?.user).toBeUndefined();
expect(event?.breadcrumbs).toBeUndefined();
expect(event?.contexts?.['aws.lambda']?.aws_request_id).toBe(awsRequestId);
}

describe('wrapHandler isolation', () => {
beforeEach(() => {
events.length = 0;
});

test('does not leak isolation scope data between invocations', async () => {
const handler = wrapHandler((async (event: { user?: string }) => handleRequest(event)) as Handler);

await (handler as any)({ user: 'alice' }, createContext('1'));
await (handler as any)({}, createContext('2'));

expect(events).toHaveLength(1);
expectNoLeakedData(events[0], '2');
});

test('does not leak isolation scope data between streaming invocations', async () => {
const streamingHandler = (async (event: { user?: string }) => handleRequest(event)) as unknown as StreamifyHandler;
(streamingHandler as any)[AWS_HANDLER_STREAMING_SYMBOL] = AWS_HANDLER_STREAMING_RESPONSE;
const handler = wrapHandler(streamingHandler);

await (handler as any)({ user: 'alice' }, new PassThrough(), createContext('1'));
await (handler as any)({}, new PassThrough(), createContext('2'));

expect(events).toHaveLength(1);
expectNoLeakedData(events[0], '2');
});

test('captures the timeout warning with the data of its own invocation', async () => {
const handler = wrapHandler((async (event: { user?: string; wait?: number }) => {
setUser({ id: event.user });
await new Promise(resolve => setTimeout(resolve, event.wait));
}) as Handler);

await (handler as any)({ user: 'alice', wait: 0 }, createContext('1'));
await (handler as any)({ user: 'bob', wait: 50 }, createContext('2', 510));

expect(events).toHaveLength(1);
expect(events[0]?.message).toBe('Possible function timeout: functionName');
expect(events[0]?.user).toEqual({ id: 'bob' });
expect(events[0]?.contexts?.['aws.lambda']?.aws_request_id).toBe('2');
});
});
9 changes: 6 additions & 3 deletions packages/aws-serverless/test/sdk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ vi.mock('@sentry/node', async () => {
mockWithScope(fn);
return fn(mockScope);
},
withIsolationScope: (fn: (scope: unknown) => unknown) => {
return fn(mockScope);
},
captureMessage: (...args: unknown[]) => {
mockCaptureMessage(...args);
},
Expand Down Expand Up @@ -118,7 +121,7 @@ describe('AWSLambda', () => {
const wrappedHandler = wrapHandler(handler);
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);

expect(mockWithScope).toBeCalledTimes(2);
expect(mockWithScope).toBeCalledTimes(1);
expect(mockCaptureMessage).toBeCalled();
expect(mockScope.setTag).toBeCalledWith('timeout', '1s');
});
Expand All @@ -134,7 +137,7 @@ describe('AWSLambda', () => {
});
await wrappedHandler(fakeEvent, fakeContext, fakeCallback);

expect(mockWithScope).toBeCalledTimes(1);
expect(mockWithScope).not.toBeCalled();
expect(mockCaptureMessage).not.toBeCalled();
expect(mockScope.setTag).not.toBeCalledWith('timeout', '1s');
});
Expand Down Expand Up @@ -524,7 +527,7 @@ describe('AWSLambda', () => {
const wrappedHandler = wrapHandler(streamingHandler);
await (wrappedHandler as any)(fakeEvent, mockResponseStream, fakeContext);

expect(mockWithScope).toBeCalledTimes(2);
expect(mockWithScope).toBeCalledTimes(1);
expect(mockCaptureMessage).toBeCalled();
expect(mockScope.setTag).toBeCalledWith('timeout', '1s');
});
Expand Down
Loading