Repository navigation
Least Authority
https://github.com/nextcloud-publish/publish/wiki/How-it-works
The project at scope extends the Nextcloud App "Collectives". Collectives lets Nextcloud users collect and manage shared knowledge in a markdown wiki. Our addition will enable users to easily generate static HTML out of a selectable set of collective (markdown) pages and have the static HTML be served under a partly custom URL.
For that we've developed two external services (symfony) and a library (vanilla PHP) helping to manage the lifecycle of CUD (Create, Update, Delete) operation for a given static site inside a dockerized environment.
The following step-by-step formula describes the process of generating a new static website from collective contents. You may find it useful to consult this flowchart during reading:
This step happens inside Nextcloud Collectives. One or more people create a collective and add / edit / delete markdown pages and subpages. Over time a tree of markdown formated content is being produced.
At some point people having access to a collective decide to publish the current state as a static website. They click the "Publish Website" button from the menu and enter some basic data necessary for the release
- The selection of pages to be rendered and published
- The page's title
- The page's slug (which is the suffix appended to the base URL)
After submission a job is being enqueued inside Nextcloud which is responsible for gathering and wrapping the selected content into a tar.gz archive which is made available for download via a dedicated URL.
In its current implementation the rendering of the markdown is being performed inside an external environment. Also serving the static HTML is being done there. The API service takes the initial building request, validates the input payload and if successful places a job into the job queue (currently rabbitmq used as message broker - will likely be replaced with postgresql). Also a database record is created which is used to keep track of the build status (pending || failed || published) as well as the static site id and some meta data (title, slug, etc.).
An instance of publish-worker picks up the message from the queue and processes the markdown making use of a custom static site generation library based on league/commonmark. During this phase a lot is happening
- preprocessing: the worker downloads the archive and unpacks it with
tar, then rejects it if it has no.mdfile.ssg-libraryscans the folders into a page tree (ignoring symlinks and dotfiles), copies only allow-listed attachment types, cleans the Markdown (BOM, NUL bytes, line endings, invalid UTF-8) and converts Nextcloud's callout and multiline-table syntax to HTML. - rendering: league/commonmark turns each page into HTML with server-side code highlighting, and HTMLPurifier then strips anything unsafe (scripts, external or root-relative embeds) since raw HTML is allowed in the Markdown. JavaScript from authors never reaches the output:
<script>tags, event-handler attributes (onclick=…) andjavascript:links are removed by HTMLPurifier, and attachments that run as script in a browser (.js,.html,.svg) are not copied. The only JS on a published page is the library's owntheme.jsandnav.js(light/dark toggle, mobile menu). The library wraps each page in a theme layout (wikiortopnav) with navigation and writes it as<slug>/index.html.
Everything is placed into a single folder that is bind-mounted from the host system into the worker and served by nginx at <publish base URL>/<slug>/. All status notification returned to Nextcloud collectives is being done by publish-worker service which has access to the internet and is also ablet to perform database operations.
The project is being resembled by several repos containing code and documentation for each of the services / components described above
- Nextcloud collectives: https://github.com/nextcloud/collectives (current feature branch: https://github.com/nextcloud/collectives/pull/2769)
- Publish API service: https://github.com/nextcloud-publish/publish
- Publish worker service: https://github.com/nextcloud-publish/publish-worker
- SSG library: https://github.com/nextcloud-publish/ssg-commonmark
We process user input in various places. Where? and how do we mitigate
- What options do we have in ether direction
We need to discuss the security implications that are opening up under different mechanisms to serve the rendered HTML.
This is the current implementation
As described above we serve the static HTML via a webserver from the same host the building is happening under the same subdomain as the nextcloud instance is to be found. So if building requests go to https://publish.codecodecode.org the pages are being served under https://publish.codecodecode.org/sites/<slug> whereas the nextcloud instance would be found under https://cloud.codecodecode.org. We are aware that this way imposes minor risks since *.codecodecode.org is the same site but we found that we have some lines of defending XSS
- Nextcloud itself provides some security measurements like setting cookies with Domain being strictly scoped to the subdomain under which the instance is running (no wildcard here)
- We can assume that javascript entering the rendered HTML is an edge-case since an attacker would need to bypass
HTMLPurifier - nginx configuration would prevent loading javascript
Which attack paths are left here?
And probably the bigger problem: We were planning to develop the feature so that pages will be build within the nextcloud runtime environment and also be served there - hence under the (sub)domain of the nextcloud instance. Which attack paths do open up here and which defense measures do we have (if any).
What is common/ good practise