-
Notifications
You must be signed in to change notification settings - Fork 369
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
Types for policies #369
Comments
There are two reasons:
Neither of these reasons are iron-clad, though. What do you think? |
Definitely not how I expected it to work. Maybe an Please see the implementation example below. export enum CrossOriginEmbedderPolicy {
RequireCorp = "require-corp",
Credentialless = "credentialless",
}
export interface CrossOriginEmbedderPolicyOptions {
policy?: CrossOriginEmbedderPolicy;
}
function getHeaderValueFromOptions({
policy = CrossOriginEmbedderPolicy.RequireCorp,
}: Readonly<CrossOriginEmbedderPolicyOptions>): string {
// Checking directly on the values of the enum
if (Object.values(CrossOriginEmbedderPolicy).includes(policy)) {
return policy;
} else {
throw new Error(
`Cross-Origin-Embedder-Policy does not support the ${JSON.stringify(policy)} policy`
);
}
} I also understand that there may be a bit of work to be done for all policies considering that tests should be updated too. |
This is a good idea. I plan to add this to Helmet v6, the next major version. I implemented this in the |
I know you closed the issue but may I know why you avoided using enums? |
A few reasons.
Hope this helps! |
Okay I understand, thanks! Anyway I have created a branch in my forked repo with an alternative solution to the changes you already made trying to remove duplicate strings for some policies. Please have a look at this commit if you mind. |
I'll take a look. Thanks! |
Hi all!
I was wondering, what is the reason for checking the allowed policies at runtime over a string rather than setting a type for the specific policy itself?
This thought came to me looking at the code of the crossOriginEmbedderPolicy middleware.
Couldn't it be better to just set a custom type for the allowed policies?
Maybe something like this:
In this way it could help the user to know in advance which policies are allowed or managed by your middlewares without incurring in runtime errors.
Or maybe rather than removing the runtime it could just be added the type as showed above, if the allowed policies check is needed for people using JS.
Thank you and sorry in case I missed a discussion about this topic.
The text was updated successfully, but these errors were encountered: