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

Self-diagnosis disabled on production #2199

Merged
merged 5 commits into from
Nov 20, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ origins of errors that could happen in runtime and be handled the following way:
Consider enabling production mode by setting `NODE_ENV` environment variable to `production` for your deployment:

- Express activates some [performance optimizations](https://expressjs.com/en/advanced/best-practice-performance.html);
- Self-diagnosis for potential problems is disabled to ensure faster startup;
- The `defaultResultHandler`, `defaultEndpointsFactory` and `LastResortHandler` generalize server-side error messages
in negative responses in order to improve the security of your API by not disclosing the exact causes of errors:
- Throwing errors that have or imply `5XX` status codes become just `Internal Server Error` message in response;
Expand Down
41 changes: 41 additions & 0 deletions src/diagnostics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { FlatObject } from "./common-helpers";
import { contentTypes } from "./content-type";
import { assertJsonCompatible } from "./deep-checks";
import { AbstractEndpoint } from "./endpoint";
import { ActualLogger } from "./logger-helpers";

export class Diagnostics {
#verified = new WeakSet<AbstractEndpoint>();

public check(
endpoint: AbstractEndpoint,
logger: ActualLogger,
ctx: FlatObject,
): void {
if (!this.#verified.has(endpoint)) {
if (endpoint.getRequestType() === "json") {
try {
assertJsonCompatible(endpoint.getSchema("input"), "in");
} catch (reason) {
logger.warn(
"The final input schema of the endpoint contains an unsupported JSON payload type.",
Object.assign(ctx, { reason }),
);
}
}
for (const variant of ["positive", "negative"] as const) {
if (endpoint.getMimeTypes(variant).includes(contentTypes.json)) {
try {
assertJsonCompatible(endpoint.getSchema(variant), "out");
} catch (reason) {
logger.warn(
`The final ${variant} response schema of the endpoint contains an unsupported JSON payload type.`,
Object.assign(ctx, { reason }),
);
}
}
}
this.#verified.add(endpoint);
}
}
}
36 changes: 6 additions & 30 deletions src/routing.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { IRouter, RequestHandler } from "express";
import { isProduction } from "./common-helpers";
import { CommonConfig } from "./config-type";
import { ContentType, contentTypes } from "./content-type";
import { assertJsonCompatible } from "./deep-checks";
import { ContentType } from "./content-type";
import { DependsOnMethod } from "./depends-on-method";
import { Diagnostics } from "./diagnostics";
import { AbstractEndpoint } from "./endpoint";
import { ActualLogger } from "./logger-helpers";
import { walkRouting } from "./routing-walker";
Expand Down Expand Up @@ -30,40 +31,15 @@ export const initRouting = ({
routing: Routing;
parsers?: Parsers;
}) => {
const verified = new WeakSet<AbstractEndpoint>();
const doc = new Diagnostics();
walkRouting({
routing,
hasCors: !!config.cors,
onEndpoint: (endpoint, path, method, siblingMethods) => {
const requestType = endpoint.getRequestType();
if (!verified.has(endpoint)) {
if (requestType === "json") {
try {
assertJsonCompatible(endpoint.getSchema("input"), "in");
} catch (reason) {
rootLogger.warn(
"The final input schema of the endpoint contains an unsupported JSON payload type.",
{ path, method, reason },
);
}
}
for (const variant of ["positive", "negative"] as const) {
if (endpoint.getMimeTypes(variant).includes(contentTypes.json)) {
try {
assertJsonCompatible(endpoint.getSchema(variant), "out");
} catch (reason) {
rootLogger.warn(
`The final ${variant} response schema of the endpoint contains an unsupported JSON payload type.`,
{ path, method, reason },
);
}
}
}
verified.add(endpoint);
}
if (!isProduction()) doc.check(endpoint, rootLogger, { path, method });
app[method](
path,
...(parsers?.[requestType] || []),
...(parsers?.[endpoint.getRequestType()] || []),
async (request, response) =>
endpoint.execute({
request,
Expand Down