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

Securitypolicyviolationevent sidebar #35384

Merged
merged 6 commits into from
Aug 12, 2024

Conversation

hamishwillee
Copy link
Collaborator

SecurityPolicyViolationEvent docs have HTTP sidebar, even though its a Web API Event. However it is defined in the HTTP CSP spec rather than an API.

I wanted to have it in its own API group, but given there is no way to show related APIs properly, decided against.
In the end I have made it as part of the Reporting_API.

The reporting API already includes the only other API entity in the CSP spec - CSPViolationReportBody. Even though that fits better here than the event does, it is not a completely unexpected fit - the content is related, and is already documented as being associated with CSP.
It isn't perfect, but IMO reasonable compromise.

FYI @chrisdavidmills @wbamberg

Fixes #35320

Part of work around #35279

@hamishwillee hamishwillee requested review from a team as code owners August 9, 2024 05:26
@hamishwillee hamishwillee requested review from wbamberg and bsmth and removed request for a team August 9, 2024 05:26
@github-actions github-actions bot added Content:WebAPI Web API docs Content:HTTP HTTP docs size/m [PR only] 51-500 LoC changed labels Aug 9, 2024
@@ -64,6 +62,15 @@ The Reporting API spec also defines a Generate Test Report [WebDriver](/en-US/do
- {{domxref("ReportingObserver")}}
- : An object that can be used to collect and access reports as they are generated.

### Related interfaces
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note "related". These are not explicitly extension to this API. Separated in this doc so that I can justifiably include the event.

@@ -34,4 +32,4 @@ document.addEventListener("securitypolicyviolation", (e) => {

## See also

- [Content Security Policy (CSP)](/en-US/docs/Web/HTTP/CSP)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, I moved this string up into the first descriptive sentence. If you don't have context you need to know what a policy is straight away.


> [!NOTE]
> It is recommended to add the handler for this event to a top level object (i.e. {{domxref("Window")}} or {{domxref("Document")}}). While the property exists in HTML elements, you can't assign a handler to the property until the elements have been loaded, by which time this event will already have fired.
> You should add the handler for this event to a top level object (i.e. {{domxref("Window")}} or {{domxref("Document")}}).
> While the property exists in HTML elements, you can't assign a handler to the property until the elements have been loaded, by which time this event will already have fired.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is true for dynamically constructed elements, such as document.createElement("img"). Not what this PR changed though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is saying if your static HTML element would trigger a violation, perhaps because it loads a restricted img, by the time you get to add your handler the event will have already fired? Makes sense, though I guess if you declare the handler inline that might be assigned and ready before the violation triggers. That could be browser dependent.

You're saying that you can add a dynamically constructed element with a handler, but the the policy violation isn't triggered until here is a global context - when you add it to the document. So that should work.

Would this fix it "enough"? I don't want to mess with this too much.

Suggested change
> While the property exists in HTML elements, you can't assign a handler to the property until the elements have been loaded, by which time this event will already have fired.
> This is because you can't define a handler for elements in your HTML file until they are fully loaded, by which time the event will already have fired.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth pointing out that the example doesn't demonstrate listening for this event on elements; maybe should be a follow-up fix?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by 9d74140 , though perhaps I should omit the statement about dynamic construction?

@@ -34,4 +32,4 @@ document.addEventListener("securitypolicyviolation", (e) => {

## See also

- [Content Security Policy (CSP)](/en-US/docs/Web/HTTP/CSP)
- [`CSPViolationReportBody.lineNumber`](/en-US/docs/Web/API/CSPViolationReportBody#cspviolationreportbody.linenumber)
Copy link
Member

@Josh-Cena Josh-Cena Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: link to the property page, even if it doesn't exist, so we don't need to update it in the future. (Same below)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but I don't think so. A link that works now is worth far more than one in future that might.

Copy link
Member

@Josh-Cena Josh-Cena Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I've added a check to https://jc-verse.github.io/mdn-graph/warnings (Replace DT link with real target) so this is in our backlog.

Copy link
Collaborator Author

@hamishwillee hamishwillee Aug 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be an excellent improvement. I wonder if the linter could do this automagically.

@Josh-Cena
Copy link
Member

FWIW—I have very little knowledge of this, but from what I know, my worry for this move is that reporting API spec itself is WIP and our current documentation doesn't even reflect the latest spec? #29292 So maybe this could give off the wrong signal on the availability of CSP APIs?

@hamishwillee
Copy link
Collaborator Author

FWIW—I have very little knowledge of this, but from what I know, my worry for this move is that reporting API spec itself is WIP and our current documentation doesn't even reflect the latest spec? #29292 So maybe this could give off the wrong signal on the availability of CSP APIs?

AFAIK this is no longer WIP. The "legacy version 0" was Chrome only, as is the Report-To header. However the new spec is being adopted by FF as well - I picked this up because I am documenting this stuff in #35279
The current docs are not quite right - but not as far off as you might think.
This will also not be experimental from FF130, since it will have two implementations.

Upshot, I'm not concerned about this. I am more concerned about whether this is discoverable and appropriate. My reasoning for putting it here is that there is prior-art, in that CSPViolationReportBody is part of the CSP "API" too, and that is here. With proper cross linking, which I have done, I think this is "better" than what exists now.

The only better alternative would require (long requested) infrastructure fixes to allow the sidebar to properly cross link items across different parts of the web platform to make them related. If that happens then I'd consider having a CSP API doc. I'm not 100% certain though, because I like CSPViolationReportBody as part of the reporting API.

@Josh-Cena
Copy link
Member

Okay, if you think CSPViolationReportBody belongs here, then rightfully everything else should too.

@hamishwillee
Copy link
Collaborator Author

Okay, if you think CSPViolationReportBody belongs here, then rightfully everything else should too.

I think it is where it "most belongs" given the limitations of the infrastructure. Can you approve this?

Copy link
Member

@Josh-Cena Josh-Cena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll merge this for all the improvements it contains, and I'll write a separate issue for the claim about "should always attach listeners to document or window".

@Josh-Cena Josh-Cena merged commit a5c1400 into mdn:main Aug 12, 2024
9 checks passed
@hamishwillee
Copy link
Collaborator Author

Thank you!

@hamishwillee hamishwillee deleted the securitypolicyviolationevent_sidebar branch August 12, 2024 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:HTTP HTTP docs Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SecurityPolicyViolationEvent docs have unexpected HTTP sidebar
2 participants