Consider a NestJS application where Helmet middleware is enabled globally. What does Helmet primarily do for your app?
import helmet from 'helmet'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.use(helmet()); await app.listen(3000); } bootstrap();
Think about what security headers do in web apps.
Helmet sets various HTTP headers that help protect your app from well-known web vulnerabilities like cross-site scripting, clickjacking, and others.
You want to enable CORS only for the origin 'https://example.com' in your NestJS app. Which code snippet does this correctly?
Check the NestJS method for enabling CORS with options.
In NestJS, enableCors is the built-in method to enable CORS with options. Passing an object with origin key sets allowed origins.
Review the code below. The app does not seem to have Helmet security headers applied. What is the cause?
import helmet from 'helmet'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); app.use(helmet); await app.listen(3000); } bootstrap();
Check how middleware functions are passed in Express-based frameworks.
Helmet is a function that returns middleware. You must call it (helmet()) to get the middleware function. Passing helmet without parentheses passes the function itself, not the middleware.
After enabling Helmet middleware with default settings in a NestJS app, which HTTP header will NOT be present in the response?
app.use(helmet());
Helmet sets security headers, but CORS headers come from a different middleware.
Helmet sets security-related headers like X-Content-Type-Options, X-DNS-Prefetch-Control, and X-Frame-Options. The Access-Control-Allow-Origin header is set by CORS middleware, not Helmet.
In a NestJS app, both CORS and Helmet middleware are enabled. What best describes how they work together?
Think about the purpose of CORS and security headers separately.
CORS middleware controls which external domains can access your app's resources by setting Access-Control-Allow-Origin and related headers. Helmet adds various security headers to protect against attacks like clickjacking and XSS. They serve different but complementary roles.