csaf-crud 1.3.7

CSAF 2.0 / 2.1 advisory CRUD server with HATEOAS JSON API and HTML UI (TLS 1.3, HTTP/1.1 + HTTP/2 + HTTP/3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 Pierre Gronau, ndaal in Cologne

//! Static information page route handlers.
//!
//! Provides About, License, System Info, Privacy, and Security pages.

use http::{Response, StatusCode};

use crate::app_state::AppState;
use crate::router::{Body, html_response};
use crate::routes::layout::{Nav, html_escape, wrap_page};

/// Sub-nav shown at the top of every Info-section page (About/License/
/// System/Privacy/Security plus the four document pages).
const INFO_SUB_NAV: &str = r#"<div class="card" style="padding:0.75rem 1rem;">
  <a href="/info/about">About</a> &middot;
  <a href="/info/license">License</a> &middot;
  <a href="/info/system">System Info</a> &middot;
  <a href="/info/privacy">Privacy</a> &middot;
  <a href="/info/security">Security</a> &middot;
  <a href="/changelog">Changelog</a> &middot;
  <a href="/readme">README</a> &middot;
  <a href="/administrator">Administrator Guide</a> &middot;
  <a href="/user">User Guide</a>
</div>"#;

/// Render an info page with the shared layout and a sub-nav for the Info
/// section.
fn render_info_page(title: &str, theme: &str, body: &str) -> Response<Body> {
    let content = format!("{INFO_SUB_NAV}{body}");
    html_response(StatusCode::OK, wrap_page(title, theme, Nav::Info, &content))
}

/// `GET /info/about` -- About page.
pub async fn about(
    state: AppState,
    _parts: http::request::Parts,
    _params: Vec<(String, String)>,
) -> Response<Body> {
    let body = r#"<h1>About CSAF</h1>
<div class="card">
  <p><strong>CSAF</strong> is a web-based management tool for creating, reading,
  updating, and deleting Common Security Advisory Framework (CSAF) documents
  conforming to OASIS CSAF versions 2.0 and 2.1.</p>

  <h2>Features</h2>
  <ul>
    <li>Full CRUD operations for CSAF security advisories, VEX, and informational advisories</li>
    <li>Built-in validation against CSAF 2.0/2.1 schema rules</li>
    <li>CVSS v3.1 and v4.0 scoring display</li>
    <li>SHA-256 and SHA3-512 sidecar hash file generation</li>
    <li>Bulk import/export with filesystem directory scanning</li>
    <li>Audit trail for all document operations</li>
    <li>HATEOAS-compliant REST API</li>
    <li>Embedded storage (redb + SQLite) — no external database required</li>
    <li>TLS 1.3 with automatic certificate generation</li>
  </ul>

  <h2>Standards</h2>
  <ul>
    <li><a href="https://docs.oasis-open.org/csaf/csaf/v2.0/csaf-v2.0.html"
           target="_blank" rel="noopener">OASIS CSAF 2.0</a></li>
    <li><a href="https://docs.oasis-open.org/csaf/csaf/v2.1/csaf-v2.1.html"
           target="_blank" rel="noopener">OASIS CSAF 2.1</a></li>
  </ul>

  <h2>Contact</h2>
  <p>Developed by <strong>ndaal Gesellschaft für Sicherheit in der
  Informationstechnik mbH &amp; Co KG</strong>, Cologne, Germany.</p>
</div>"#;

    render_info_page("About", &state.settings().theme, body)
}

/// `GET /info/license` -- License page.
pub async fn license(
    state: AppState,
    _parts: http::request::Parts,
    _params: Vec<(String, String)>,
) -> Response<Body> {
    let body = r#"<h1>License</h1>
<div class="card">
  <p>CSAF is licensed under the <strong>Apache License, Version 2.0</strong>.</p>
  <p>SPDX-License-Identifier: <code>Apache-2.0</code></p>
  <p>Copyright (c) 2026 Pierre Gronau, ndaal in Cologne.</p>

  <h2>Apache License 2.0 Summary</h2>
  <ul>
    <li>You may use, modify, and distribute this software freely.</li>
    <li>You must include the copyright notice and license in any distribution.</li>
    <li>The software is provided "AS IS", without warranties.</li>
    <li>Contributors grant a patent license for their contributions.</li>
  </ul>
  <p>Full license text:
    <a href="https://www.apache.org/licenses/LICENSE-2.0"
       target="_blank" rel="noopener">https://www.apache.org/licenses/LICENSE-2.0</a>
  </p>
</div>"#;

    render_info_page("License", &state.settings().theme, body)
}

/// `GET /info/system` -- System information page.
pub async fn system_info(
    state: AppState,
    _parts: http::request::Parts,
    _params: Vec<(String, String)>,
) -> Response<Body> {
    let mut sys = sysinfo::System::new_all();
    sys.refresh_all();

    let os_name = sysinfo::System::name().unwrap_or_else(|| "Unknown".to_owned());
    let os_version = sysinfo::System::os_version().unwrap_or_else(|| "Unknown".to_owned());
    let kernel_version = sysinfo::System::kernel_version().unwrap_or_else(|| "Unknown".to_owned());
    let hostname = sysinfo::System::host_name().unwrap_or_else(|| "Unknown".to_owned());

    let total_memory_mb = sys.total_memory() / 1_048_576;
    let used_memory_mb = sys.used_memory() / 1_048_576;
    let cpu_count = sys.cpus().len();

    let doc_count = state.csaf_storage().count_documents().unwrap_or(0);
    let storage_ok = state.csaf_storage().check_storage_up().unwrap_or(false);

    let config = state.config();
    let settings = state.settings();

    let body = format!(
        r#"<h1>System Information</h1>
<div class="card">
  <h2>Host</h2>
  <table>
    <tr><th>Hostname</th><td>{hostname}</td></tr>
    <tr><th>OS</th><td>{os_name} {os_version}</td></tr>
    <tr><th>Kernel</th><td>{kernel_version}</td></tr>
    <tr><th>CPUs</th><td>{cpu_count}</td></tr>
    <tr><th>Memory</th><td>{used_memory_mb} MB / {total_memory_mb} MB</td></tr>
  </table>
</div>
<div class="card">
  <h2>Application</h2>
  <table>
    <tr><th>Version</th><td>{app_version}</td></tr>
    <tr><th>Rust Version</th><td>{rust_version}</td></tr>
    <tr><th>Listen Address</th><td>{listen}</td></tr>
    <tr><th>Data Directory</th><td>{data_dir}</td></tr>
    <tr><th>Storage Status</th><td>{storage_status}</td></tr>
    <tr><th>Documents Stored</th><td>{doc_count}</td></tr>
    <tr><th>CSAF Mode</th><td>{csaf_mode}</td></tr>
  </table>
</div>"#,
        hostname = html_escape(&hostname),
        os_name = html_escape(&os_name),
        os_version = html_escape(&os_version),
        kernel_version = html_escape(&kernel_version),
        app_version = env!("CARGO_PKG_VERSION"),
        rust_version = html_escape(env!("CARGO_PKG_RUST_VERSION", "unknown")),
        listen = html_escape(&config.listen_address()),
        data_dir = html_escape(&config.data_dir.display().to_string()),
        storage_status = if storage_ok { "Healthy" } else { "Degraded" },
        csaf_mode = html_escape(&settings.csaf_mode),
    );

    render_info_page("System Info", &settings.theme, &body)
}

/// `GET /info/privacy` -- Privacy page.
pub async fn privacy(
    state: AppState,
    _parts: http::request::Parts,
    _params: Vec<(String, String)>,
) -> Response<Body> {
    let body = r#"<h1>Privacy</h1>
<div class="card">
  <h2>Data Processing</h2>
  <p>CSAF is designed as a self-hosted application. All data is stored
  locally on the server where the application is deployed.</p>

  <h2>Data Collected</h2>
  <ul>
    <li><strong>CSAF documents</strong> — stored in the embedded redb database</li>
    <li><strong>User accounts</strong> — login credentials (hashed) stored in SQLite</li>
    <li><strong>Audit logs</strong> — timestamps and actions for document operations</li>
    <li><strong>Session data</strong> — temporary session tokens for authentication</li>
  </ul>

  <h2>No External Communication</h2>
  <p>The application does not send data to external services, analytics
  platforms, or third-party APIs. All processing occurs locally.</p>

  <h2>Data Retention</h2>
  <p>Data is retained until explicitly deleted by the user or administrator.
  Audit logs are stored indefinitely for compliance purposes.</p>

  <h2>GDPR Compliance</h2>
  <p>As a self-hosted tool, GDPR compliance responsibility lies with the
  deploying organisation. The application supports data export and deletion
  to facilitate compliance.</p>
</div>"#;

    render_info_page("Privacy", &state.settings().theme, body)
}

/// `GET /info/security` -- Security page.
pub async fn security(
    state: AppState,
    _parts: http::request::Parts,
    _params: Vec<(String, String)>,
) -> Response<Body> {
    let body = r#"<h1>Security</h1>
<div class="card">
  <h2>Transport Security</h2>
  <ul>
    <li>TLS 1.3 enforced via Rustls (no OpenSSL dependency)</li>
    <li>RSA ciphers excluded; only elliptic-curve cryptography (ECC) used</li>
    <li>Automatic self-signed certificate generation (45-day validity)</li>
    <li>HTTP/1.1 + HTTP/2 auto-negotiated via ALPN over TCP</li>
    <li>HTTP/3 served over QUIC/UDP</li>
  </ul>

  <h2>Authentication</h2>
  <ul>
    <li>Password hashing using Argon2id (RFC 9106)</li>
    <li>API key authentication for programmatic access</li>
    <li>Session-based authentication for the web UI</li>
  </ul>

  <h2>Data Integrity</h2>
  <ul>
    <li>SHA-256 and SHA3-512 sidecar hash files for exported documents</li>
    <li>CSAF document validation on every create, update, import, and export</li>
    <li>Audit trail for all document lifecycle operations</li>
  </ul>

  <h2>Input Validation</h2>
  <ul>
    <li>All CSAF documents validated against structural and semantic rules</li>
    <li>CVSS score range validation (0.0 to 10.0)</li>
    <li>Product ID cross-reference validation</li>
    <li>HTML output is escaped to prevent XSS</li>
    <li>RFC 9457 Problem Details for all API errors</li>
  </ul>

  <h2>Reporting Vulnerabilities</h2>
  <p>If you discover a security vulnerability in CSAF, please report it
  responsibly by contacting the development team at ndaal in Cologne.</p>
</div>"#;

    render_info_page("Security", &state.settings().theme, body)
}

// These embed from crates/csaf-crud/embedded_docs/, a synced local copy of
// the workspace-root files, not the originals directly: `cargo package`/
// `cargo publish` never bundles files outside the crate directory, so a
// source-file-relative include_str! into ../../CHANGELOG.md etc. compiles
// fine in a normal workspace build but fails package verification (and
// would 404 for anyone building the crate standalone from crates.io). The
// `on_disk()` helper in `doc_page_tests` below still reads the canonical
// workspace-root files directly, so any drift between the two fails CI.

/// The repository's `CHANGELOG.md`, embedded at compile time from the
/// crate-local synced copy (see comment above).
const CHANGELOG_MD: &str = include_str!("../../embedded_docs/CHANGELOG.md");
/// The repository's `README.md`, embedded at compile time from the
/// crate-local synced copy.
const README_MD: &str = include_str!("../../embedded_docs/README.md");
/// The repository's `documentation/Administrator_Guide.md`, embedded at
/// compile time from the crate-local synced copy.
const ADMINISTRATOR_GUIDE_MD: &str = include_str!("../../embedded_docs/Administrator_Guide.md");
/// The repository's `documentation/User_Guide.md`, embedded at compile
/// time from the crate-local synced copy.
const USER_GUIDE_MD: &str = include_str!("../../embedded_docs/User_Guide.md");

/// Build a document page's full HTML: the raw Markdown source,
/// HTML-escaped and shown as preformatted text (no Markdown-to-HTML
/// rendering — matches the sibling `grundschutz-oscal-viewer` project's
/// doc-page convention). Pure function, synchronously testable without a
/// live `AppState`.
fn doc_page_html(title: &str, theme: &str, heading: &str, markdown: &str) -> String {
    let body = format!(
        r#"<h1>{heading}</h1>
<div class="card">
  <pre style="white-space: pre-wrap; margin: 0;">{content}</pre>
</div>"#,
        heading = html_escape(heading),
        content = html_escape(markdown),
    );
    wrap_page(title, theme, Nav::Info, &format!("{INFO_SUB_NAV}{body}"))
}

/// Render a document page as an HTTP response.
fn render_doc_page(title: &str, theme: &str, heading: &str, markdown: &str) -> Response<Body> {
    html_response(
        StatusCode::OK,
        doc_page_html(title, theme, heading, markdown),
    )
}

/// `GET /changelog` -- Changelog page.
pub async fn changelog(
    state: AppState,
    _parts: http::request::Parts,
    _params: Vec<(String, String)>,
) -> Response<Body> {
    render_doc_page(
        "Changelog",
        &state.settings().theme,
        "Changelog",
        CHANGELOG_MD,
    )
}

/// `GET /readme` -- README page.
pub async fn readme(
    state: AppState,
    _parts: http::request::Parts,
    _params: Vec<(String, String)>,
) -> Response<Body> {
    render_doc_page("README", &state.settings().theme, "README", README_MD)
}

/// `GET /administrator` -- Administrator Guide page.
pub async fn administrator_guide(
    state: AppState,
    _parts: http::request::Parts,
    _params: Vec<(String, String)>,
) -> Response<Body> {
    render_doc_page(
        "Administrator Guide",
        &state.settings().theme,
        "Administrator Guide",
        ADMINISTRATOR_GUIDE_MD,
    )
}

/// `GET /user` -- User Guide page.
pub async fn user_guide(
    state: AppState,
    _parts: http::request::Parts,
    _params: Vec<(String, String)>,
) -> Response<Body> {
    render_doc_page(
        "User Guide",
        &state.settings().theme,
        "User Guide",
        USER_GUIDE_MD,
    )
}

#[cfg(test)]
mod doc_page_tests {
    use super::*;

    fn on_disk(rel: &str) -> String {
        let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../../")
            .join(rel);
        std::fs::read_to_string(root).expect("read on-disk doc file")
    }

    #[test]
    fn changelog_embed_matches_on_disk_file() {
        assert_eq!(CHANGELOG_MD, on_disk("CHANGELOG.md"));
    }

    #[test]
    fn readme_embed_matches_on_disk_file() {
        assert_eq!(README_MD, on_disk("README.md"));
    }

    #[test]
    fn administrator_guide_embed_matches_on_disk_file() {
        assert_eq!(
            ADMINISTRATOR_GUIDE_MD,
            on_disk("documentation/Administrator_Guide.md")
        );
    }

    #[test]
    fn user_guide_embed_matches_on_disk_file() {
        assert_eq!(USER_GUIDE_MD, on_disk("documentation/User_Guide.md"));
    }

    #[test]
    fn all_four_embeds_are_non_trivial() {
        for (name, content) in [
            ("CHANGELOG_MD", CHANGELOG_MD),
            ("README_MD", README_MD),
            ("ADMINISTRATOR_GUIDE_MD", ADMINISTRATOR_GUIDE_MD),
            ("USER_GUIDE_MD", USER_GUIDE_MD),
        ] {
            assert!(
                content.len() > 200,
                "{name} is suspiciously short ({} bytes) -- embed likely broken",
                content.len()
            );
        }
    }

    #[test]
    fn changelog_page_renders_full_content_without_truncation() {
        let html = doc_page_html("Changelog", "light", "Changelog", CHANGELOG_MD);
        assert!(html.contains("<h1>Changelog</h1>"));
        // The escaped changelog body must be present in full -- assert the
        // first and last non-blank lines of the source both appear, which
        // fails if the page ever truncates the content.
        let first_line = CHANGELOG_MD.lines().next().unwrap_or_default();
        let last_line = CHANGELOG_MD.lines().last().unwrap_or_default();
        assert!(html.contains(&html_escape(first_line)));
        if !last_line.trim().is_empty() {
            assert!(html.contains(&html_escape(last_line)));
        }
    }

    #[test]
    fn readme_page_renders_ok() {
        let html = doc_page_html("README", "light", "README", README_MD);
        assert!(html.contains("<h1>README</h1>"));
    }

    #[test]
    fn administrator_guide_page_renders_ok() {
        let html = doc_page_html(
            "Administrator Guide",
            "light",
            "Administrator Guide",
            ADMINISTRATOR_GUIDE_MD,
        );
        assert!(html.contains("<h1>Administrator Guide</h1>"));
    }

    #[test]
    fn user_guide_page_renders_ok() {
        let html = doc_page_html("User Guide", "light", "User Guide", USER_GUIDE_MD);
        assert!(html.contains("<h1>User Guide</h1>"));
    }

    #[test]
    fn doc_page_html_escapes_markdown_source() {
        // The raw Markdown is shown as escaped preformatted text, not
        // rendered HTML -- a `<script>` payload in the source must never
        // survive unescaped into the page. `wrap_page` itself legitimately
        // emits `<script>` tags (theme-toggle JS), so check for the exact
        // payload rather than the bare tag.
        let payload = "<script>alert(1)</script>";
        let html = doc_page_html("Test", "light", "Test", payload);
        assert!(!html.contains(payload));
        assert!(html.contains("&lt;script&gt;alert(1)&lt;/script&gt;"));
    }

    #[test]
    fn info_dropdown_links_all_four_doc_pages() {
        let html = wrap_page("Test", "light", Nav::Info, "");
        for href in ["/changelog", "/readme", "/administrator", "/user"] {
            assert!(
                html.contains(&format!(r#"href="{href}""#)),
                "navbar Info dropdown missing link to {href}"
            );
        }
        // Divider must separate the pre-existing info pages from the new
        // document links.
        assert!(html.contains("dropdown-divider"));
    }
}