Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

[RTL] Add property to limit locations BBCode can load resources from.

Pāvels Nadtočajevs6969b0011735
diff --git a/doc/classes/RichTextLabel.xml b/doc/classes/RichTextLabel.xml
index 7cb6f0db814..813115dd5c0 100644
--- a/doc/classes/RichTextLabel.xml
+++ b/doc/classes/RichTextLabel.xml
@@ -777,6 +777,9 @@
If [code]true[/code], the text size will automatically shrink or grow to fit within the node's bounding rectangle. It will pick the largest font size between [member minimum_font_size] and [member maximum_font_size] that doesn't cause the text to be truncated.
Font sizes that are overridden with the [code skip-lint][font_size][/code] tag are scaled relative to the theme font size. For example, if the theme font size is [code]16[/code] and [code skip-lint][font_size=24][/code] is used with an automatic font size determined to be [code]32[/code], text in the overridden block will be in size [code]48[/code].
</member>
+ <member name="resource_access_flags" type="int" setter="set_resource_access_flags" getter="get_resource_access_flags" enum="RichTextLabel.ResourceAccessFlag" is_bitfield="true" default="3">
+ The set of locations BBCode is allowed to load resource files from.
+ </member>
<member name="scroll_active" type="bool" setter="set_scroll_active" getter="is_scroll_active" default="true">
If [code]true[/code], the scrollbar is visible. Setting this to [code]false[/code] does not block scrolling completely. See [method scroll_to_line].
</member>
@@ -921,6 +924,21 @@
<constant name="UPDATE_WIDTH_UNIT" value="128" enum="ImageUpdateMask" is_bitfield="true">
If this bit is set, [method update_image] changes the units used to calculate image size.
</constant>
+ <constant name="RESOURCE_ACCESS_RESOURCES" value="1" enum="ResourceAccessFlag" is_bitfield="true">
+ Allows accessing files under the [Resource] path ([code]res://[/code]).
+ </constant>
+ <constant name="RESOURCE_ACCESS_USERDATA" value="2" enum="ResourceAccessFlag" is_bitfield="true">
+ Allows accessing files under the user data path ([code]user://[/code]).
+ </constant>
+ <constant name="RESOURCE_ACCESS_FILESYSTEM" value="4" enum="ResourceAccessFlag" is_bitfield="true">
+ Allows accessing files on the whole local file system.
+ </constant>
+ <constant name="RESOURCE_ACCESS_PIPE" value="8" enum="ResourceAccessFlag" is_bitfield="true">
+ Allows accessing named pipes ([code]pipe://[/code]).
+ </constant>
+ <constant name="RESOURCE_ACCESS_NETWORK" value="16" enum="ResourceAccessFlag" is_bitfield="true">
+ Allows accessing files on network shares.
+ </constant>
<constant name="IMAGE_UNIT_PIXEL" value="0" enum="ImageUnit">
Images drawn with this unit will be in pixels.
</constant>
diff --git a/scene/gui/rich_text_label.cpp b/scene/gui/rich_text_label.cpp
index 0863c0f8cca..cde29788c13 100644
--- a/scene/gui/rich_text_label.cpp
+++ b/scene/gui/rich_text_label.cpp
@@ -5122,6 +5122,7 @@ void RichTextLabel::push_table(int p_columns, InlineAlignment p_alignment, int p
ERR_FAIL_COND(current->type == ITEM_TABLE);
ERR_FAIL_COND(p_columns < 1);
+ ERR_FAIL_COND(p_columns > 65536);
ItemTable *item = memnew(ItemTable);
item->rid = items.make_rid(item);
item->name = p_alt_text;
@@ -6261,9 +6262,13 @@ void RichTextLabel::append_text(const String &p_bbcode) {
}
if (font_option) {
const String &fnt = font_option->value;
- Ref<Font> font = ResourceLoader::load(fnt, "Font");
- if (font.is_valid()) {
- f = font;
+ if (_validate_resource_path(fnt)) {
+ Ref<Font> font = ResourceLoader::load(fnt, "Font");
+ if (font.is_valid()) {
+ f = font;
+ }
+ } else {
+ WARN_PRINT(vformat("Attempting to open resource \"%s\" outside allowed location set.", fnt));
}
}
OptionMap::Iterator font_size_option = bbcode_options.find("font_size");
@@ -6388,123 +6393,127 @@ void RichTextLabel::append_text(const String &p_bbcode) {
String image = bbcode.substr(brk_end + 1, end - brk_end - 1);
String alt_text;
- Ref<Texture2D> texture = ResourceLoader::load(image, "Texture2D");
- if (texture.is_valid()) {
- Rect2 region;
- OptionMap::Iterator region_option = bbcode_options.find("region");
- if (region_option) {
- Vector<String> region_values = _split_unquoted(region_option->value, U',');
- if (region_values.size() == 4) {
- region.position.x = region_values[0].to_float();
- region.position.y = region_values[1].to_float();
- region.size.x = region_values[2].to_float();
- region.size.y = region_values[3].to_float();
+ if (_validate_resource_path(image)) {
+ Ref<Texture2D> texture = ResourceLoader::load(image, "Texture2D");
+ if (texture.is_valid()) {
+ Rect2 region;
+ OptionMap::Iterator region_option = bbcode_options.find("region");
+ if (region_option) {
+ Vector<String> region_values = _split_unquoted(region_option->value, U',');
+ if (region_values.size() == 4) {
+ region.position.x = region_values[0].to_float();
+ region.position.y = region_values[1].to_float();
+ region.size.x = region_values[2].to_float();
+ region.size.y = region_values[3].to_float();
+ }
}
- }
- Color color = Color(1.0, 1.0, 1.0);
- OptionMap::Iterator color_option = bbcode_options.find("color");
- if (color_option) {
- color = Color::from_string(color_option->value, color);
- }
+ Color color = Color(1.0, 1.0, 1.0);
+ OptionMap::Iterator color_option = bbcode_options.find("color");
+ if (color_option) {
+ color = Color::from_string(color_option->value, color);
+ }
- OptionMap::Iterator alt_text_option = bbcode_options.find("alt");
- if (alt_text_option) {
- alt_text = alt_text_option->value;
- }
+ OptionMap::Iterator alt_text_option = bbcode_options.find("alt");
+ if (alt_text_option) {
+ alt_text = alt_text_option->value;
+ }
- float width = 0;
- float height = 0;
- bool pad = false;
- String tooltip;
- ImageUnit width_unit = IMAGE_UNIT_PIXEL;
- ImageUnit height_unit = IMAGE_UNIT_PIXEL;
- if (!bbcode_value.is_empty()) {
- int sep = bbcode_value.find_char('x');
- if (sep == -1) {
- if (bbcode_value.ends_with("%")) {
- width_unit = IMAGE_UNIT_PERCENT;
+ float width = 0;
+ float height = 0;
+ bool pad = false;
+ String tooltip;
+ ImageUnit width_unit = IMAGE_UNIT_PIXEL;
+ ImageUnit height_unit = IMAGE_UNIT_PIXEL;
+ if (!bbcode_value.is_empty()) {
+ int sep = bbcode_value.find_char('x');
+ if (sep == -1) {
+ if (bbcode_value.ends_with("%")) {
+ width_unit = IMAGE_UNIT_PERCENT;
+ }
+ width = bbcode_value.to_float();
+ } else {
+ if (bbcode_value.substr(0, sep).ends_with("%")) {
+ width_unit = IMAGE_UNIT_PERCENT;
+ }
+ width = bbcode_value.substr(0, sep).to_float();
+ if (bbcode_value.substr(sep + 1).ends_with("%")) {
+ height_unit = IMAGE_UNIT_PERCENT;
+ }
+ height = bbcode_value.substr(sep + 1).to_float();
}
- width = bbcode_value.to_float();
} else {
- if (bbcode_value.substr(0, sep).ends_with("%")) {
- width_unit = IMAGE_UNIT_PERCENT;
- }
- width = bbcode_value.substr(0, sep).to_float();
- if (bbcode_value.substr(sep + 1).ends_with("%")) {
- height_unit = IMAGE_UNIT_PERCENT;
- }
- height = bbcode_value.substr(sep + 1).to_float();
- }
- } else {
- OptionMap::Iterator align_option = bbcode_options.find("align");
- if (align_option) {
- Vector<String> subtag = _split_unquoted(align_option->value, U',');
- _normalize_subtags(subtag);
-
- if (subtag.size() > 1) {
- if (subtag[0] == "top" || subtag[0] == "t") {
- alignment = INLINE_ALIGNMENT_TOP_TO;
- } else if (subtag[0] == "center" || subtag[0] == "c") {
- alignment = INLINE_ALIGNMENT_CENTER_TO;
- } else if (subtag[0] == "bottom" || subtag[0] == "b") {
- alignment = INLINE_ALIGNMENT_BOTTOM_TO;
- }
- if (subtag[1] == "top" || subtag[1] == "t") {
- alignment |= INLINE_ALIGNMENT_TO_TOP;
- } else if (subtag[1] == "center" || subtag[1] == "c") {
- alignment |= INLINE_ALIGNMENT_TO_CENTER;
- } else if (subtag[1] == "baseline" || subtag[1] == "l") {
- alignment |= INLINE_ALIGNMENT_TO_BASELINE;
- } else if (subtag[1] == "bottom" || subtag[1] == "b") {
- alignment |= INLINE_ALIGNMENT_TO_BOTTOM;
+ OptionMap::Iterator align_option = bbcode_options.find("align");
+ if (align_option) {
+ Vector<String> subtag = _split_unquoted(align_option->value, U',');
+ _normalize_subtags(subtag);
+
+ if (subtag.size() > 1) {
+ if (subtag[0] == "top" || subtag[0] == "t") {
+ alignment = INLINE_ALIGNMENT_TOP_TO;
+ } else if (subtag[0] == "center" || subtag[0] == "c") {
+ alignment = INLINE_ALIGNMENT_CENTER_TO;
+ } else if (subtag[0] == "bottom" || subtag[0] == "b") {
+ alignment = INLINE_ALIGNMENT_BOTTOM_TO;
+ }
+ if (subtag[1] == "top" || subtag[1] == "t") {
+ alignment |= INLINE_ALIGNMENT_TO_TOP;
+ } else if (subtag[1] == "center" || subtag[1] == "c") {
+ alignment |= INLINE_ALIGNMENT_TO_CENTER;
+ } else if (subtag[1] == "baseline" || subtag[1] == "l") {
+ alignment |= INLINE_ALIGNMENT_TO_BASELINE;
+ } else if (subtag[1] == "bottom" || subtag[1] == "b") {
+ alignment |= INLINE_ALIGNMENT_TO_BOTTOM;
+ }
+ } else if (!subtag.is_empty()) {
+ if (subtag[0] == "top" || subtag[0] == "t") {
+ alignment = INLINE_ALIGNMENT_TOP;
+ } else if (subtag[0] == "center" || subtag[0] == "c") {
+ alignment = INLINE_ALIGNMENT_CENTER;
+ } else if (subtag[0] == "bottom" || subtag[0] == "b") {
+ alignment = INLINE_ALIGNMENT_BOTTOM;
+ }
}
- } else if (!subtag.is_empty()) {
- if (subtag[0] == "top" || subtag[0] == "t") {
- alignment = INLINE_ALIGNMENT_TOP;
- } else if (subtag[0] == "center" || subtag[0] == "c") {
- alignment = INLINE_ALIGNMENT_CENTER;
- } else if (subtag[0] == "bottom" || subtag[0] == "b") {
- alignment = INLINE_ALIGNMENT_BOTTOM;
+ }
+ OptionMap::Iterator width_option = bbcode_options.find("width");
+ if (width_option) {
+ width = width_option->value.to_float();
+ if (width_option->value.ends_with("%")) {
+ width = width_option->value.trim_suffix("%").to_float();
+ width_unit = IMAGE_UNIT_PERCENT;
+ } else if (width_option->value.ends_with("em")) {
+ width = width_option->value.trim_suffix("em").to_float();
+ width_unit = IMAGE_UNIT_EM;
}
}
- }
- OptionMap::Iterator width_option = bbcode_options.find("width");
- if (width_option) {
- width = width_option->value.to_float();
- if (width_option->value.ends_with("%")) {
- width = width_option->value.trim_suffix("%").to_float();
- width_unit = IMAGE_UNIT_PERCENT;
- } else if (width_option->value.ends_with("em")) {
- width = width_option->value.trim_suffix("em").to_float();
- width_unit = IMAGE_UNIT_EM;
+
+ OptionMap::Iterator height_option = bbcode_options.find("height");
+ if (height_option) {
+ height = height_option->value.to_float();
+ if (height_option->value.ends_with("%")) {
+ height = height_option->value.trim_suffix("%").to_float();
+ height_unit = IMAGE_UNIT_PERCENT;
+ } else if (height_option->value.ends_with("em")) {
+ height = height_option->value.trim_suffix("em").to_float();
+ height_unit = IMAGE_UNIT_EM;
+ }
}
- }
- OptionMap::Iterator height_option = bbcode_options.find("height");
- if (height_option) {
- height = height_option->value.to_float();
- if (height_option->value.ends_with("%")) {
- height = height_option->value.trim_suffix("%").to_float();
- height_unit = IMAGE_UNIT_PERCENT;
- } else if (height_option->value.ends_with("em")) {
- height = height_option->value.trim_suffix("em").to_float();
- height_unit = IMAGE_UNIT_EM;
+ OptionMap::Iterator tooltip_option = bbcode_options.find("tooltip");
+ if (tooltip_option) {
+ tooltip = tooltip_option->value;
}
- }
- OptionMap::Iterator tooltip_option = bbcode_options.find("tooltip");
- if (tooltip_option) {
- tooltip = tooltip_option->value;
+ OptionMap::Iterator pad_option = bbcode_options.find("pad");
+ if (pad_option) {
+ pad = (pad_option->value == "true");
+ }
}
- OptionMap::Iterator pad_option = bbcode_options.find("pad");
- if (pad_option) {
- pad = (pad_option->value == "true");
- }
+ add_image(texture, width, height, color, (InlineAlignment)alignment, region, Variant(), pad, tooltip, width_unit, height_unit, alt_text);
}
-
- add_image(texture, width, height, color, (InlineAlignment)alignment, region, Variant(), pad, tooltip, width_unit, height_unit, alt_text);
+ } else {
+ WARN_PRINT(vformat("Attempting to open resource \"%s\" outside allowed location set.", image));
}
pos = end;
@@ -6592,10 +6601,14 @@ void RichTextLabel::append_text(const String &p_bbcode) {
Vector<String> base_tag_block = _split_unquoted(tag, ' ');
if (!base_tag_block.is_empty()) {
const String &fnt = _get_tag_value(base_tag_block[0]).unquote();
- Ref<Font> font_data = ResourceLoader::load(fnt, "Font");
- if (font_data.is_valid()) {
- font = font_data;
- def_font = RTL_CUSTOM_FONT;
+ if (_validate_resource_path(fnt)) {
+ Ref<Font> font_data = ResourceLoader::load(fnt, "Font");
+ if (font_data.is_valid()) {
+ font = font_data;
+ def_font = RTL_CUSTOM_FONT;
+ }
+ } else {
+ WARN_PRINT(vformat("Attempting to open resource \"%s\" outside allowed location set.", fnt));
}
}
}
@@ -6606,10 +6619,14 @@ void RichTextLabel::append_text(const String &p_bbcode) {
}
if (name_option) {
const String &fnt = name_option->value;
- Ref<Font> font_data = ResourceLoader::load(fnt, "Font");
- if (font_data.is_valid()) {
- font = font_data;
- def_font = RTL_CUSTOM_FONT;
+ if (_validate_resource_path(fnt)) {
+ Ref<Font> font_data = ResourceLoader::load(fnt, "Font");
+ if (font_data.is_valid()) {
+ font = font_data;
+ def_font = RTL_CUSTOM_FONT;
+ }
+ } else {
+ WARN_PRINT(vformat("Attempting to open resource \"%s\" outside allowed location set.", fnt));
}
}
OptionMap::Iterator size_option = bbcode_options.find("size");
@@ -7948,6 +7965,40 @@ int RichTextLabel::get_line_width(int p_line) const {
return 0;
}
+bool RichTextLabel::_validate_resource_path(const String &p_path) const {
+ if (p_path.begins_with("res://") || p_path.begins_with("uid://") || p_path.is_relative_path()) {
+ return access_flags.has_flag(RichTextLabel::RESOURCE_ACCESS_RESOURCES);
+ } else if (p_path.begins_with("user://")) {
+ return access_flags.has_flag(RichTextLabel::RESOURCE_ACCESS_USERDATA);
+ } else if (p_path.begins_with("pipe://")) {
+ return access_flags.has_flag(RichTextLabel::RESOURCE_ACCESS_PIPE);
+ } else if (p_path.is_network_share_path()) {
+ return access_flags.has_flag(RichTextLabel::RESOURCE_ACCESS_NETWORK);
+ } else {
+ return access_flags.has_flag(RichTextLabel::RESOURCE_ACCESS_FILESYSTEM);
+ }
+}
+
+BitField<RichTextLabel::ResourceAccessFlag> RichTextLabel::get_resource_access_flags() const {
+ return access_flags;
+}
+
+void RichTextLabel::set_resource_access_flags(BitField<RichTextLabel::ResourceAccessFlag> p_flags) {
+ if (p_flags == access_flags) {
+ return;
+ }
+ access_flags = p_flags;
+
+ // Reload text.
+ if (!stack_externally_modified) {
+ if (text.is_empty()) {
+ clear();
+ } else {
+ _apply_translation();
+ }
+ }
+}
+
#ifndef DISABLE_DEPRECATED
// People will be very angry, if their texts get erased, because of #39148. (3.x -> 4.0)
// Although some people may not used bbcode_text, so we only overwrite, if bbcode_text is not empty.
@@ -8166,7 +8217,11 @@ void RichTextLabel::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_menu_visible"), &RichTextLabel::is_menu_visible);
ClassDB::bind_method(D_METHOD("menu_option", "option"), &RichTextLabel::menu_option);
+ ClassDB::bind_method(D_METHOD("get_resource_access_flags"), &RichTextLabel::get_resource_access_flags);
+ ClassDB::bind_method(D_METHOD("set_resource_access_flags", "flags"), &RichTextLabel::set_resource_access_flags);
+
// Note: set "bbcode_enabled" first, to avoid unnecessary "text" resets.
+ ADD_PROPERTY(PropertyInfo(Variant::INT, "resource_access_flags", PROPERTY_HINT_FLAGS, "Resources:1,User Data:2,File System:4,Named Pipes:8,Network Shares:16"), "set_resource_access_flags", "get_resource_access_flags");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "bbcode_enabled"), "set_use_bbcode", "is_using_bbcode");
ADD_PROPERTY(PropertyInfo(Variant::STRING, "text", PROPERTY_HINT_MULTILINE_TEXT), "set_text", "get_text");
@@ -8244,6 +8299,12 @@ void RichTextLabel::_bind_methods() {
BIND_BITFIELD_FLAG(UPDATE_TOOLTIP);
BIND_BITFIELD_FLAG(UPDATE_WIDTH_UNIT);
+ BIND_BITFIELD_FLAG(RESOURCE_ACCESS_RESOURCES);
+ BIND_BITFIELD_FLAG(RESOURCE_ACCESS_USERDATA);
+ BIND_BITFIELD_FLAG(RESOURCE_ACCESS_FILESYSTEM);
+ BIND_BITFIELD_FLAG(RESOURCE_ACCESS_PIPE);
+ BIND_BITFIELD_FLAG(RESOURCE_ACCESS_NETWORK);
+
BIND_ENUM_CONSTANT(IMAGE_UNIT_PIXEL);
BIND_ENUM_CONSTANT(IMAGE_UNIT_PERCENT);
BIND_ENUM_CONSTANT(IMAGE_UNIT_EM);
diff --git a/scene/gui/rich_text_label.h b/scene/gui/rich_text_label.h
index 4d5323b1a0c..bfa2f930b92 100644
--- a/scene/gui/rich_text_label.h
+++ b/scene/gui/rich_text_label.h
@@ -139,6 +139,14 @@ public:
IMAGE_UNIT_EM,
};
+ enum ResourceAccessFlag {
+ RESOURCE_ACCESS_RESOURCES = 1 << 0,
+ RESOURCE_ACCESS_USERDATA = 1 << 1,
+ RESOURCE_ACCESS_FILESYSTEM = 1 << 2,
+ RESOURCE_ACCESS_PIPE = 1 << 3,
+ RESOURCE_ACCESS_NETWORK = 1 << 4,
+ };
+
protected:
virtual void _update_theme_item_cache() override;
@@ -567,6 +575,10 @@ private:
ItemMeta *meta_hovering = nullptr;
Variant current_meta;
+ BitField<ResourceAccessFlag> access_flags = RESOURCE_ACCESS_RESOURCES | RESOURCE_ACCESS_USERDATA;
+
+ bool _validate_resource_path(const String &p_path) const;
+
Array custom_effects;
HashMap<RID, Rect2> ac_element_bounds_cache;
@@ -1042,6 +1054,9 @@ public:
void install_effect(const Variant effect);
void reload_effects();
+ BitField<ResourceAccessFlag> get_resource_access_flags() const;
+ void set_resource_access_flags(BitField<ResourceAccessFlag> p_flags);
+
virtual Size2 get_minimum_size() const override;
RichTextLabel(const String &p_text = String());
@@ -1052,4 +1067,5 @@ VARIANT_ENUM_CAST(RichTextLabel::ListType);
VARIANT_ENUM_CAST(RichTextLabel::MenuItems);
VARIANT_ENUM_CAST(RichTextLabel::MetaUnderline);
VARIANT_BITFIELD_CAST(RichTextLabel::ImageUpdateMask);
+VARIANT_BITFIELD_CAST(RichTextLabel::ResourceAccessFlag);
VARIANT_ENUM_CAST(RichTextLabel::ImageUnit);