helmetjs / helmet

Help secure Express apps with various HTTP headers

Home Page:https://helmetjs.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Export typings for individual middleware options

billyjanitsch opened this issue · comments

Hi! Currently, Helmet exports the top-level options type (HelmetOptions) but not any of the constituent middleware options types (ContentSecurityPolicyOptions, etc.).

As a workaround, it's possible to extract them from the top-level type, but doing so isn't very ergonomic because they're mixed in with booleans and null values, so you have to do something like:

import {type HelmetOptions} from 'helmet'

type ContentSecurityPolicyOptions = Extract<HelmetOptions['contentSecurityPolicy'], object>

Would you be open to exporting all of the middleware options types so that this isn't necessary?

Would something like this work for you?

type ContentSecurityPolicyOptions = Parameters<typeof helmet.contentSecurityPolicy>[0];

I'm going to close this issue because there hasn't been much movement on it. Let me know if you'd like me to reopen.

Hey @EvanHahn,
we are currently upgrading from 6.1.0 to 6.2.0.

Before we were able to import ContentSecurityPolicyOptions through:

import { ContentSecurityPolicyOptions } from 'helmet/dist/types/middlewares/content-security-policy';

not pretty but it did the job.

in 6.2.0 I tried both:

type ContentSecurityPolicyOptions = Parameters<typeof helmet.contentSecurityPolicy>[0];

and

import {type HelmetOptions} from 'helmet'

type ContentSecurityPolicyOptions = Extract<HelmetOptions['contentSecurityPolicy'], object>

neither of the 2 suggested solutions work, due to the following error:
TS4023

Exported variable 'contentSecurityPolicies' has or is using name 'ContentSecurityPolicyOptions' from external module `[PROJECT_PATH]/node_modules/helmet/index" but cannot be named.

most likely due to ContentSecurityPolicyOptions not being exported

is there a way, you can export the in between interfaces as well?

@judithhartmann Does something like this work for you?

import type { HelmetOptions } from "helmet";

type ContentSecurityPolicyOptions = HelmetOptions["contentSecurityPolicy"];

const cspOptions: ContentSecurityPolicyOptions = {
  directives: {
    defaultSrc: ["'self'"],
    // ...
  },
};

@EvanHahn No i get the same error unless i explicity export ContentSecurityPolicyOptions

Exported variable 'contentSecurityPolicies' has or is using name 'ContentSecurityPolicyOptions' from external module "[PATH]/node_modules/helmet/index" but cannot be named.

Typescript version 4.9.5 btw

@judithhartmann Does this happen with the latest version of Helmet, too? If so, could you open a new issue with code that reproduces this problem?