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

[BUG] Latex-Support plugin doesn't implement CSP rules #6200

Closed
sherlcok314159 opened this issue Mar 14, 2024 · 34 comments
Closed

[BUG] Latex-Support plugin doesn't implement CSP rules #6200

sherlcok314159 opened this issue Mar 14, 2024 · 34 comments

Comments

@sherlcok314159
Copy link

Describe the bug
Latex-Support plugin does not work. It seems like the old latex plugin does not consider CSP rules.

图片

To Reproduce
Just install the latex plugin at here and then you will find the error.

Expected behavior
It should work smoothly.

Screenshots
None of latex is rendered.

图片

Environment information (please complete the following information):

  • Device: MacOS
  • OS: Ubuntu 20.04
  • Browser: Firefox / Edge
  • FreshRSS version: 1.23.1
  • PHP version: 8.2.7
  • Installation type: Docker-compose
@math-GH
Copy link
Contributor

math-GH commented Mar 14, 2024

Thanks for bringing up this issue. Please use the issue tracker of this extension: https://github.com/aledeg/xExtension-LatexSupport/issues

(Ping @aledeg )

@sherlcok314159
Copy link
Author

Hi! I am not intended to use this issue tracker. However, the latest commit of that plugin is 3 years ago. I have no way but to ask here. And this issue can be general for others who developing plugins or inserting something. I hope you can answer it if available.

@math-GH
Copy link
Contributor

math-GH commented Mar 14, 2024

I thought that the issue tracker of this extension is available for everyone. I apologize.

This extension uses inline code that is prohibited by the CSP (Content security policy) of FreshRSS. It is an important security layer. The fix needs to be done by the extension itself.

Could you please give some example feeds with LaTeX code? It would help to understand the issue better.

@sherlcok314159
Copy link
Author

Fine. You can try this feed with a CSS selector as #PostContent. Thanks for the quick response.

@Frenzie Frenzie changed the title [BUG] Latex-Support plugin does not work [BUG] Latex-Support plugin doesn't implement CSP rules Mar 14, 2024
@Frenzie
Copy link
Member

Frenzie commented Mar 14, 2024

(I rather doubt I have any special permissions there, opened one at aledeg/xExtension-LatexSupport#3.)

@sherlcok314159
Copy link
Author

sherlcok314159 commented Mar 16, 2024

any help?

@aledeg
Copy link
Member

aledeg commented Mar 18, 2024

I'll look into that

@aledeg
Copy link
Member

aledeg commented Mar 18, 2024

Hi! I am not intended to use this issue tracker. However, the latest commit of that plugin is 3 years ago. I have no way but to ask here. And this issue can be general for others who developing plugins or inserting something. I hope you can answer it if available.

There was no issue so far. There is no need to change when it's working. That's why there was no commit.

@sherlcok314159
Copy link
Author

Sorry. I beg to differ. Have you tried my feed yet? How could you say there was no issue? The console errors are almost bombarded. Or are you using very old freshrss?

@aledeg
Copy link
Member

aledeg commented Mar 19, 2024

There was no issue so far. I will look into your feed.
I am not saying that there is no error in your feed. Just that nobody had problem in the past.

@aledeg
Copy link
Member

aledeg commented Mar 20, 2024

Please paste your feed configuration (screenshot for instance). I cannot add it on my instance with the limited information I have.

@sherlcok314159
Copy link
Author

Fine. You can try this feed with a CSS selector as #PostContent. Thanks for the quick response.

Here. Tks!

@aledeg
Copy link
Member

aledeg commented Mar 20, 2024

It's not really a CSP problem. It's how extensions add scripts. Mathjax scripts load other scripts by using their relative paths. That not how extensions work in FreshRSS.
I need to find a way to fix that.

The file html.js is not loaded. I guess that nobody had a problem with that before. I certainly don't.

It's under investigation.

@aledeg
Copy link
Member

aledeg commented Mar 20, 2024

Here is a working example:
Screen Shot 2024-03-19 at 23 32 51

The extension works most of the time.

@sherlcok314159
Copy link
Author

The problem may lie in the loading order. When I use feeds with full-article selector, it almost fails. For instance, this feed is not working either.

图片

The feed is https://lilianweng.github.io/index.xml with CSS selector as ".post-single".

@aledeg
Copy link
Member

aledeg commented Mar 21, 2024

@Alkarex Is there a way to alter the CSP from an extension? I can make this extension work but I need to tweak the CSP rules. Thank you

@sherlcok314159
Copy link
Author

Thanks for your help! If we fail to alter the CSP by an extension, can the user alter the CSP by rewriting the FreshRSS.php and replacing the one in the container? For instance,

    volumes:
      - ./FreshRSS.php:/var/www/FreshRSS/app/FreshRSS.php

@Alkarex
Copy link
Member

Alkarex commented Mar 21, 2024

can the user alter the CSP

@sherlcok314159 The user way to alter the CSP is at Web server level (e.g. Apache), not at PHP level, which will fail at each FreshRSS update.

@aledeg So far, controllers have access to a method to redefine CSP:

/**
* Set CSP policies.
*
* A default-src directive should always be given.
*
* References:
* - https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
* - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/default-src
*
* @param array<string,string> $policies An array where keys are directives and values are sources.
*/
protected function _csp(array $policies): void {
if (!isset($policies['default-src'])) {
$action = Minz_Request::controllerName() . '#' . Minz_Request::actionName();
Minz_Log::warning(
"Default CSP policy is not declared for action {$action}.",
ADMIN_LOG
);
}
$this->csp_policies = $policies;
}

I have not checked how practical this is for extensions, but that could be extended if needed (I will be with intermittent Internet connectivity the coming two weeks)

@aledeg
Copy link
Member

aledeg commented Mar 21, 2024

Thank you for your answer.
I've seen that method but I can figure out how to use it from an extension. I need to dig a little bit.

@sherlcok314159
Copy link
Author

Thanks for your help again!!!

@sherlcok314159
Copy link
Author

Hi, aledeg, any update? Is there anything I can help with? :-)

@aledeg
Copy link
Member

aledeg commented Mar 30, 2024

Hi, aledeg, any update? Is there anything I can help with? :-)

I did not have time to look into it. But I will.

@Alkarex I think we might need something to interact with the CSP directly from the extension. Probably a hook is a way to go. If you think this is a good idea, I can work on that. If you don't, what do you have in mind? Thank you!

@Alkarex
Copy link
Member

Alkarex commented Mar 30, 2024

@aledeg A hook sounds like a possibility, yes. Another approach could be to add a method to the base extension class, if needed.

@aledeg
Copy link
Member

aledeg commented Mar 30, 2024

@sherlcok314159 there is a fix to use the library from a CDN. You need the edge branch for FreshRSS and the last release of the extension. Let me know if this works for you.

@sherlcok314159
Copy link
Author

Hi! I try the edge branch and the latest extension. It did not work.

services:

  freshrss:
    image: freshrss/freshrss:edge
    # Optional build section if you want to build the image locally:
    # build:
    #   # Pick #latest (stable release) or #edge (rolling release) or a specific release like #1.21.0
    #   context: https://github.com/FreshRSS/FreshRSS.git#edge
    #   dockerfile: Docker/Dockerfile-Alpine
    container_name: freshrss
    hostname: freshrss
    restart: unless-stopped

And the console screenshot is below (edge):

Snipaste_2024-03-31_14-31-19

Here is the screenshot for firefox:

图片

@aledeg
Copy link
Member

aledeg commented Mar 31, 2024

While I do have some errors, the content is properly modified. See capture
Screen Shot 2024-03-31 at 07 03 35

When I am fixing errors, the content disappears. I have no idea why.

@aledeg
Copy link
Member

aledeg commented Mar 31, 2024

Screen Shot 2024-03-31 at 07 26 55
Same entry when unsafe-inline is authorized and there are no more errors. There are a lot of warnings though. All related to styling.

@Frenzie
Copy link
Member

Frenzie commented Mar 31, 2024

The LaTeX JS probably requires inline styles to render the math and the CSP rules disable that. I'm not really sure what disabling inline styles is good for at all tbh, but that aside. But what doesn't work about unsafe-inline exactly?

@aledeg
Copy link
Member

aledeg commented Mar 31, 2024

That's the weird thing. When the rules are not authorized it works (see first capture). But it does not work when the rules are authorized (see second capture). I am lost :)

@sherlcok314159
Copy link
Author

Can we just let the user decide whether to open these CSP rules? If nothing is blocked, things should be fine.

@sherlcok314159
Copy link
Author

Any update please?

@math-GH
Copy link
Contributor

math-GH commented May 7, 2024

Can we just let the user decide whether to open these CSP rules? If nothing is blocked, things should be fine.

see #6437 (comment)

@sherlcok314159
Copy link
Author

For anyone who wants to display equation well in the freshrss, here are two steps for you:

  1. Modify var/www/FreshRSS/app/layout/layout.phtml to replace CSP rules. Note that the below rules can be unsafe. Use it carefully.
<?php
	declare(strict_types=1);
	/** @var FreshRSS_View $this */
	FreshRSS::preLayout();
	header("Content-Security-Policy: default-src 'self' https: data: 'unsafe-inline' 'unsafe-eval' 'unsafe-hashes'"); 
?>
  1. I quit using freshrss plugin to solve this. I borrow code from one Tampermonkey script and modify some places for correct font size and equation reference.
// ==UserScript==
// @name         MathJax For All
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Load MathJax and Polyfills on specified pages
// @author       Apricity
// @match        http*://*/*
// @grant        none
// ==/UserScript==

if (window.MathJax === undefined) {
    var mjscr = document.createElement("script");
    mjscr.type = "text/javascript";
    mjscr.async = true;
    mjscr.src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js";
    mjscr.text = 'MathJax.Hub.Config({extensions:["tex2jax.js"],'+"tex2jax:{inlineMath:[['$','$']]"
        +',skipTags:["script","noscript","pre","code"]},jax:["input/TeX","output/CommonHTML"],CommonHTML: {minScaleAdjust:120},'
        +'TeX:{extensions:["autoload-all.js","noUndefined.js"], equationNumbers: { autoNumber: "AMS" }}'
        +'});MathJax.Hub.Startup.onload();';
    document.getElementsByTagName("head")[0].appendChild(mjscr);

    (new MutationObserver(function(mutationsList, observer) {
        let el = [];
        for (const mutation of mutationsList) {
            const node = mutation.addedNodes.item(0);
            if (node === null || node.nodeType !== 1 || node.className == "MathJax_Preview" || node.id.substring(0,7) == "MathJax" || node.className.substring(0,4) == "mjx-" || node.isContentEditable) break;
            if (node.offsetParent !== null && node.innerText != "") el.push(node);
        }
        if (el.length != 0) MathJax.Hub.Queue(["Typeset", MathJax.Hub,el,{}]);
    })).observe(document,{subtree: true, childList: true});

Then, everything is fine.

图片

@Frenzie
Copy link
Member

Frenzie commented May 7, 2024

Unsafe is overstating it a bit imho. :-) All <script> tags (and much more) are filtered from feeds before database insertion.

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

No branches or pull requests

5 participants