Giter Club home page Giter Club logo

Comments (8)

bernesto avatar bernesto commented on June 3, 2024 2

I think the pre-parser option is a really good idea. It sticks to the 'plug-in' per feature concept.

How about updating fromElement to accept a string element ID or boolean. If bool == true, works as it does now, parsing the container HTML. If a string ID, it uses the contents of the element and the container becomes the editor as it does now. Then we could update the docs in favor of a HTML script template for development. This prevents any braking changes, and promotes proper usage.

const editor = grapesjs.init({
  container: '#gjs',
  fromElement: '#mySource',
  height: '100%',
  storageManager: { type: 0 },
  plugins: ['gjs-blocks-basic']
});

from grapesjs.

artf avatar artf commented on June 3, 2024 2

Totally agree with @bernesto indeed no matter how hard we try to make it safe, it will never be enough and I don't want to give the impression that the library is "so safe" to justify a missing server-side validation.
The current options (eg. allowUnsafeAttr, allowUnsafeAttrValue) avoid only the basic common stuff.

from grapesjs.

bernesto avatar bernesto commented on June 3, 2024 1

This is unavoidable when using fromElement to load from an active DOM element. The element of the page loads and executes synchronously. GrapesJS would never have a chance to process and disarm the XSS html.

This would need to be addressed by preventing the malicious code from ever loading. This should be handled at the server on save. But, if on the client processing is needed, by wrapping the code in script tags, and then sanitizing the code prior to loading it in to the editor like this:

https://jsfiddle.net/bernesto/5gcxa0jm/1/

I do see that when loading via the components property at initialization or dynamically post from a remote source, the same XSS vulnerability exists, and this may be something we want to prevent.

Personally, I believe that the server should be processing all saves and sanitizing. A malicious actor could always emulate a client request and save an malicious XSS to your server regardless of the editor used.

@artf what do you think?

In your opinion, should an XSS sanitizer be built into the editor? And if so, that begs the question; how do you feel about fromElement anyways? Should it be reimplemented as something similar to my example, where content is provided using script tags instead? E.g. maybe providing a source (script template) and destination (editor) element IDs instead?

Not only would this allow us to sanitize for XSS vulnerabilities, but it would also eliminate the browser processing and painting those initial DOM elements twice (once on load, second to the new frame). This may actually speed up the editor load time as a happy benefit.

from grapesjs.

artf avatar artf commented on June 3, 2024 1

Yeah indeed with fromElement option this is inevitable, also a reason why I marked it as deprecated besides seeing people using it the wrong way. That option should only be used for debugging/prototyping and we need to update the docs 🥲

I wouldn't push an entire sanitizer here (might be a pre-parser option though) but at least I can add a new option to prevent "unsafe" attribute values.

from grapesjs.

bernesto avatar bernesto commented on June 3, 2024 1

This will depend on how @artf decides he'd like that architected. I can see how it would make the sense to be inserted in the pipeline where projectData and components sources coalesce prior to being written to the frame DOM and/or when reading HTML/CSS out.

That said, you mentioned 'best practices'. BP for security would be parsing and filtering the data at the server prior to storage, where it is beyond the manipulation of nefarious parties. e.g. A user may be well meaning, but a browser plugin may not. For instance it may take advantage of client RTEs with contenteditables to inject hidden code to mine bitcoin in a web worker for instance... (not a suggestion by any means lol)

Client side code filtering should be exclusively used for user experience. i.e. Bubbling up warnings from user interactions prior to round tripping to the server to create a responsive and intuitive experience. I speculate that is part of why Artur is more inclined to not include this filtering in the client, favoring pre/post processor of some sort so you can float your own if you need. I personally really like his modular approach and flexibility, as this allows choice in implementation.

from grapesjs.

artf avatar artf commented on June 3, 2024 1

@davidgabrichidze I'll try to push the next release in a few days.

from grapesjs.

davidgabrichidze avatar davidgabrichidze commented on June 3, 2024

Thanks for the insightful discussion. I want to clarify that while I used fromElement in my example, the core issue remains the same with my use of editor.loadProjectData and editor.getProjectData.

Here's how I currently handle HTML compilation:

export class HtmlTemplateEditorComponent
  implements OnInit, ControlValueAccessor
{
....
  private compileHtml(html: string, styles: string, script: string) {
    const regexForIndex = /(<\/body>)/g;
    const everyLastBodyTagIndex = html.search(regexForIndex);
    const result =
      `<head><style>${styles}</style></head>` +
      html.slice(0, everyLastBodyTagIndex) +
      `<script>${script}</script>` +
      html.slice(everyLastBodyTagIndex);

    return result;
  }

  private buildContentModel(): HtmlTemplateEditorContentModel {
    const html = this.editor.getHtml();
    const styles = this.editor.getCss() as string;
    const script = this.editor.getJs();

    return {
      content: this.compileHtml(html, styles, script),
      contentObject: { ...this.editor.getProjectData(), assets: null },
    };
  }
....
}

My primary focus is on best practices for preventing XSS within this context. Will the upcoming pre-parser feature in GrapesJS adequately address these concerns?

Looking forward to your suggestions.

from grapesjs.

davidgabrichidze avatar davidgabrichidze commented on June 3, 2024

@artf @bernesto thanks for you support and super fast response.

@artf what is ETA of the next Release?

from grapesjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.