Skip to content

Commit cc6a119

Browse files
committed
Sanitize text fields
1 parent debb028 commit cc6a119

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

app/Functions/functions.php

+12
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,16 @@ function footer($key)
138138
$title = env('TITLE_FOOTER_'.$upperStr);
139139
}
140140
return $title;
141+
}
142+
143+
function strip_tags_except_allowed_protocols($str) {
144+
preg_match_all('/<a[^>]+>(.*?)<\/a>/i', $str, $matches, PREG_SET_ORDER);
145+
146+
foreach ($matches as $val) {
147+
if (!preg_match('/href=["\'](http:|https:|mailto:|tel:)[^"\']*["\']/', $val[0])) {
148+
$str = str_replace($val[0], $val[1], $str);
149+
}
150+
}
151+
152+
return $str;
141153
}

app/Http/Controllers/UserController.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,13 @@ public function saveLink(request $request)
264264
'button_id' => "42",
265265
]);
266266
}elseif($linkType->typename == "text"){
267+
$sanitizedText = $request->text;
268+
$sanitizedText = strip_tags($sanitizedText, '<a><p><strong><i><ul><ol><li><blockquote><h2><h3><h4>');
269+
$sanitizedText = preg_replace("/<a([^>]*)>/i", "<a $1 rel=\"noopener noreferrer nofollow\">", $sanitizedText);
270+
$sanitizedText = strip_tags_except_allowed_protocols($sanitizedText);
267271
$OrigLink->update([
268272
'button_id' => "93",
269-
'title' => $request->text,
273+
'title' => $sanitizedText,
270274
]);
271275
}elseif($linkType->typename == "email"){
272276
$LinkURL = "mailto:".$LinkURL;
@@ -387,8 +391,12 @@ public function saveLink(request $request)
387391
}elseif($linkType->typename == "heading"){
388392
$links->button_id = "42";
389393
}elseif($linkType->typename == "text"){
394+
$sanitizedText = $request->text;
395+
$sanitizedText = strip_tags($sanitizedText, '<a><p><strong><i><ul><ol><li><blockquote><h2><h3><h4>');
396+
$sanitizedText = preg_replace("/<a([^>]*)>/i", "<a $1 rel=\"noopener noreferrer nofollow\">", $sanitizedText);
397+
$sanitizedText = strip_tags_except_allowed_protocols($sanitizedText);
390398
$links->button_id = "93";
391-
$links->title = $request->text;
399+
$links->title = $sanitizedText;
392400
}elseif($linkType->typename == "email"){
393401
$links->link = "mailto:".$links->link;
394402
$links->button_id = $button?->id;
@@ -789,6 +797,7 @@ public function editPage(Request $request)
789797
$pageName = $request->littlelink_name;
790798
$pageDescription = strip_tags($request->pageDescription, '<a><p><strong><i><ul><ol><li><blockquote><h2><h3><h4>');
791799
$pageDescription = preg_replace("/<a([^>]*)>/i", "<a $1 rel=\"noopener noreferrer nofollow\">", $pageDescription);
800+
$pageDescription = strip_tags_except_allowed_protocols($pageDescription);
792801
$name = $request->name;
793802
$checkmark = $request->checkmark;
794803
$sharebtn = $request->sharebtn;

0 commit comments

Comments
 (0)