Harden core packages and add interactive CLI config builder (rud config/validate/test)

This commit is contained in:
tyuXX 2026-11-19 13:05:26 +00:00
parent d37503c863
commit 99e24be6c9
29 changed files with 6763 additions and 165 deletions

View file

@ -126,10 +126,10 @@ type renderedElement struct {
ShowLiveIndicator bool
// Banner
BannerMessage string
BannerMessagesJSON template.JS
ShowOverallUptime bool
ShowCounts bool
BannerMessage string
BannerMessagesJSON string
ShowOverallUptime bool
ShowCounts bool
// Summary cards
Cards []summaryCardView
@ -161,7 +161,7 @@ type monitorDisplayFlags struct {
ShowDescription bool
ShowMessage bool
ShowLastChecked bool
LabelsJSON template.JS
LabelsJSON string
}
type monitorView struct {
@ -214,7 +214,7 @@ func (s *Server) renderHTML(snap store.Snapshot) ([]byte, error) {
elements = append(elements, renderedElement{
Type: "status_banner",
BannerMessage: msgs[snap.OverallState],
BannerMessagesJSON: template.JS(msgBytes),
BannerMessagesJSON: string(msgBytes),
ShowOverallUptime: el.ShowOverallUptime,
ShowCounts: el.ShowCounts,
})
@ -343,7 +343,7 @@ func resolveDisplayFlags(d config.MonitorDisplayConfig, defaultTicks int) monito
ShowDescription: boolVal(d.ShowDescription, true),
ShowMessage: boolVal(d.ShowMessage, false),
ShowLastChecked: boolVal(d.ShowLastChecked, false),
LabelsJSON: template.JS(labelsBytes),
LabelsJSON: string(labelsBytes),
}
}
@ -415,8 +415,23 @@ func selectGroupMonitors(ids []string, snap store.Snapshot, flags monitorDisplay
return views
}
var cssSanitizer = strings.NewReplacer(
"{", "",
"}", "",
";", "",
"<", "",
">", "",
"\r", "",
"\n", "",
)
func safeCSSValue(v string) template.CSS {
return template.CSS(cssSanitizer.Replace(strings.TrimSpace(v)))
}
var pageTemplate = template.Must(template.New("status").Funcs(template.FuncMap{
"upper": strings.ToUpper,
"upper": strings.ToUpper,
"safeCSS": safeCSSValue,
"fmtTime": func(t time.Time) string {
if t.IsZero() {
return "-"
@ -431,19 +446,19 @@ var pageTemplate = template.Must(template.New("status").Funcs(template.FuncMap{
<title>{{.Title}}</title>
<style>
:root {
--bg: {{.Theme.BgColor}};
--card: {{.Theme.CardBgColor}};
--text: {{.Theme.TextColor}};
--muted: {{.Theme.MutedTextColor}};
--border: {{.Theme.BorderColor}};
--accent: {{.Theme.AccentColor}};
--up: {{.Theme.UpColor}};
--degraded: {{.Theme.DegradedColor}};
--down: {{.Theme.DownColor}};
--pending: {{.Theme.PendingColor}};
--font: {{.Theme.FontFamily}};
--maxw: {{.Theme.MaxWidth}};
--radius: {{.Theme.BorderRadius}};
--bg: {{safeCSS .Theme.BgColor}};
--card: {{safeCSS .Theme.CardBgColor}};
--text: {{safeCSS .Theme.TextColor}};
--muted: {{safeCSS .Theme.MutedTextColor}};
--border: {{safeCSS .Theme.BorderColor}};
--accent: {{safeCSS .Theme.AccentColor}};
--up: {{safeCSS .Theme.UpColor}};
--degraded: {{safeCSS .Theme.DegradedColor}};
--down: {{safeCSS .Theme.DownColor}};
--pending: {{safeCSS .Theme.PendingColor}};
--font: {{safeCSS .Theme.FontFamily}};
--maxw: {{safeCSS .Theme.MaxWidth}};
--radius: {{safeCSS .Theme.BorderRadius}};
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
@ -677,7 +692,7 @@ a:hover { color: var(--accent); }
<div class="mon-sub">
<span>
{{- if and $disp.ShowDescription .Monitor.Description}}{{.Monitor.Description}}{{end}}
{{- if and $disp.ShowMessage .Monitor.LastMessage}} <span data-field="message">({{.Monitor.LastMessage}})</span>{{end}}
{{- if $disp.ShowMessage}} <span data-field="message">{{if .Monitor.LastMessage}}({{.Monitor.LastMessage}}){{end}}</span>{{end}}
</span>
{{- if $disp.ShowLastChecked}}<span data-field="last-checked">{{.LastChecked}}</span>{{end}}
</div>
@ -767,17 +782,26 @@ a:hover { color: var(--accent); }
if (bar && Array.isArray(m.history)) {
var hist = m.history.slice(-ticksCount);
var pad = ticksCount - hist.length;
var html = '';
var frag = document.createDocumentFragment();
for (var i = 0; i < pad; i++) {
html += '<div class="tick" data-state="pending" title="No data"></div>';
var pt = document.createElement('div');
pt.className = 'tick';
pt.setAttribute('data-state', 'pending');
pt.title = 'No data';
frag.appendChild(pt);
}
for (var j = 0; j < hist.length; j++) {
var h = hist[j];
var t = h.timestamp ? new Date(h.timestamp).toISOString().slice(11, 19) + ' UTC' : '';
var tip = t + ' | ' + (h.state || '').toUpperCase() + ' | ' + (h.latency_ms || 0).toFixed(1) + 'ms' + (h.message ? ' | ' + h.message : '');
html += '<div class="tick" data-state="' + h.state + '" title="' + tip.replace(/"/g, '&quot;') + '"></div>';
var tk = document.createElement('div');
tk.className = 'tick';
tk.setAttribute('data-state', h.state || 'pending');
tk.title = tip;
frag.appendChild(tk);
}
bar.innerHTML = html;
bar.textContent = '';
bar.appendChild(frag);
}
});
});

View file

@ -68,3 +68,54 @@ func TestSingleEndpointHTMLAndJSON(t *testing.T) {
t.Fatalf("expected JSON status 'down', got %q", snap.OverallState)
}
}
func TestServerSecurityAndRenderingFixes(t *testing.T) {
cfg := config.DefaultConfig()
cfg.Page.Theme.FontFamily = `"JetBrains Mono", monospace`
cfg.Page.Theme.BgColor = `#111111; } </style><script>alert(1)</script>`
cfg.Page.Elements = []config.PageElement{
{
Type: "status_banner",
Messages: map[string]string{
"down": "We're experiencing issues",
},
},
{
Type: "monitor_group",
Monitors: []string{"rud-http"},
Display: &config.MonitorDisplayConfig{
ShowMessage: config.BoolPtr(true),
},
},
}
st := store.New(&cfg) // rud-http is pending with empty LastMessage
srv := New(&cfg, st)
req := httptest.NewRequest(http.MethodGet, "/", nil)
w := httptest.NewRecorder()
srv.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("expected 200 OK, got %d", w.Code)
}
body := w.Body.String()
if strings.Contains(body, "ZgotmplZ") {
t.Fatalf("unexpected ZgotmplZ in rendered HTML CSS")
}
if !strings.Contains(body, `--font: "JetBrains Mono", monospace;`) {
t.Fatalf("expected quoted FontFamily to render intact, got:\n%s", body)
}
if strings.Contains(body, `<script>alert(1)</script>`) {
t.Fatalf("CSS value breakout was not stripped")
}
if !strings.Contains(body, `We&#39;re experiencing issues`) {
t.Fatalf("expected single quote in data-messages to be HTML-attribute escaped")
}
if !strings.Contains(body, `data-field="message"`) {
t.Fatalf("expected [data-field=\"message\"] placeholder even when monitor LastMessage is empty")
}
if strings.Contains(body, `bar.innerHTML`) {
t.Fatalf("expected safe DOM construction instead of bar.innerHTML")
}
}