Skip to content
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

refactor(core): report forwarded headers #5907

Merged
merged 1 commit into from
May 21, 2024
Merged
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
30 changes: 13 additions & 17 deletions packages/core/src/utils/request.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type ExceptionTelemetry } from '@logto/app-insights/node';
import { type Context } from 'koa';
import { type ExtendableContext } from 'koa';

// eslint-disable-next-line @typescript-eslint/ban-types
const getRequestIdFromContext = (context: object): string | undefined => {
Expand All @@ -8,26 +8,22 @@ const getRequestIdFromContext = (context: object): string | undefined => {
}
};

const getHostFromContext = (context: Context): string | undefined => {
if ('host' in context.headers && typeof context.headers.host === 'string') {
return context.headers.host;
}
};

// eslint-disable-next-line @typescript-eslint/ban-types
export const buildAppInsightsTelemetry = (context: object): Partial<ExceptionTelemetry> => {
export const buildAppInsightsTelemetry = (
context: ExtendableContext
): Partial<ExceptionTelemetry> => {
const requestId = getRequestIdFromContext(context);
// eslint-disable-next-line no-restricted-syntax
const host = getHostFromContext(context as Context);

if (!requestId && !host) {
return {};
}
const {
host,
'x-forwarded-proto': xForwardedProto,
'x-forwarded-host': xForwardedHost,
} = context.headers;

return {
properties: {
...(requestId ? { requestId } : {}),
...(host ? { host } : {}),
requestId,
host,
xForwardedProto,
xForwardedHost,
},
};
};
Loading