Skip to content

Database isn't escaped when dumped in the published HTML file #1100

Closed

Description

@karlicoss

Describe the bug

When publishing, doesn't look like database is escaped when put into the script tag

https://github.com/logseq/logseq/blob/master/src/main/frontend/publishing/html.cljs#L53

This might result in broken HTML, in my case because of a closing </script> tag in the note body.

To Reproduce
Something like this is enough to break it

* <script> BOOM </script>

Steps to reproduce the behavior:

  1. Add the note above, reindex
  2. Publish HTML (make sure to make the page public)
  3. Open HTML in browser (doesn't matter if just as HTML file or via actual local webserver)
  4. You'll see Uncaught SyntaxError: "" literal not terminated before end of script in the logs and generally it won't load properly

Expected behavior
Not sure how to fix this properly.. I messed with JSON stringifying a bit, but it didn't work for some reason. Would be happy for pointers what's the best way to escape and fix it.

Activity

  1. tiensonqin commented on Jan 13, 2021

    @tiensonqin
    Contributor

    @karlicoss Yes, we need to espape the data for window.logseq_db, something like this:

    [:script (str "window.logseq_db=" (escape-html transit-db))]

    https://github.com/logseq/logseq/blob/master/src/main/frontend/publishing/html.cljs#L53
    And, when loading the publishing the exported HTML, we need to unescape the data, something might like this:

    (let [data (js/JSON.stringify data)
           data (unescape-html data)]
           ...
           )

    The difficult part to me is how to escape the string properly, something like this might work for the <script> tag:

    (defn escape-html
      [text]
      (-> text
          (string/replace "<script>"  "&lt;script&gt;")
          (string/replace "</script>"  "&lt;/script&gt;")))
    
    (defn unescape-html
      [text]
      (-> text
          (string/replace "&lt;/script&gt;" "</script>")
          (string/replace "&lt;script&gt;" "<script>")))

    But what about other cross-site scripting vulnerabilities? I guess we don't need to worry too much for self-hosted digital garden 😁

  2. karlicoss commented on Jan 14, 2021

    @karlicoss
    ContributorAuthor

    Yeah it could be a potential security risk. IMO best to use some existing node/clojure library to handle this, even if you handle script tag, other tags can break it as well, and escaping is not something fun to maintain

  3. tiensonqin commented on Jan 15, 2021

    @tiensonqin
    Contributor

    @karlicoss I copied some code from hiccup which should resolve most issues related to escaping the publishing HTML, I just tested it and it works for several of my graphs, let me know whether it works for you.
    PR: #1134

  4. tiensonqin commented on Jan 18, 2021

    @tiensonqin
    Contributor

    Closing this now.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions