Just amending the dependencies used in a NodeJS/ Typescript application and have hit a snag with the Helmet change from version "3.23.2" to “4.5.0”.
I've removed the dependency "@types/helmet": "0.0.47" from the package.json file.
A compilation results in the following semantic errors:
src/loaders/security.ts(2,18): error TS2305: Module '"helmet"' has no exported member 'IHelmetContentSecurityPolicyDirectives'.
src/options.ts(1,10): error TS2305: Module '"helmet"' has no exported member 'IHelmetContentSecurityPolicyDirectives'.
options.ts includes:
import { IHelmetContentSecurityPolicyDirectives } from 'helmet';
export interface Options {
redirectUrl: string;
mountPath: string;
serviceName?: string;
views?: string | string[];
csp?: IHelmetContentSecurityPolicyDirectives;
i18n?: I18nOptions;
}
security.ts is defined as:
import { Application } from 'express';
import helmet, { IHelmetContentSecurityPolicyDirectives } from 'helmet';
import logger from '../lib/logger';
const configureSecurity = (app: Application, csp: IHelmetContentSecurityPolicyDirectives | undefined): void => {
logger.info('Configuring Security using Helmet');
const defaultSrc = (csp && csp.defaultSrc) || [];
const styleSrc = (csp && csp.styleSrc) || [];
const scriptSrc = (csp && csp.scriptSrc) || [];
app.use(helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: [...defaultSrc, "'self'"],
styleSrc: [...styleSrc, "'self'"],
scriptSrc: [
...scriptSrc,
"'self'",
"'sha256-+XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'",
"'sha256-+XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'",
],
},
},
}));
};
export default configureSecurity;
I can't work out what to use instead of IHelmetContentSecurityPolicyDirectives
for the csp type.
Maintainer of Helmet here.
Helmet v3 did not have official TypeScript definitions and Helmet v4 does. In short, you're seeing this problem because the community-made types for Helmet v3 don't map cleanly to the official types in Helmet v4.
You're looking for an alternative to
IHelmetContentSecurityPolicyDirectives
. Here's a snippet from Helmet's official type definitions for the Content Security Policy module:The
directives
key ofContentSecurityPolicyOptions
is the "reincarnation" of the oldIHelmetContentSecurityPolicyDirectives
.I see two options:
Define the type yourself, and then use it. For example:
You can then use
CspDirectives
in place of the oldIHelmetContentSecurityPolicyDirectives
type.Rework your code a bit.
You could update the
Options
interface to take all of Helmet's options:You can then use it in
configureSecurity
:Your actual code might vary a bit.