Skip to content

Commit

Permalink
Support stricter CSP with nonce (#411)
Browse files Browse the repository at this point in the history
When Content-Security-Policy is set to `style-src 'self';`, inline
styles are disallowed.

An easy fix is to allow passing a nonce and move the inline style to a
style block.

Happy to discuss if you have any questions about this, or if there's a
better approach!

---------

Co-authored-by: Sergio Xalambrí <[email protected]>
  • Loading branch information
Bathlamos and sergiodxa authored Jan 20, 2025
1 parent eed0c80 commit eec18cf
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/react/honeypot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ const HoneypotContext = React.createContext<HoneypotContextType>({});

export function HoneypotInputs({
label = "Please leave this field blank",
}: {
label?: string;
}) {
nonce,
className = "__honeypot_inputs",
}: HoneypotInputs.Props) {
let context = React.useContext(HoneypotContext);

let {
Expand All @@ -19,11 +19,8 @@ export function HoneypotInputs({
} = context;

return (
<div
id={`${nameFieldName}_wrap`}
style={{ display: "none" }}
aria-hidden="true"
>
<div id={`${nameFieldName}_wrap`} className={className} aria-hidden="true">
<style nonce={nonce}>{".__honeypot_inputs { display: none; }"}</style>
<label htmlFor={nameFieldName}>{label}</label>
<input
id={nameFieldName}
Expand Down Expand Up @@ -51,6 +48,18 @@ export function HoneypotInputs({
);
}

export namespace HoneypotInputs {
export type Props = {
label?: string;
nonce?: string;
/**
* The classname used to link the Honeypot input with the CSS that hides it.
* @default "__honeypot_inputs"
*/
className?: string;
};
}

export type HoneypotProviderProps = HoneypotContextType & {
children: React.ReactNode;
};
Expand Down

0 comments on commit eec18cf

Please sign in to comment.