Skip to content

Commit 81ab2de

Browse files
committed
Update local file scheme handler
1 parent f5f64b1 commit 81ab2de

File tree

7 files changed

+120
-12
lines changed

7 files changed

+120
-12
lines changed

CMakeLists.txt

Lines changed: 5 additions & 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
@@ -46,10 +47,14 @@ target_link_libraries(obs-browser PRIVATE OBS::libobs OBS::frontend-api OBS::web
4647

4748
if(OS_WINDOWS)
4849
include(cmake/os-windows.cmake)
50+
target_link_libraries(obs-browser PRIVATE Ws2_32)
51+
target_sources(obs-browser PRIVATE deps/ip-string-windows.cpp)
4952
elseif(OS_MACOS)
5053
include(cmake/os-macos.cmake)
54+
target_sources(obs-browser PRIVATE deps/ip-string-posix.cpp)
5155
elseif(OS_LINUX)
5256
include(cmake/os-linux.cmake)
57+
target_sources(obs-browser PRIVATE deps/ip-string-posix.cpp)
5358
endif()
5459

5560
if(ENABLE_BROWSER_PANELS)

browser-scheme.cpp

Lines changed: 12 additions & 7 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

@@ -31,8 +32,15 @@ CefRefPtr<CefResourceHandler> BrowserSchemeHandlerFactory::Create(CefRefPtr<CefB
3132

3233
std::string path = CefString(&parts.path);
3334

34-
path = CefURIDecode(path, true, cef_uri_unescape_rule_t::UU_SPACES);
35-
path = CefURIDecode(path, true, cef_uri_unescape_rule_t::UU_URL_SPECIAL_CHARS_EXCEPT_PATH_SEPARATORS);
35+
// An IP address should never be a valid path for CreateForFile normally, but in some cases an OS
36+
// can resolve one as such. As an extra safeguard, we prevent any IP addresses in the path.
37+
if (checkForIpv4String(path)) {
38+
return nullptr;
39+
}
40+
41+
if (checkForIpv6String(path)) {
42+
return nullptr;
43+
}
3644

3745
std::string fileExtension = path.substr(path.find_last_of(".") + 1);
3846

@@ -41,11 +49,8 @@ CefRefPtr<CefResourceHandler> BrowserSchemeHandlerFactory::Create(CefRefPtr<CefB
4149
if (fileExtension.compare("woff2") == 0)
4250
fileExtension = "woff";
4351

44-
#ifdef _WIN32
45-
CefRefPtr<CefStreamReader> stream = CefStreamReader::CreateForFile(path.substr(1));
46-
#else
47-
CefRefPtr<CefStreamReader> stream = CefStreamReader::CreateForFile(path);
48-
#endif
52+
std::string filePath = path.substr(path.find_first_of("//file") + 7);
53+
CefRefPtr<CefStreamReader> stream = CefStreamReader::CreateForFile(filePath);
4954

5055
if (stream) {
5156
CefString mimeType = CefGetMimeType(fileExtension);

deps/ip-string-posix.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+
#pragma once
19+
20+
#include "ip-string.hpp"
21+
22+
#include <stdio.h>
23+
#include <arpa/inet.h>
24+
25+
bool checkForIpv4String(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(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-windows.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 "ip-string.hpp"
21+
22+
#include <stdio.h>
23+
#define WIN32_LEAN_AND_MEAN
24+
#include <winsock2.h>
25+
#include <ws2tcpip.h>
26+
27+
bool checkForIpv4String(std::string path)
28+
{
29+
sockaddr_in sa4;
30+
31+
return inet_pton(AF_INET, path.c_str(), &sa4.sin_addr) == 1;
32+
}
33+
34+
bool checkForIpv6String(std::string path)
35+
{
36+
sockaddr_in6 sa6;
37+
38+
return inet_pton(AF_INET6, path.c_str(), &sa6.sin6_addr) == 1;
39+
}

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(std::string path);
23+
extern bool checkForIpv6String(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
}

obs-browser-source.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,8 @@ void BrowserSource::Update(obs_data_t *settings)
498498
while (n_url.find("%2F") != std::string::npos)
499499
n_url.replace(n_url.find("%2F"), 3, "/");
500500

501-
/* http://absolute/ based mapping for older CEF */
502-
n_url = "http://absolute/" + n_url;
501+
// Local files are routed through our custom scheme handler to give them acess to other local files
502+
n_url = "obsbrowser://file/" + n_url;
503503
}
504504

505505
if (n_is_local == is_local && n_fps_custom == fps_custom && n_fps == fps &&

0 commit comments

Comments
 (0)