Skip to content
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

Add securitypolicyviolation plugin #136

Open
xPaw opened this issue May 23, 2024 · 4 comments
Open

Add securitypolicyviolation plugin #136

xPaw opened this issue May 23, 2024 · 4 comments

Comments

@xPaw
Copy link

xPaw commented May 23, 2024

I'm experimenting with something like this:

	document.addEventListener( 'securitypolicyviolation', ( e ) =>
	{
		if( !e.sourceFile.startsWith( location.origin ) )
		{
			return;
		}

		const error = new Error( `CSP Error ${e.violatedDirective}` );
		error.name = 'SecurityPolicyViolationError';

		Bugsnag.notify( error, {
			severity: 'info',
			metadata:
			{
				originalPolicy: e.originalPolicy,
				sourceFile: e.sourceFile,
				blockedURI: e.blockedURI
			}
		} );
	} );

This could be wrapped into a nicely formatted plugin.

@birtles
Copy link
Member

birtles commented May 28, 2024

Looks great! Are you interested in making a PR for this?

@xPaw
Copy link
Author

xPaw commented May 28, 2024

This might require more work as bugsnag groups these errors together inconsistently, perhaps it needs to create separate error names for each directive.

And as always with CSP there's some spam caused by browser extensions.

@birtles
Copy link
Member

birtles commented May 28, 2024

This might require more work as bugsnag groups these errors together inconsistently, perhaps it needs to create separate error names for each directive.

For what it's worth, we can simplify the code to just pass in a structured object rather than an Error object since we recognize them here:

Bugsnag.notify(
  {
    name: 'SecurityPolicyViolationError',
    message: `CSP Error ${e.violatedDirective}`,
  },
  {
    severity: 'info',
    metadata: {
      originalPolicy: e.originalPolicy,
      sourceFile: e.sourceFile,
      blockedURI: e.blockedURI,
    },
  },
);

I'm not quite sure why Bugsnag is grouping them differently. I think it uses the error class and top stack frame (ref) so it should be the same.

And as always with CSP there's some spam causes by browser extensions.

Yeah, I'm finding CSP reports mostly useless because of browser extensions.

@xPaw
Copy link
Author

xPaw commented May 28, 2024

The stacktrace being the same in this callback would cause the groupings yeah. But that's maybe not ideal for different classes of violations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants