* Serve the build-independent web assets from the backend `privacy.html`, `markdown-help.html` and the `400x400.jpeg` it embeds carry no template variable, link no script or stylesheet, and are imported by nothing in the widget. They now live in `backend/app/webassets/assets`, embedded there, and are served under `/web` alongside the frontend build. `/web` reads the frontend build first and falls back to them, which is what lets an operator replace one by dropping a file into `--web-root`. That is what `privacy.html` needs: it describes remark42.com, while the authorization guide tells operators to hand its URL to Google and Facebook as their own application's privacy policy. Only a missing file falls through. An unreadable file in the web root keeps reporting as unreadable rather than being silently replaced by the embedded copy, and a name the filesystem rejects reports as missing rather than as a server error, both matching what `http.Dir` did. The dev server serves the same directory, so the Markdown help link in the comment form resolves on the dev port as well as in production. The two pages are served as they are written. `markdown-help.html` was minified before, and its formatted inline stylesheet is most of its 8.5 kB; that is 2.4 kB more over the wire, behind the hour-long cache header the file server already sets. Drops `copy-webpack-plugin`, which had no other pattern, and the stylelint entries that only ever matched these files. * Make pnpm dev:app start again The dev server has been failing to start on two counts, so the flow the contributing guide documents does not run at all. `webpack-cli` 4 drives `webpack-dev-server` 5 through the argument order of an older major, handing it the compiler where it expects the options object. It rejects that against its schema and exits, complaining about an unknown `_assetEmittingPreviousFiles` property, which is a field of the compiler. `webpack-cli` 7 is the release that declares `webpack-dev-server` 5 as a peer. Past that, `http-proxy-middleware` resolves to 4.1.1, which no longer accepts the two-argument call `webpack-dev-server` makes, so the `/api` and `/auth` proxies throw on startup. It is pulled in by the security override for CVE-2025-32996, the only override in the file with no upper bound: `>=2.0.10` matches every later major. Bounding it to the 2.x line keeps the fix and the API `webpack-dev-server` calls. With both in place `pnpm dev:app` serves the widget and the pages under `/web` on port 9000.
148 lines
4.8 KiB
Go
148 lines
4.8 KiB
Go
package api
|
|
|
|
import (
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"github.com/umputun/remark42/backend/app/webassets"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestWebFiles_Open(t *testing.T) {
|
|
frontend := fstest.MapFS{
|
|
"both.html": {Data: []byte("from the frontend build")},
|
|
"only-frontend.html": {Data: []byte("frontend only")},
|
|
}
|
|
embedded := fstest.MapFS{
|
|
"both.html": {Data: []byte("from the embedded assets")},
|
|
"only-embedded.html": {Data: []byte("embedded only")},
|
|
}
|
|
w := webFiles{frontend: frontend, embedded: embedded}
|
|
|
|
tbl := []struct {
|
|
name string
|
|
lookup string
|
|
want string
|
|
wantErr error
|
|
}{
|
|
{name: "present in both is served from the frontend build", lookup: "both.html", want: "from the frontend build"},
|
|
{name: "frontend only", lookup: "only-frontend.html", want: "frontend only"},
|
|
{name: "embedded only", lookup: "only-embedded.html", want: "embedded only"},
|
|
{name: "missing in both", lookup: "neither.html", wantErr: fs.ErrNotExist},
|
|
}
|
|
|
|
for _, tt := range tbl {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
f, err := w.Open(tt.lookup)
|
|
if tt.wantErr != nil {
|
|
require.Error(t, err)
|
|
assert.ErrorIs(t, err, tt.wantErr)
|
|
return
|
|
}
|
|
require.NoError(t, err)
|
|
defer f.Close()
|
|
b, err := io.ReadAll(f)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, tt.want, string(b))
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestEmptyFS_ServesNothing pins the stand-in used when the frontend source cannot be opened:
|
|
// every name must report as missing rather than panicking, since it backs a nil-free fallback.
|
|
func TestEmptyFS_ServesNothing(t *testing.T) {
|
|
for _, name := range []string{".", "index.html", "web/index.html"} {
|
|
t.Run(name, func(t *testing.T) {
|
|
f, err := emptyFS{}.Open(name)
|
|
require.Error(t, err)
|
|
assert.ErrorIs(t, err, fs.ErrNotExist)
|
|
assert.Nil(t, f)
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestWebFiles_EmptyFrontendFallsThrough covers the shape routes() builds when fs.Sub refuses:
|
|
// the embedded assets must still answer even though the frontend source serves nothing.
|
|
func TestWebFiles_EmptyFrontendFallsThrough(t *testing.T) {
|
|
w := webFiles{frontend: emptyFS{}, embedded: webassets.FS}
|
|
|
|
want, err := fs.ReadFile(webassets.FS, "privacy.html")
|
|
require.NoError(t, err)
|
|
|
|
f, err := w.Open("privacy.html")
|
|
require.NoError(t, err)
|
|
defer f.Close()
|
|
got, err := io.ReadAll(f)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, string(want), string(got))
|
|
}
|
|
|
|
// TestWebFiles_OpenRootIsNotListed keeps the embedded assets from being browsable: they answer
|
|
// for their own names only, so a web root that has gone missing reports as missing.
|
|
func TestWebFiles_OpenRootIsNotListed(t *testing.T) {
|
|
w := webFiles{frontend: os.DirFS(filepath.Join(t.TempDir(), "absent")), embedded: webassets.FS}
|
|
|
|
f, err := w.Open(".")
|
|
require.Error(t, err, "the embedded assets must not answer for the directory itself")
|
|
assert.ErrorIs(t, err, fs.ErrNotExist)
|
|
if err == nil {
|
|
_ = f.Close()
|
|
}
|
|
|
|
// the assets themselves still serve
|
|
f, err = w.Open("privacy.html")
|
|
require.NoError(t, err)
|
|
require.NoError(t, f.Close())
|
|
}
|
|
|
|
// TestWebFiles_OpenInvalidName pins that a name fs rejects reports as missing rather than invalid.
|
|
// os.DirFS returns fs.ErrInvalid for these, which http.FileServer renders as 500, so the check has
|
|
// to happen before the lookup. A memory filesystem cannot show this: it reports missing either way.
|
|
func TestWebFiles_OpenInvalidName(t *testing.T) {
|
|
dir := t.TempDir()
|
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "privacy.html"), []byte("frontend"), 0o600))
|
|
w := webFiles{frontend: os.DirFS(dir), embedded: webassets.FS}
|
|
|
|
for _, name := range []string{"../escape.html", "/etc/passwd", "a\x00b.html", "./privacy.html"} {
|
|
t.Run(name, func(t *testing.T) {
|
|
f, err := w.Open(name)
|
|
require.Error(t, err)
|
|
assert.ErrorIs(t, err, fs.ErrNotExist)
|
|
assert.NotErrorIs(t, err, fs.ErrInvalid, "an invalid name must not surface as 500")
|
|
if err == nil {
|
|
_ = f.Close()
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestWebFiles_OpenUnreadableFrontendFile pins the rule that only a missing file falls through:
|
|
// a frontend file that cannot be read must report that, not be masked by the embedded copy.
|
|
func TestWebFiles_OpenUnreadableFrontendFile(t *testing.T) {
|
|
if os.Geteuid() == 0 {
|
|
t.Skip("root ignores file permissions")
|
|
}
|
|
|
|
dir := t.TempDir()
|
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "privacy.html"), []byte("operator's own"), 0o000))
|
|
|
|
w := webFiles{
|
|
frontend: os.DirFS(dir),
|
|
embedded: fstest.MapFS{"privacy.html": {Data: []byte("built in")}},
|
|
}
|
|
|
|
f, err := w.Open("privacy.html")
|
|
require.Error(t, err, "an unreadable frontend file must not be replaced by the embedded copy")
|
|
assert.NotErrorIs(t, err, fs.ErrNotExist, "the error must stay a permission error so it does not render as 404")
|
|
assert.ErrorIs(t, err, fs.ErrPermission)
|
|
if err == nil {
|
|
_ = f.Close()
|
|
}
|
|
}
|