Line data Source code
1 : #pragma once
2 :
3 : #include <QObject>
4 : #include <QString>
5 : #include <functional>
6 :
7 : class QWebEnginePage;
8 :
9 : // T-608/SEC-04: DOMPurify-based HTML sanitizer.
10 : //
11 : // Uses a hidden QWebEnginePage (JS enabled) to run DOMPurify.sanitize()
12 : // on untrusted HTML. The sanitized output is then safe to render on the
13 : // MailView page (which has JS disabled).
14 : //
15 : // Architecture:
16 : // - On init, loads a minimal HTML skeleton that includes DOMPurify
17 : // - sanitize() calls DOMPurify.sanitize(html, config) via runJavaScript()
18 : // - Allowlist config: only safe tags/attributes pass through
19 : // - Callback delivers clean HTML asynchronously
20 : //
21 : // Thread safety: must be used on the GUI thread only.
22 : class HtmlSanitizer : public QObject {
23 : Q_OBJECT
24 :
25 : public:
26 : explicit HtmlSanitizer(QObject *parent = nullptr);
27 : ~HtmlSanitizer() override;
28 :
29 : // Initialize the sanitizer. Must be called once before sanitize().
30 : // Loads DOMPurify into the hidden page. Emits ready() when done.
31 : void init();
32 :
33 : // Returns true if the sanitizer is ready to accept sanitize() calls.
34 2 : bool isReady() const { return m_ready; }
35 :
36 : // Sanitize untrusted HTML. Calls callback with clean HTML asynchronously.
37 : // If the sanitizer is not ready, queues the request until ready.
38 : void sanitize(const QString &dirtyHtml,
39 : std::function<void(const QString &cleanHtml)> callback);
40 :
41 : signals:
42 : void ready();
43 :
44 : private:
45 : QWebEnginePage *m_page = nullptr;
46 : bool m_ready = false;
47 :
48 : // Queued request while page is loading
49 : QString m_pendingHtml;
50 : std::function<void(const QString &)> m_pendingCallback;
51 : };
|