[MIRROR] Allows admins to skip some paper sanitization [MDB IGNORE] (#18728)

* Allows admins to skip some paper sanitization (#72553)

## About The Pull Request
This PR allows admins with R_FUN to create paper that doesn't sanitize
as much HTML as the default, player-accessible paper. Specifically, the
ability for admins with R_FUN to add images that papercode would
normally sanitise via HTML img tags.

## Why It's Good For The Game
I'd like to make some fancy papers to send as faxes, but the current
sanitization doesn't allow for images.

![image](https://user-images.githubusercontent.com/41448081/211176186-c33d611d-8ac3-4683-bef8-10016e34eaf4.png)

This was a fax I threw together in a few minutes after making the logo
## Changelog
🆑
admins: Admins with the appropriate permissions can now use HTML image
tags in paper and faxes.
/🆑

* Allows admins to skip some paper sanitization

Co-authored-by: Zonespace <41448081+Zonespace27@users.noreply.github.com>
This commit is contained in:
SkyratBot
2023-01-14 21:57:45 +01:00
committed by GitHub
parent 96862e7829
commit c3291cefa2
5 changed files with 56 additions and 30 deletions
+20 -20
View File
@@ -10,7 +10,7 @@
var/datum/fax_panel_interface/ui = new(usr)
ui.ui_interact(usr)
/// Admin Fax Panel. Tool for sending fax messages faster.
/datum/fax_panel_interface
/// All faxes in from machinery list()
@@ -30,14 +30,14 @@
//Get all faxes, and save them to our list.
for(var/obj/machinery/fax/fax in GLOB.machines)
available_faxes += WEAKREF(fax)
//Get all stamps
for(var/stamp in subtypesof(/obj/item/stamp))
var/obj/item/stamp/real_stamp = new stamp()
if(!istype(real_stamp, /obj/item/stamp/chameleon) && !istype(real_stamp, /obj/item/stamp/mod))
var/stamp_detail = real_stamp.get_writing_implement_details()
stamp_list += list(list(real_stamp.name, real_stamp.icon_state, stamp_detail["stamp_class"]))
//Give our paper special status, to read everywhere.
fax_paper.request_state = TRUE
@@ -74,12 +74,12 @@
for(var/stamp in stamp_list)
data["stamps"] += list(stamp[1]) // send only names.
for(var/datum/weakref/weakrefed_fax as anything in available_faxes)
var/obj/machinery/fax/another_fax = weakrefed_fax.resolve()
if(another_fax && istype(another_fax))
data["faxes"] += list(another_fax.fax_name)
return data
/datum/fax_panel_interface/ui_act(action, list/params, datum/tgui/ui, datum/ui_state/state)
@@ -88,12 +88,12 @@
if(!check_rights(R_ADMIN))
return
var/obj/machinery/fax/action_fax
if(params["faxName"])
action_fax = get_fax_by_name(params["faxName"])
action_fax = get_fax_by_name(params["faxName"])
switch(action)
if("follow")
@@ -101,20 +101,20 @@
usr.client?.admin_ghost()
usr.client?.admin_follow(action_fax)
if("preview") // see saved variant
if(!fax_paper)
return
fax_paper.ui_interact(usr)
if("save") // save paper
if(params["paperName"])
default_paper_name = params["paperName"]
if(params["fromWho"])
sending_fax_name = params["fromWho"]
fax_paper.clear_paper()
var/stamp
var/stamp
var/stamp_class
for(var/needed_stamp in stamp_list)
@@ -122,24 +122,24 @@
stamp = needed_stamp[2]
stamp_class = needed_stamp[3]
break
fax_paper.name = "paper — [default_paper_name]"
fax_paper.add_raw_text(params["rawText"])
fax_paper.add_raw_text(params["rawText"], advanced_html = TRUE)
if(stamp)
fax_paper.add_stamp(stamp_class, params["stampX"], params["stampY"], params["stampAngle"], stamp)
fax_paper.update_static_data(usr) // OK, it's work, and update UI.
fax_paper.update_static_data(usr) // OK, it's work, and update UI.
if("send")
//copy
var/obj/item/paper/our_fax = fax_paper.copy(/obj/item/paper)
our_fax.name = fax_paper.name
//send
action_fax.receive(our_fax, sending_fax_name)
message_admins("[key_name_admin(usr)] has send custom fax message to [action_fax.name][ADMIN_FLW(action_fax)][ADMIN_SHOW_PAPER(fax_paper)].")
log_admin("[key_name(usr)] has send custom fax message to [action_fax.name]")
message_admins("[key_name_admin(usr)] has sent a custom fax message to [action_fax.name][ADMIN_FLW(action_fax)][ADMIN_SHOW_PAPER(fax_paper)].")
log_admin("[key_name(usr)] has sent a custom fax message to [action_fax.name]")
if("createPaper")
var/obj/item/paper/our_paper = fax_paper.copy(/obj/item/paper, usr.loc)
our_paper.name = fax_paper.name
+10 -4
View File
@@ -161,13 +161,15 @@
* * font - The font to use.
* * color - The font color to use.
* * bold - Whether this text should be rendered completely bold.
* * advanced_html - Boolean that is true when the writer has R_FUN permission, which sanitizes less HTML (such as images) from the new paper_input
*/
/obj/item/paper/proc/add_raw_text(text, font, color, bold)
/obj/item/paper/proc/add_raw_text(text, font, color, bold, advanced_html)
var/new_input_datum = new /datum/paper_input(
text,
font,
color,
bold,
advanced_html,
)
input_field_count += get_input_field_count(text)
@@ -585,7 +587,7 @@
// Safe to assume there are writing implement details as user.can_write(...) fails with an invalid writing implement.
var/writing_implement_data = holding.get_writing_implement_details()
add_raw_text(paper_input, writing_implement_data["font"], writing_implement_data["color"], writing_implement_data["use_bold"])
add_raw_text(paper_input, writing_implement_data["font"], writing_implement_data["color"], writing_implement_data["use_bold"], check_rights_for(user?.client, R_FUN))
log_paper("[key_name(user)] wrote to [name]: \"[paper_input]\"")
to_chat(user, "You have added to your paper masterpiece!");
@@ -672,15 +674,18 @@
var/colour = ""
/// Whether to render the font bold or not.
var/bold = FALSE
/// Whether the creator of this input field has the R_FUN permission, thus allowing less sanitization
var/advanced_html = FALSE
/datum/paper_input/New(_raw_text, _font, _colour, _bold)
/datum/paper_input/New(_raw_text, _font, _colour, _bold, _advanced_html)
raw_text = _raw_text
font = _font
colour = _colour
bold = _bold
advanced_html = _advanced_html
/datum/paper_input/proc/make_copy()
return new /datum/paper_input(raw_text, font, colour, bold);
return new /datum/paper_input(raw_text, font, colour, bold, advanced_html)
/datum/paper_input/proc/to_list()
return list(
@@ -688,6 +693,7 @@
font = font,
color = colour,
bold = bold,
advanced_html = advanced_html,
)
/// A single instance of a saved stamp on paper.
+2 -2
View File
@@ -199,7 +199,7 @@ export const FaxMainPanel = (props, context) => {
faxName: fax,
})
}>
Send fax
Send
</Button>
<Button
icon="floppy-disk"
@@ -218,7 +218,7 @@ export const FaxMainPanel = (props, context) => {
fromWho: fromWho,
});
}}>
Save changes
Save
</Button>
<Button
disabled={!saved}
+9 -3
View File
@@ -29,6 +29,7 @@ type PaperContext = {
default_pen_font: string;
default_pen_color: string;
signature_font: string;
sanitize_text: boolean;
// ui_data
held_item_details?: WritingImplement;
@@ -39,6 +40,7 @@ type PaperInput = {
font?: string;
color?: string;
bold?: boolean;
advanced_html?: boolean;
};
type StampInput = {
@@ -511,6 +513,7 @@ export class PreviewView extends Component<PreviewViewProps> {
const fontColor = value.color || default_pen_color;
const fontFace = value.font || default_pen_font;
const fontBold = value.bold || false;
const advancedHtml = value.advanced_html || false;
let processingOutput = this.formatAndProcessRawText(
rawText,
@@ -519,7 +522,8 @@ export class PreviewView extends Component<PreviewViewProps> {
paper_color,
fontBold,
fieldCount,
readOnly
readOnly,
advancedHtml
);
output += processingOutput.text;
@@ -646,16 +650,18 @@ export class PreviewView extends Component<PreviewViewProps> {
paperColor: string,
bold: boolean,
fieldCounter: number = 0,
forceReadonlyFields: boolean = false
forceReadonlyFields: boolean = false,
advanced_html: boolean = false
): FieldCreationReturn => {
// First lets make sure it ends in a new line
const { data } = useBackend<PaperContext>(this.context);
rawText += rawText[rawText.length] === '\n' ? '\n' : '\n\n';
// Second, parse the text using markup
const parsedText = this.runMarkedDefault(rawText);
// Third, we sanitize the text of html
const sanitizedText = sanitizeText(parsedText);
const sanitizedText = sanitizeText(parsedText, advanced_html);
// Fourth we replace the [__] with fields
const fieldedText = this.createFields(
+15 -1
View File
@@ -45,21 +45,35 @@ const defTag = [
'ul',
];
// Advanced HTML tags that we can trust admins (but not players) with
const advTag = ['img'];
const defAttr = ['class', 'style'];
/**
* Feed it a string and it should spit out a sanitized version.
*
* @param {string} input
* @param {boolean} advHtml
* @param {array} tags
* @param {array} forbidAttr
* @param {array} advTags
*/
export const sanitizeText = (input, tags = defTag, forbidAttr = defAttr) => {
export const sanitizeText = (
input,
advHtml,
tags = defTag,
forbidAttr = defAttr,
advTags = advTag
) => {
// This is VERY important to think first if you NEED
// the tag you put in here. We are pushing all this
// though dangerouslySetInnerHTML and even though
// the default DOMPurify kills javascript, it dosn't
// kill href links or such
if (advHtml) {
tags = tags.concat(advTags);
}
return DOMPurify.sanitize(input, {
ALLOWED_TAGS: tags,
FORBID_ATTR: forbidAttr,