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

Allow render HTML with css/js external links #19017

Merged
merged 31 commits into from Jun 16, 2022

Conversation

lunny
Copy link
Member

@lunny lunny commented Mar 7, 2022

This PR allows to render some HTMLs which has <style>, <script> tags when SANITIZER disabled. This also makes it simpler when configuring some external renderers.

@lunny lunny added the type/enhancement An improvement of existing functionality label Mar 7, 2022
@6543 6543 added this to the 1.17.0 milestone Mar 7, 2022
Copy link
Member

@techknowlogick techknowlogick left a comment

Choose a reason for hiding this comment

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

I'm concerned with this PR because someone could link to /user/repo/render/.... and have XSS execute. I know that they chose to do this by disabling SANITIZER, but this seems especially dangerous. If merged, we should at least create a big warning in docs, and perhaps in admin saying the config is insecure.

@GiteaBot GiteaBot added the lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. label Mar 8, 2022
@lunny
Copy link
Member Author

lunny commented Mar 13, 2022

I'm concerned with this PR because someone could link to /user/repo/render/.... and have XSS execute. I know that they chose to do this by disabling SANITIZER, but this seems especially dangerous. If merged, we should at least create a big warning in docs, and perhaps in admin saying the config is insecure.

Warning added in the docs.

@techknowlogick
Copy link
Member

Could we use Content Security Policy headers to prevent any XSS?

@lunny
Copy link
Member Author

lunny commented May 31, 2022

Could we use Content Security Policy headers to prevent any XSS?

Done.

modules/markup/renderer.go Outdated Show resolved Hide resolved
routers/web/repo/render.go Outdated Show resolved Hide resolved
routers/web/repo/render.go Outdated Show resolved Hide resolved
routers/web/repo/view.go Show resolved Hide resolved
routers/web/repo/view.go Show resolved Hide resolved
routers/web/repo/render.go Outdated Show resolved Hide resolved
Co-authored-by: delvh <dev.lh@web.de>
Copy link
Member

@delvh delvh left a comment

Choose a reason for hiding this comment

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

Apart from the deprecation issue where we have yet to find a solution LGTM.

@GiteaBot GiteaBot added lgtm/need 1 This PR needs approval from one additional maintainer to be merged. and removed lgtm/need 2 This PR needs two approvals by maintainers to be considered for merging. labels Jun 11, 2022
@delvh
Copy link
Member

delvh commented Jun 14, 2022

#19017 (comment):

DISABLE_SANITIZER is not in 1.16. It's a good chance to make it clear.

So it is not breaking for users who are on a stable version, which should be almost everyone.

@lunny
Copy link
Member Author

lunny commented Jun 14, 2022

#19017 (comment):

DISABLE_SANITIZER is not in 1.16. It's a good chance to make it clear.

So it is not breaking for users who are on a stable version, which should be almost everyone.

If that, we need a migration and deprecated warning in setting.

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Jun 14, 2022

Beside the option, still one more thing (sorry for bothering ...)

Now the iframe is using sandbox="allow-same-origin allow-scripts", then the rendered JS can access to the window.config which contains CSRF token, then the JS can make requests as the current users. Is it fine?

image

image


update: fixed.

@delvh
Copy link
Member

delvh commented Jun 14, 2022

#19017 (comment):
Now we certainly know that we cannot allow that.

If we allow that, we have an obvious and unintended privilege escalation.
I am strongly against allowing the scripts now.

@wxiaoguang
Copy link
Contributor

#19017 (comment): Now we certainly know that we cannot allow that.

If we allow that, we have an obvious and unintended privilege escalation. I am strongly against allowing the scripts now.

Only allow-scripts can be used here. I am working on it.

Copy link
Member

@techknowlogick techknowlogick left a comment

Choose a reason for hiding this comment

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

temporarily blocking per my discussion w/ delvh

@wxiaoguang
Copy link
Contributor

wxiaoguang commented Jun 14, 2022

OK, all fixed. Now the iframe is protected by browser's same-origin policy. @techknowlogick

Uncaught DOMException: Blocked a frame with origin "null" from accessing a cross-origin frame.

Refer: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-sandbox

If this token (allow-same-origin) is not used, the resource is treated as being from a special origin that always fails the same-origin policy (potentially preventing access to data storage/cookies and some JavaScript APIs).

custom/conf/app.example.ini Outdated Show resolved Hide resolved
@codecov-commenter
Copy link

Codecov Report

Merging #19017 (c7df5bf) into main (7d1770c) will decrease coverage by 0.18%.
The diff coverage is 19.25%.

@@            Coverage Diff             @@
##             main   #19017      +/-   ##
==========================================
- Coverage   47.36%   47.18%   -0.19%     
==========================================
  Files         967      968       +1     
  Lines      134131   134224      +93     
==========================================
- Hits        63530    63332     -198     
- Misses      62876    63178     +302     
+ Partials     7725     7714      -11     
Impacted Files Coverage Δ
modules/markup/console/console.go 38.46% <ø> (+2.74%) ⬆️
modules/markup/csv/csv.go 29.16% <ø> (+0.88%) ⬆️
modules/markup/external/external.go 1.28% <0.00%> (-0.04%) ⬇️
modules/markup/markdown/markdown.go 58.78% <ø> (-0.55%) ⬇️
modules/markup/orgmode/orgmode.go 52.88% <ø> (+0.99%) ⬆️
modules/setting/markup.go 4.90% <0.00%> (-0.72%) ⬇️
routers/web/repo/compare.go 46.95% <0.00%> (ø)
routers/web/repo/render.go 0.00% <0.00%> (ø)
modules/markup/renderer.go 57.80% <15.15%> (-10.48%) ⬇️
routers/web/repo/view.go 42.07% <73.68%> (+0.80%) ⬆️
... and 14 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7d1770c...c7df5bf. Read the comment docs.

@lunny lunny merged commit b01dce2 into go-gitea:main Jun 16, 2022
@lunny lunny deleted the lunny/render_iframe branch June 16, 2022 03:33
zjjhot added a commit to zjjhot/gitea that referenced this pull request Jun 16, 2022
* giteaofficial/main:
  Allow render HTML with css/js external links (go-gitea#19017)
  Use correct count for `NumOpenIssues` (go-gitea#19980)
  In code search, get code unit accessible repos in one (main) query (go-gitea#19764)
  [skip ci] Updated translations via Crowdin
  Always try to fetch repo for mirrors (go-gitea#19975)
  Remove tab/TabName usage where it's not needed (go-gitea#19973)
  Fix cli command restore-repo: "units" should be parsed as StringSlice (go-gitea#19953)
  Uppercase first languages letters (go-gitea#19965)
  Move tests as seperate sub packages to reduce duplicated file names (go-gitea#19951)
  Replace unstyled meter with progress (go-gitea#19968)
  [skip ci] Updated translations via Crowdin
  [skip ci] Updated translations via Crowdin
  Remove singuliere from MAINTAINERS (go-gitea#19883)
  Fix aria for logo (go-gitea#19955)
  Fix mirror template bug (go-gitea#19959)
vsysoev pushed a commit to IntegraSDL/gitea that referenced this pull request Aug 10, 2022
* Allow render HTML with css/js external links

* Fix bug because of filename escape chars

* Fix lint

* Update docs about new configuration item

* Fix bug of render HTML in sub directory

* Add CSP head for displaying iframe in rendering file

* Fix test

* Apply suggestions from code review

Co-authored-by: delvh <dev.lh@web.de>

* Some improvements

* some improvement

* revert change in SanitizerDisabled of external renderer

* Add sandbox for iframe and support allow-scripts and allow-same-origin

* refactor

* fix

* fix lint

* fine tune

* use single option RENDER_CONTENT_MODE, use sandbox=allow-scripts

* fine tune CSP

* Apply suggestions from code review

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>

Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
AbdulrhmnGhanem pushed a commit to kitspace/gitea that referenced this pull request Aug 24, 2022
* Allow render HTML with css/js external links

* Fix bug because of filename escape chars

* Fix lint

* Update docs about new configuration item

* Fix bug of render HTML in sub directory

* Add CSP head for displaying iframe in rendering file

* Fix test

* Apply suggestions from code review

Co-authored-by: delvh <dev.lh@web.de>

* Some improvements

* some improvement

* revert change in SanitizerDisabled of external renderer

* Add sandbox for iframe and support allow-scripts and allow-same-origin

* refactor

* fix

* fix lint

* fine tune

* use single option RENDER_CONTENT_MODE, use sandbox=allow-scripts

* fine tune CSP

* Apply suggestions from code review

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>

Co-authored-by: delvh <dev.lh@web.de>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
PlushBeaver added a commit to PlushBeaver/gitea that referenced this pull request Sep 6, 2022
It was always attempted to read the file to be rendered as UTF-8.
The encoding was determined heuristically, regardless of the file type.
Sometimes binary files were not recognized as binary.
Renderers of binary formats were then fed with corrupted data:
binary stream treated as text and encoded in UTF-8.
Only apply heuristics for textual formats, read other formats as-is.

Fixes: b01dce2 ("Allow render HTML with css/js external links (go-gitea#19017)")
Cc: xiaolunwen@gmail.com

Signed-off-by: Dmitry Kozlyuk <dmitry.kozliuk@gmail.com>
@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/done This PR has enough approvals to get merged. There are no important open reservations anymore. type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants