Skip to content
API Documentation
BastionGuard™
SecureBrowser Class Reference

#include <SecureBrowser.hpp>

Static Public Member Functions

static bool open (const std::string &url, int argc, char **argv)
static void open (const std::string &url)
static void openInWindow (const std::string &url, uintptr_t window_id)
static void open_notify (const std::string &url, int argc, char **argv)
static void open_notify (const std::string &url)
static CefRefPtr< CefApp > createApp ()

Detailed Description

Definition at line 25 of file SecureBrowser.hpp.

Member Function Documentation

◆ createApp()

CefRefPtr< CefApp > SecureBrowser::createApp ( )
static

Definition at line 632 of file SecureBrowser.cpp.

632 {
633 return new ClamApp();
634}
ClamApp(bool start_minimized)
Definition main.cpp:242

References ClamApp::ClamApp().

Referenced by open(), and open_notify().

◆ open() [1/2]

void SecureBrowser::open ( const std::string & url)
inlinestatic

Definition at line 29 of file SecureBrowser.hpp.

29 {
30 open(url, 0, nullptr);
31 }
static bool open(const std::string &url, int argc, char **argv)

References open().

◆ open() [2/2]

bool SecureBrowser::open ( const std::string & url,
int argc,
char ** argv )
static

Definition at line 636 of file SecureBrowser.cpp.

636 {
638 std::cerr << _("[SecureBrowser] CEF disabilitato su questa build o distribuzione.\n");
639 return false;
640 }
641
642 if (!std::getenv("BG_SANDBOX"))
644
645 bool expected = false;
646 if (!g_cef_started.compare_exchange_strong(expected, true)) {
647 std::cerr << _("[SecureBrowser] ⚠️ Richiesta duplicata nello stesso processo.\n");
648 return false;
649 }
650
651 InstanceLock lock("/tmp/BastionGuard-bankopener.lock");
652 if (!lock.acquired()) {
653 std::cerr << _("[SecureBrowser] ℹ️ Istanza già attiva. Rifiuto nuovo avvio.\n");
654 g_cef_started = false;
655 return false;
656 }
657
658 auto fail = [&](const std::string& msg) -> bool {
659 std::cerr << msg << "\n";
660 g_cef_started = false;
661 return false;
662 };
663
664 std::cout << _("[SecureBrowser] 🧩 Avvio sandbox Blink/CEF per URL: ") << url << std::endl;
665
667
668 setenv("SSL_CERT_DIR", "/etc/ssl/certs", 1);
669 setenv("SSL_CERT_FILE", "/etc/ssl/certs/ca-certificates.crt", 1);
670 setenv("NSS_CERT_FILE", "/etc/ssl/certs/ca-certificates.crt", 1);
671 setenv("NSS_CERT_DIR", "/etc/ssl/certs", 1);
672
673 const std::string cef_dir = "/usr/share/BastionGuard/cef";
674 const std::string cache_dir = Glib::get_home_dir() + "/.cache/BastionGuard/cef_sandbox";
675 std::error_code ec;
676 fs::create_directories(cache_dir, ec);
677 if (ec) {
678 std::cerr << _("[SecureBrowser] ⚠️ Creazione cache_dir fallita: ") << ec.message() << "\n";
679 }
680
681 // Profilo CEF dedicato persistente: non importiamo cookie dal browser
682 // esterno (Firefox/Chrome), ma manteniamo quelli creati dalla sessione
683 // sicura. Questo evita di mischiare token legati a fingerprint diversi.
684 setenv("BG_CEF_PERSIST_SESSION", "1", 1);
685
686 CefMainArgs args(argc, argv);
687 CefRefPtr<CefApp> app = createApp();
688
689 auto try_init_cef = [&](const char* backend) -> bool {
690 if (backend) setenv("BastionGuard_CEF_BACKEND", backend, 1);
691
692 CefSettings settings;
693 settings.no_sandbox = true;
694 settings.windowless_rendering_enabled = false;
695 settings.log_severity = LOGSEVERITY_WARNING;
696 settings.background_color = CefColorSetARGB(0xFF, 0xFF, 0xFF, 0xFF);
697
698 CefString(&settings.resources_dir_path) = cef_dir;
699 CefString(&settings.locales_dir_path) = cef_dir + "/locales";
700 CefString(&settings.cache_path) = cache_dir;
701
702 // Il tuo SDK CEF espone persist_session_cookies (a differenza di
703 // persist_user_preferences, che nella build in uso non esiste).
704 // Con cache_path non vuoto conserva anche i cookie senza Expires/Max-Age.
705 settings.persist_session_cookies = true;
706
707 bool init_ok = CefInitialize(args, settings, app, nullptr);
708 if (!init_ok) {
709 std::cerr << _("[SecureBrowser] ❌ CefInitialize returned false for backend: ")
710 << (backend ? backend : "auto") << "\n";
711 }
712 return init_ok;
713 };
714
715 // -------------------------------------------------------------------------
716 // Scelta backend prima di CefInitialize.
717 //
718 // CEF chiama OnBeforeCommandLineProcessing UNA VOLTA SOLA, prima del primo
719 // CefInitialize, e CefInitialize non si può ri-eseguire dentro lo stesso
720 // processo. Quindi il backend va deciso QUI, e l'eventuale fallback X11
721 // va impostato via env (BastionGuard_CEF_BACKEND/OZONE_PLATFORM) prima di
722 // toccare CefInitialize. Se CefInitialize fallisce, il processo deve
723 // ri-execvp se stesso impostando l'env diverso.
724 // -------------------------------------------------------------------------
725 auto pick_backend = []() -> const char* {
726 if (const char* b = std::getenv("BastionGuard_CEF_BACKEND")) {
727 if (std::strcmp(b, "wayland") == 0 || std::strcmp(b, "x11") == 0)
728 return b;
729 }
730 if (const char* o = std::getenv("OZONE_PLATFORM")) {
731 if (std::strcmp(o, "wayland") == 0 || std::strcmp(o, "x11") == 0)
732 return o;
733 }
734 if (const char* s = std::getenv("XDG_SESSION_TYPE")) {
735 if (std::strcmp(s, "wayland") == 0) return "wayland";
736 if (std::strcmp(s, "x11") == 0) return "x11";
737 }
738 if (std::getenv("WAYLAND_DISPLAY")) return "wayland";
739 if (std::getenv("DISPLAY")) return "x11";
740 return "x11"; // default safe
741 };
742
743 const char* backend_pref = pick_backend();
744 std::cerr << _("[SecureBrowser] backend preferito: ") << backend_pref << "\n";
745
746 bool ok = try_init_cef(backend_pref);
747
748 // Fallback: se Wayland fallisce e DISPLAY è disponibile, ri-execvp con X11.
749 // (CefInitialize non si può richiamare nello stesso processo.)
750 if (!ok && std::strcmp(backend_pref, "wayland") == 0 &&
751 std::getenv("DISPLAY") && !std::getenv("BG_CEF_X11_FALLBACK")) {
752 std::cerr << _("[SecureBrowser] ⚠️ CEF Wayland fallito → re-exec con X11.\n");
753 setenv("BG_CEF_X11_FALLBACK", "1", 1);
754 setenv("BastionGuard_CEF_BACKEND", "x11", 1);
755 setenv("OZONE_PLATFORM", "x11", 1);
756 setenv("GDK_BACKEND", "x11", 1);
757 unsetenv("WAYLAND_DISPLAY");
758 // execvp con argv originale: re-entry sicuro, niente stato CEF a metà.
759 if (argv && argv[0]) {
760 std::vector<char*> reargv;
761 for (int i = 0; i < argc; ++i) reargv.push_back(argv[i]);
762 reargv.push_back(nullptr);
763 execvp(argv[0], reargv.data());
764 std::perror("execvp(x11 fallback)");
765 }
766 }
767
768 if (!ok) {
769 return fail(_("[SecureBrowser] ❌ CefInitialize fallito definitivamente."));
770 }
771
772 CefRefPtr<SimpleHandler> handler = new SimpleHandler();
773 CefRefPtr<SecureBrowserViewDelegate> view_delegate =
774 new SecureBrowserViewDelegate();
775
776 CefBrowserSettings bset;
777 bset.javascript = STATE_ENABLED;
778 bset.webgl = STATE_ENABLED;
779
780 CefRefPtr<CefBrowserView> mainView =
781 CefBrowserView::CreateBrowserView(
782 handler, url, bset, nullptr, nullptr, view_delegate);
783
784 if (!mainView) {
785 CefShutdown();
786 return fail(_("[SecureBrowser] ❌ CreateBrowserView() ha restituito nullptr."));
787 }
788
789 class WindowDelegate : public CefWindowDelegate {
790 public:
791 explicit WindowDelegate(CefRefPtr<CefBrowserView> main) : mainView_(main) {}
792
793 void OnWindowCreated(CefRefPtr<CefWindow> window) override {
794 window->AddChildView(mainView_);
795 window->SetBounds(CefRect(100, 100, 1200, 800));
796 mainView_->SetBounds(CefRect(0, 0, 1200, 800));
797 window->SetTitle("BastionGuard Secure Browser");
798 window->Show();
799 }
800
801 void OnWindowDestroyed(CefRefPtr<CefWindow>) override {
802 CefQuitMessageLoop();
803 }
804
805 IMPLEMENT_REFCOUNTING(WindowDelegate);
806
807 private:
808 CefRefPtr<CefBrowserView> mainView_;
809 };
810
811 CefWindow::CreateTopLevelWindow(new WindowDelegate(mainView));
812
813 std::cout << _("[SecureBrowser] ✅ Finestra sandbox creata, avvio message loop.\n");
814
815 CefRunMessageLoop();
816 CefShutdown();
817
818 g_cef_started = false;
819 std::cout << _("[SecureBrowser] ✅ Sandbox chiusa correttamente.\n");
820 return true;
821}
#define _(s)
static void clear_proxy_env()
void start_local_warning_server()
static std::atomic< bool > g_cef_started
static CefRefPtr< CefApp > createApp()
void fail(const std::string &message)

References _, InstanceLock::acquired(), BastionGuard::Platform::cef_available(), clear_proxy_env(), createApp(), g_cef_started, main(), and start_local_warning_server().

Referenced by open(), SecureList::open_if_allowed(), BankOpener::open_if_trusted(), and openInWindow().

◆ open_notify() [1/2]

void SecureBrowser::open_notify ( const std::string & url)
inlinestatic

Definition at line 40 of file SecureBrowser.hpp.

40 {
41 open_notify(url, 0, nullptr);
42 }
static void open_notify(const std::string &url, int argc, char **argv)

References open_notify().

◆ open_notify() [2/2]

void SecureBrowser::open_notify ( const std::string & url,
int argc,
char ** argv )
static

Definition at line 897 of file SecureBrowser.cpp.

897 {
899 std::cerr << _("[SecureBrowser] Notifiche CEF disabilitate su questa build o distribuzione.\n");
900 return;
901 }
902
903 static std::atomic<bool> s_running{false};
904 bool expected = false;
905 if (!s_running.compare_exchange_strong(expected, true)) {
906 std::cerr << _("[SecureBrowser] open_notify: già in esecuzione, ignoro\n");
907 return;
908 }
909
910 InstanceLock lock("/tmp/BastionGuard-notify.lock");
911 if (!lock.acquired()) {
912 std::cerr << _("[SecureBrowser] open_notify: istanza notifica già attiva\n");
913 s_running = false;
914 return;
915 }
916
917 std::cout << _("[SecureBrowser] 🛡️ Apertura finestra notifica: ") << url << "\n";
918
920
921 setenv("SSL_CERT_DIR", "/etc/ssl/certs", 1);
922 setenv("SSL_CERT_FILE", "/etc/ssl/certs/ca-certificates.crt", 1);
923
924 const std::string cef_dir = "/usr/share/BastionGuard/cef";
925 const std::string cache_dir = Glib::get_home_dir() + "/.cache/BastionGuard/cef_notify";
926 std::error_code ec;
927 fs::create_directories(cache_dir, ec);
928
929 CefMainArgs args(argc ? argc : 0, argv);
930 CefRefPtr<CefApp> app = createApp();
931
932 CefSettings settings;
933 settings.no_sandbox = true;
934 settings.windowless_rendering_enabled = false;
935 settings.log_severity = LOGSEVERITY_DISABLE;
936 settings.background_color = CefColorSetARGB(0xFF, 0x07, 0x09, 0x0F);
937
938 CefString(&settings.resources_dir_path) = cef_dir;
939 CefString(&settings.locales_dir_path) = cef_dir + "/locales";
940 CefString(&settings.cache_path) = cache_dir;
941
942 if (!CefInitialize(args, settings, app, nullptr)) {
943 std::cerr << _("[SecureBrowser] ❌ open_notify: CefInitialize fallito\n");
944 s_running = false;
945 return;
946 }
947
948 class NotifyHandler : public CefClient,
949 public CefLifeSpanHandler,
950 public CefDisplayHandler {
951 public:
952 CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override { return this; }
953 CefRefPtr<CefDisplayHandler> GetDisplayHandler() override { return this; }
954
955 void OnAfterCreated(CefRefPtr<CefBrowser>) override {}
956
957 void OnBeforeClose(CefRefPtr<CefBrowser>) override {
958 CEF_REQUIRE_UI_THREAD();
959 CefQuitMessageLoop();
960 }
961
962 IMPLEMENT_REFCOUNTING(NotifyHandler);
963 };
964
965 CefRefPtr<NotifyHandler> handler = new NotifyHandler();
966 CefBrowserSettings bset;
967 bset.javascript = STATE_ENABLED;
968
969 CefRefPtr<CefBrowserView> view =
970 CefBrowserView::CreateBrowserView(handler, url, bset, nullptr, nullptr, nullptr);
971
972 if (!view) {
973 std::cerr << _("[SecureBrowser] ❌ open_notify: CreateBrowserView fallito\n");
974 CefShutdown();
975 s_running = false;
976 return;
977 }
978
979 class NotifyWindowDelegate : public CefWindowDelegate {
980 public:
981 explicit NotifyWindowDelegate(CefRefPtr<CefBrowserView> v) : view_(v) {}
982
983 void OnWindowCreated(CefRefPtr<CefWindow> w) override {
984 w->AddChildView(view_);
985 w->SetBounds(CefRect(200, 200, 560, 420));
986 view_->SetBounds(CefRect(0, 0, 560, 420));
987 w->SetTitle(_("BastionGuard — Protected Session"));
988 w->Show();
989 }
990
991 void OnWindowDestroyed(CefRefPtr<CefWindow>) override {
992 CefQuitMessageLoop();
993 }
994
995 IMPLEMENT_REFCOUNTING(NotifyWindowDelegate);
996
997 private:
998 CefRefPtr<CefBrowserView> view_;
999 };
1000
1001 CefWindow::CreateTopLevelWindow(new NotifyWindowDelegate(view));
1002 CefRunMessageLoop();
1003 CefShutdown();
1004
1005 s_running = false;
1006 std::cout << _("[SecureBrowser] ✅ Finestra notifica chiusa.\n");
1007}

References _, InstanceLock::acquired(), BastionGuard::Platform::cef_available(), clear_proxy_env(), and createApp().

Referenced by open_notify(), and open_notify_window().

◆ openInWindow()

void SecureBrowser::openInWindow ( const std::string & url,
uintptr_t window_id )
inlinestatic

Definition at line 32 of file SecureBrowser.hpp.

32 {
33 open(url, 0, nullptr);
34 }

References open().


The documentation for this class was generated from the following files: