Skip to content

Add condition for pdf file type in safari issue #20455

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

Closed
wants to merge 2 commits into from

Conversation

tyroneyeh
Copy link
Contributor

Closes #20404

ctx.Resp.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; sandbox")
if !st.IsPDF() {
ctx.Resp.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; sandbox")
}
Copy link
Member

Choose a reason for hiding this comment

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

You may as well remove the || st.IsPDF() above for the same effect.

Copy link
Member

Choose a reason for hiding this comment

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

I think this was added here for security reasons otherwise JavaScript in pdf would be executed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You may as well remove the || st.IsPDF() above for the same effect.

yes, modified

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this was added here for security reasons otherwise JavaScript in pdf would be executed

With or without policy javascript will be executed

image

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Jul 22, 2022
@silverwind
Copy link
Member

silverwind commented Jul 22, 2022

What is the exact problem? Assuming it works in other browsers, what does Safari not like?

@techknowlogick
Copy link
Member

I'm rather hesitant about this PR, as it removes some protections that were put in place to restrict XSS from PDFs

Copy link
Contributor

@zeripath zeripath left a comment

Choose a reason for hiding this comment

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

@techknowlogick agreed and to that end I'm going to mark this PR as request changes until these concerns about security are addressed.

@tyroneyeh
Copy link
Contributor Author

What is the exact problem? Assuming it works in other browsers, what does Safari not like?

With policy header only safari browser can't display pdf file in browser, pdf only direct to download

@tyroneyeh
Copy link
Contributor Author

test GitHub pdf attachment
js_injected_PentestTools-WebsiteScanner-report.pdf

@silverwind
Copy link
Member

silverwind commented Jul 23, 2022

The root problem here is the sandbox directive, some browser opt to not render a PDF with that directive present because of potentially untrusted content (see whatwg/html#3958 (comment)) because browsers can not fully "sandbox" pdf content.

I'm not sure what the worst-case scenario of a pdf attack will be, but if we can come up with a best-effort CSP for PDFs that does not rely on the sandbox attribute, it should be acceptable. I'd expect modern browser's embedded PDF viewers to restrict the attack vectors (mainly js inside pdf) already.

Note that the issue is generally only relevant when such user content is served on the same origin, which I think is true for most gitea instances.

@silverwind
Copy link
Member

#20460 will fix the content-type handling and also send the correct pdf type by default.

@silverwind
Copy link
Member

#20464 fixes the content-type and more. What this PR should do is remove sandbox for PDF and possibly SVG as well, after it has been demostrated it is safe to do so.

@tyroneyeh
Copy link
Contributor Author

#20455 (comment)

Even with Content Security Policy javascript will still be executed in chrome browser

@silverwind
Copy link
Member

I've added the sandbox attribute removal in #20464 to make it work in Safari, so this PR is now obsolete.

@silverwind silverwind closed this Jul 24, 2022
@tyroneyeh tyroneyeh deleted the 20404-safari_pdf_issue branch July 24, 2022 13:40
6543 pushed a commit that referenced this pull request Jul 29, 2022
- Always respect the user's configured mime type map
- Allow more types like image/pdf/video/audio to serve with correct content-type
- Shorten cache duration of raw files to 5 minutes, matching GitHub
- Don't set `content-disposition: attachment`, let the browser decide whether it wants to download or display a file directly
- Implement rfc5987 for filenames, remove previous hack. Confirmed it working in Safari.
- Make PDF attachment work in Safari by removing `sandbox` attribute.

This change will make a lot more file types open directly in browser now. Logic should generally be more readable than before with less `if` nesting and such.

Replaces: #20460
Replaces: #20455
Fixes: #20404
silverwind added a commit to silverwind/gitea that referenced this pull request Jul 29, 2022
- Always respect the user's configured mime type map
- Allow more types like image/pdf/video/audio to serve with correct content-type
- Shorten cache duration of raw files to 5 minutes, matching GitHub
- Don't set `content-disposition: attachment`, let the browser decide whether it wants to download or display a file directly
- Implement rfc5987 for filenames, remove previous hack. Confirmed it working in Safari.
- Make PDF attachment work in Safari by removing `sandbox` attribute.

This change will make a lot more file types open directly in browser now. Logic should generally be more readable than before with less `if` nesting and such.

Replaces: go-gitea#20460
Replaces: go-gitea#20455
Fixes: go-gitea#20404
6543 pushed a commit that referenced this pull request Jul 30, 2022
- Always respect the user's configured mime type map
- Allow more types like image/pdf/video/audio to serve with correct content-type
- Shorten cache duration of raw files to 5 minutes, matching GitHub
- Don't set `content-disposition: attachment`, let the browser decide whether it wants to download or display a file directly
- Implement rfc5987 for filenames, remove previous hack. Confirmed it working in Safari.
- Make PDF attachment work in Safari by removing `sandbox` attribute.

This change will make a lot more file types open directly in browser now. Logic should generally be more readable than before with less `if` nesting and such.

Replaces: #20460
Replaces: #20455
Fixes: #20404
vsysoev pushed a commit to IntegraSDL/gitea that referenced this pull request Aug 10, 2022
- Always respect the user's configured mime type map
- Allow more types like image/pdf/video/audio to serve with correct content-type
- Shorten cache duration of raw files to 5 minutes, matching GitHub
- Don't set `content-disposition: attachment`, let the browser decide whether it wants to download or display a file directly
- Implement rfc5987 for filenames, remove previous hack. Confirmed it working in Safari.
- Make PDF attachment work in Safari by removing `sandbox` attribute.

This change will make a lot more file types open directly in browser now. Logic should generally be more readable than before with less `if` nesting and such.

Replaces: go-gitea#20460
Replaces: go-gitea#20455
Fixes: go-gitea#20404
@go-gitea go-gitea locked and limited conversation to collaborators May 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

pdf inline viewer showing blocked plugin on issues attachment using safari
6 participants