mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:27:45 +00:00
Ladybird/AppKit: Sanitize user-provided URLs with LibWebView
This commit is contained in:
parent
f023e37de7
commit
aa5cd24c90
7 changed files with 58 additions and 95 deletions
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/URL.h>
|
||||
|
||||
#import <System/Cocoa.h>
|
||||
|
||||
namespace Ladybird {
|
||||
|
||||
URL sanitize_url(NSString*);
|
||||
URL sanitize_url(StringView);
|
||||
|
||||
URL rebase_url_on_serenity_resource_root(StringView);
|
||||
|
||||
}
|
|
@ -1,51 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/DeprecatedString.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <Ladybird/Utilities.h>
|
||||
#include <LibFileSystem/FileSystem.h>
|
||||
|
||||
#import <Utilities/URL.h>
|
||||
|
||||
namespace Ladybird {
|
||||
|
||||
URL sanitize_url(StringView url_string)
|
||||
{
|
||||
if (url_string.starts_with('/') || FileSystem::exists(url_string))
|
||||
return MUST(String::formatted("file://{}", MUST(FileSystem::real_path(url_string))));
|
||||
|
||||
URL url { url_string };
|
||||
if (!url.is_valid())
|
||||
url = MUST(String::formatted("https://{}", url_string));
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
URL sanitize_url(NSString* url_string)
|
||||
{
|
||||
auto const* utf8 = [url_string UTF8String];
|
||||
return sanitize_url({ utf8, strlen(utf8) });
|
||||
}
|
||||
|
||||
URL rebase_url_on_serenity_resource_root(StringView url_string)
|
||||
{
|
||||
URL url { url_string };
|
||||
Vector<DeprecatedString> paths;
|
||||
|
||||
for (auto segment : s_serenity_resource_root.split('/'))
|
||||
paths.append(move(segment));
|
||||
|
||||
for (size_t i = 0; i < url.path_segment_count(); ++i)
|
||||
paths.append(url.path_segment_at_index(i));
|
||||
|
||||
url.set_paths(move(paths));
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue