Skip to content

Commit 4ac792a

Browse files
committed
Update local file scheme handler
1 parent f5f64b1 commit 4ac792a

11 files changed

+132
-18
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ target_sources(
3131
cef-headers.hpp
3232
deps/base64/base64.cpp
3333
deps/base64/base64.hpp
34+
deps/ip-string.hpp
3435
deps/signal-restore.cpp
3536
deps/signal-restore.hpp
3637
deps/wide-string.cpp

browser-app.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ CefRefPtr<CefBrowserProcessHandler> BrowserApp::GetBrowserProcessHandler()
5050

5151
void BrowserApp::OnRegisterCustomSchemes(CefRawPtr<CefSchemeRegistrar> registrar)
5252
{
53-
registrar->AddCustomScheme("http", CEF_SCHEME_OPTION_STANDARD | CEF_SCHEME_OPTION_CORS_ENABLED);
53+
registrar->AddCustomScheme("obsbrowser", CEF_SCHEME_OPTION_STANDARD | CEF_SCHEME_OPTION_CORS_ENABLED |
54+
CEF_SCHEME_OPTION_FETCH_ENABLED);
5455
}
5556

5657
void BrowserApp::OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> command_line)

browser-scheme.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
******************************************************************************/
1818

1919
#include "browser-scheme.hpp"
20+
#include "ip-string.hpp"
2021
#include "wide-string.hpp"
2122
#include <include/wrapper/cef_stream_resource_handler.h>
2223

@@ -41,18 +42,28 @@ CefRefPtr<CefResourceHandler> BrowserSchemeHandlerFactory::Create(CefRefPtr<CefB
4142
if (fileExtension.compare("woff2") == 0)
4243
fileExtension = "woff";
4344

44-
#ifdef _WIN32
45-
CefRefPtr<CefStreamReader> stream = CefStreamReader::CreateForFile(path.substr(1));
46-
#else
47-
CefRefPtr<CefStreamReader> stream = CefStreamReader::CreateForFile(path);
48-
#endif
49-
50-
if (stream) {
51-
CefString mimeType = CefGetMimeType(fileExtension);
52-
if (mimeType.empty())
53-
mimeType = "application/octet-stream";
54-
return new CefStreamResourceHandler(mimeType, stream);
55-
} else {
45+
std::string filePath = path.substr(path.find_first_not_of("/"));
46+
std::string checkString = filePath.substr(0, filePath.find_first_of("/"));
47+
48+
// An IP address should never be a valid path for CreateForFile normally, but in some cases an OS
49+
// can resolve one as such. As an extra safeguard, we prevent any IP addresses in the path.
50+
if (checkForIpv4String(checkString)) {
51+
return nullptr;
52+
}
53+
54+
if (checkForIpv6String(checkString)) {
55+
return nullptr;
56+
}
57+
58+
CefRefPtr<CefStreamReader> stream = CefStreamReader::CreateForFile(filePath);
59+
if (!stream) {
5660
return nullptr;
5761
}
62+
63+
CefString mimeType = CefGetMimeType(fileExtension);
64+
if (mimeType.empty()) {
65+
mimeType = "application/octet-stream";
66+
}
67+
68+
return new CefStreamResourceHandler(mimeType, stream);
5869
}

cmake/os-linux.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ target_include_directories(browser-helper PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/d
2121

2222
target_link_libraries(browser-helper PRIVATE CEF::Wrapper CEF::Library)
2323

24+
target_sources(obs-browser PRIVATE deps/ip-string.cpp)
25+
2426
set(OBS_EXECUTABLE_DESTINATION "${OBS_PLUGIN_DESTINATION}")
2527

2628
# cmake-format: off

cmake/os-macos.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ foreach(helper IN LISTS helper_suffixes)
5454
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/macos/entitlements-helper${helper_plist}.plist")
5555
endforeach()
5656

57+
target_sources(obs-browser PRIVATE deps/ip-string.cpp)
58+
5759
set_target_properties(
5860
obs-browser
5961
PROPERTIES AUTOMOC ON

cmake/os-windows.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ target_compile_definitions(obs-browser-helper PRIVATE ENABLE_BROWSER_SHARED_TEXT
2222
target_link_libraries(obs-browser-helper PRIVATE CEF::Wrapper CEF::Library nlohmann_json::nlohmann_json)
2323
target_link_options(obs-browser-helper PRIVATE /IGNORE:4099 /SUBSYSTEM:WINDOWS)
2424

25+
target_link_libraries(obs-browser PRIVATE Ws2_32)
26+
target_sources(obs-browser PRIVATE deps/ip-string-windows.cpp)
27+
2528
set(OBS_EXECUTABLE_DESTINATION "${OBS_PLUGIN_DESTINATION}")
2629
set_target_properties_obs(
2730
obs-browser-helper

deps/ip-string-posix.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/******************************************************************************
2+
Copyright (C) 2026 by Warchamp7 <[email protected]>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 2 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
******************************************************************************/
17+
18+
#include "ip-string.hpp"
19+
20+
#include <arpa/inet.h>
21+
#include <stdio.h>
22+
23+
bool checkForIpv4String(const std::string &path)
24+
{
25+
sockaddr_in sa4;
26+
27+
return inet_pton(AF_INET, path.c_str(), &sa4.sin_addr) == 1;
28+
}
29+
30+
bool checkForIpv6String(const std::string &path)
31+
{
32+
sockaddr_in6 sa6;
33+
34+
return inet_pton(AF_INET6, path.c_str(), &sa6.sin6_addr) == 1;
35+
}

deps/ip-string-windows.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/******************************************************************************
2+
Copyright (C) 2026 by Warchamp7 <[email protected]>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 2 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
******************************************************************************/
17+
18+
#include "ip-string.hpp"
19+
20+
#include <stdio.h>
21+
#define WIN32_LEAN_AND_MEAN
22+
#include <winsock2.h>
23+
#include <ws2tcpip.h>
24+
25+
bool checkForIpv4String(const std::string &path)
26+
{
27+
sockaddr_in sa4;
28+
29+
return inet_pton(AF_INET, path.c_str(), &sa4.sin_addr) == 1;
30+
}
31+
32+
bool checkForIpv6String(const std::string &path)
33+
{
34+
sockaddr_in6 sa6;
35+
36+
return inet_pton(AF_INET6, path.c_str(), &sa6.sin6_addr) == 1;
37+
}

deps/ip-string.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/******************************************************************************
2+
Copyright (C) 2026 by Warchamp7 <[email protected]>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 2 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
******************************************************************************/
17+
18+
#pragma once
19+
20+
#include <string>
21+
22+
extern bool checkForIpv4String(const std::string &path);
23+
extern bool checkForIpv6String(const std::string &path);

obs-browser-plugin.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,8 @@ static void BrowserInit(void)
388388
return;
389389
}
390390

391-
/* Register http://absolute/ scheme handler for older
392-
* CEF builds which do not support file:// URLs */
393-
CefRegisterSchemeHandlerFactory("http", "absolute", new BrowserSchemeHandlerFactory());
391+
// Register custom scheme handler for local browser sources
392+
CefRegisterSchemeHandlerFactory("obsbrowser", "file", new BrowserSchemeHandlerFactory());
394393

395394
os_event_signal(cef_started_event);
396395
}

0 commit comments

Comments
 (0)