英文:
Replace invalid html tags with < and > to < and >
问题
当通过机器人发送电报消息时,会使用HTML格式,这意味着当尝试发送包含<argument>的消息时会出错,您需要将这些尖括号替换为&lt;
和&gt;
,但在大段文本中每次写它们都很不方便。我想尝试创建一个正则表达式,它将自动替换这些内容,但不会触及有效的HTML标记,例如:
有效标记示例,无需替换:
<a href="tg://user?id=1">Bot</a>
需要替换的无效标记:
<argument>
这是我尝试创建的代码,但最终没有成功:
import re
def replace_invalid_tags(html_string):
invalid_tag_pattern = r'<[^a-zA-Z/!?](.*?)>'
fixed_html = re.sub(invalid_tag_pattern, r'&lt;&gt;', html_string)
return fixed_html
html_string = '<a href="#">Link</a> <argument1> <argument2>'
fixed_html = replace_invalid_tags(html_string)
print(fixed_html)
希望这对您有帮助。
英文:
When sending a message to telegram with the help of a bot, html formatting is used, which means when trying to send message with <argument> there will be an error, and you need to replace these arrows with &lt;
and &gt;
but writing them every time in a pile of text is just not convenient as hell, I would like to try to make a regular that will replace such things automatically but do not touch valid html tags for example:
Example of a valid tag that does not need to be replaced
<a href="tg://user?id=1">Bot</a>
Invalid tag that needs to be replaced
<argument>
Here is the code I tried to make, but it doesn't work in the end
import re
def replace_invalid_tags(html_string):
invalid_tag_pattern = r'<[^a-zA-Z/!?](.*?)>'
fixed_html = re.sub(invalid_tag_pattern, r'&lt;&gt;', html_string)
return fixed_html
html_string = '<a href="#">Link</a> <argument1> <argument2>'
fixed_html = replace_invalid_tags(html_string)
print(fixed_html)
答案1
得分: 1
我个人建议您使用一个Python的HTML解析或消毒库来完成这项任务。正则表达式很好,我也喜欢它们。但在某些情况下,我更倾向于使用经过充分测试的专门用于解决该问题的库。
我不是Python程序员,但大多数时候我是PHP程序员。在良好的CMS项目中,您可以添加一些消毒库,比如HTMLPurifier,并定义规则。
在您的情况下,一些标记应该被转换为HTML实体,以便正常显示文本,而在其他一些情况下,标记应该保持原样。当然,某些属性和特定的标记也应该被删除(例如:<img onload="alert('xss attack')"
或<script>alert('bad')</script>
)。这就是解析器或消毒库会做得更安全的地方。
让我们假设这些标记是允许的:
<a>
具有href
属性。可能不应该允许其他任何属性。通常,我会删除style="font-size: 100px"
。<strong>
和<em>
没有属性。那么旧的<b>
和<i>
标记呢?我会将它们分别转换为<strong>
和<em>
,因为它们可能对可读性有用,但在Telegram中不允许。
所有其他标记应该转换为 <var>
(如果允许的话),并将内容转换为HTML特殊字符(<
变成 &lt;
,>
变成 &gt;
)如您所述。如果需要的话,也许可以安全地处理其他转换。
在Python中,我看到您可以使用html-sanitizer库。
我看到可以定义一些预处理函数,通常用于根据需要转换一些标记。这意味着您可以创建一个函数,它将所有未经授权的标记转换为 <var>
或 <pre>
标记,并填充其内容为找到的标记的转义等价HTML。已经存在一些预先构建的预处理函数,比如 bold_span_to_strong() ,所以有一些示例可以解决您的问题。
用于查找无效标记的纯正则表达式解决方案可以如下:
<\s*/?\s*(?!(?:a|strong|em)\b)\w+[^>]*>
演示链接在这里:https://regex101.com/r/xiZl1n/1
我接受可选的空格,用于闭合标记的斜杠,然后使用负面先行断言来避免匹配您想接受的标记。我在有效标记之后添加了单词边界 \b
,以避免它接受以 "a" 字符开头的 <argument>
。我只想匹配完整的单词。
然后您可以决定如何处理所有匹配项。如果您直接想将 <
替换为 &lt;
,可以这样做:
https://regex101.com/r/xiZl1n/3
编辑:处理 ->
, >.<
, =>
等
我仍然坚信解析器是最佳选择。但您问是否可以更改正则表达式以处理更多情况。我个人认为一个正则表达式可能无法胜任。它肯定也会不安全。
但正如我评论的那样,您可以尝试分几个步骤进行某些操作:
-
如果您认为有必要的话,将
<i>
标记替换为<em>
,将<b>
标记替换为<strong>
。 -
找到所有有效标记,如
<a>
,<strong>
,<em>
,并将它们替换为[a]
,[strong]
和[em]
。
可以使用以下模式实现:<\s*(/?)\s*(?=(?:a|strong|em)\b)(\w+[^>]*)>
并将其替换为
[\1\2]
。演示链接在这里:https://regex101.com/r/xiZl1n/4 -
现在您可以将
<
替换为&lt;
,将>
替换为&gt;
:- 将
<
替换为&lt;
: https://regex101.com/r/xiZl1n/5 - 将
>
替换为&gt;
: https://regex101.com/r/xiZl1n/6
- 将
-
将接受的标记转换回正确的HTML。与步骤1中的正则表达式相同,但使用方括号代替:
\[\s*(/?)\s*((?:a|strong|em)\b)([^\]]*)\]
并将其替换为 `<\1\2\
英文:
I would personally suggest you use a Python HTML parsing or sanitizing
library to do the job. Regular expressions are great and I love them. But
in some cases I prefer using well-tested libraries which are especially
built for the purpose of the problem.
I'm not a Python programmer, but mostly a PHP programmer. In good
CMS projects, you can add some sanitizing libraries such as
HTMLPurifier and define rules.
In your case, some tags should be transformed to HTML entities, in order
to display as normal text and in some other cases the tags have to be
left as they are. Certainly some attributes and specific tags should be
also dropped (ex: <img onload="alert('xss attack')"
or
<script>alert('bad')</script>
. This is where parsers or sanitizing
libs will do a safer job.
Let's say that these tags are allowed:
<a>
with thehref
attribute. Probably no other attributes should
be allowed. Typically, I would dropstyle="font-size: 100px"
.<strong>
and<em>
without attributes. What about the old<b>
and
<i>
tags? I would transform them to respectively<strong>
and<em>
as they might be useful for readability but not allowed in
Telegram.
All other tags should be converted to <var>
(if allowed) with the content
converted to HTML special chars (<
to &lt;
and >
to &gt;
) as you
mentionned. Maybe it would be safe to handle other conversions if needed.
In Python, I see that you can use the
html-sanitizer library.
I see that one can define some preprocessor functions, typically
to convert some tags if needed. This means that you could create a
function that will convert all unauthorized tags to a <var>
or
<pre>
tag and fill its content with the escaped equivalent HTML of
the found tag. Some pre-built preprocessor functions already exist,
such as bold_span_to_strong() so there are some examples to solve your problem.
A pure regex solution to find invalid tags could be done with this:
<\s*/?\s*(?!(?:a|strong|em)\b)\w+[^>]*>
Demo here: https://regex101.com/r/xiZl1n/1
I'm accepting optional spaces, the slash for the closing tag and then
using a negative lookahead to avoid matching the tags you want to accept.
I added the word-boundary \b
after the valid tags to avoid that
it accepts <argument>
which starts with an "a" char. I want to match
the full word only.
You can then decide what to do with all your matches. If you directly
want to replace <
by &lt;
then you can do this:
https://regex101.com/r/xiZl1n/3
EDIT: handle ->
, >.<
, =>
, etc
I'm still convinced that a parser is the best choice. But you asked
if the regex could be changed to handle more cases. I personally
don't think one regex can do it. It will certainly be unsafe too.
But as I commented, you could try something in several steps:
-
Convert
<i>
tags to<em>
and<b>
tags to<strong>
if
you think it's worth it. -
Find all valid tags, such as
<a>
,<strong>
,<em>
and
replace them by[a]
,[strong]
and respectively[em]
.
This can be done with a pattern such as:<\s*(/?)\s*(?=(?:a|strong|em)\b)(\w+[^>]*)>
and replace it by
[\1\2]
. In action:
https://regex101.com/r/xiZl1n/4 -
Now you can replace
<
by&lt;
and>
by&gt;
:<
becomes&lt;
: https://regex101.com/r/xiZl1n/5>
becomes&gt;
: https://regex101.com/r/xiZl1n/6
-
Transform back the valid tags to correct HTML. It will be
the same regex as in step 1, but with brackets instead:\[\s*(/?)\s*((?:a|strong|em)\b)([^\]]*)\]
and replace it by
<\1\2\3>
. In action:
https://regex101.com/r/xiZl1n/8In this step, the capturing group n°3 contains all the
tag attributes. This is where you could filter it and only
accept some specific ones, such ashref
,id
,title
but remove all the others (ex:class
,style
,onclick
).
It may be important to enable the i
flag to be case-insensitive.
This is what it could look like in JavaScript:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const input = `<a href="https://www.example.com/page">Example page</a> is a <strong>valid</strong> tag.
< EM class="something"> is also allowed</em>
But <param> or <argument> should be escaped.
This also: <br/> <
br /> <img onload="alert('xss')" src="http://images.com/nice-cat.jpg" alt="Nice cat" />
<script type="text/javascript">alert('Bad JS code')</` + `script>
<a href="javascript:alert('XSS attack')" onclick="alert('xss')">A bad link</a>
<a href = http://test.com title="This is just a test">test.com</a>
Turn left <- or turn right ->
Also, accept this => or a smiley >.<
Accept <B>bold</B> and <i style="color:green">italic without style</i> converted to new tags.
Also strip <b href="https://www.google.com">wrong attributes</b>`;
// Attributes to drop are all design changes done with classes or style
// and all attributes such as onload, onclick, etc.
const regexAttributeToDrop = /^(?:style|class|on.*)$/i;
// The attributes which can have a URL.
const regexAttributeWithURL = /^(?:href|xlink:href|src)$/i;
// Only accept absolute URLs and not bad stuff like javascript:alert('xss')
const regexValidURL = /^(https?|ftp|mailto|tel):/i;
/**
* Filter tag attributes, based on the tag name, if provided.
*
* @param string attributes All the attributes of the tag.
* @param string tagName Optional tag name (in lowercase).
* @return string The filtered string of attributes.
*/
function filterAttributes(attributes, tagName = '') {
// Regex for attribute: $1 = attribute name, $2 = value, $3 = simple/double quote or nothing.
const regexAttribute = /\s*([\w-]+)\s*=\s*((?:(["']).*?|[^"'=<>\s]+))/g;
attributes = attributes.replace(regexAttribute, function (attrMatch, attrName, attrValue, quote) {
// Don't keep attributes that can change the rendering or run JavaScript.
if (name.match(regexAttributeToDrop)) {
return '';
}
// Not an attribute to drop.
else {
// If the attribute is "href" or "xlink:href" then only accept full URLs
// with the correct protocols and only for <a> tags.
if (attrName.match(/^(?:xlink:)?href$/i)) {
// If it's not a link then drop the attribute.
if (tagName !== 'a') {
return '';
}
// The attribute value can be quoted or not so we'll remove them.
const url = attrValue.replace(/^['"]|['"]$/g, '');
// If the URL is valid.
if (url.match(regexValidURL)) {
return ` ${attrName}="${url}"`;
}
// Invalid URL: drop href and notify visually.
else {
return ' class="invalid-url" title="Invalid URL"';
}
}
// All other attributes: nothing special to do.
else {
return ` ${attrName}=${attrValue}`;
}
}
});
// Clean up: trim spaces around. If it's not empty then just add a space before.
attributes = attributes.trim();
if (attributes.length) {
attributes = ' ' + attributes;
}
return attributes;
}
const steps = [
{
// Replace <b> by <strong>.
search: /<\s*(\/?)\s*(b\b)([^>]*)>/gi,
replace: '<$1strong>' // or '<$1strong$3>' to keep the attributes.
},
{
// Replace <i> by <em>.
search: /<\s*(\/?)\s*(i\b)([^>]*)>/gi,
replace: '<$1em>' // or '<$1em$3>' to keep the attributes.
},
{
// Transform accepted HTML tags into bracket tags.
search: /<\s*(\/?)\s*(?=(?:a|strong|em)\b)(\w+[^>]*)>/gi,
replace: '[$1$2]'
},
{
// Replace "<" by "&lt;".
search: /</g,
replace: '&lt;'
},
{
// Replace ">" by "&gt;".
search: />/g,
replace: '&gt;'
},
{
// Transform the accepted tags back to correct HTML.
search: /\[\s*(\/?)\s*((?:a|strong|em)\b)([^\]]*)\]/gi,
// For the replacement, we'll provide a callback function
// so that we can alter the attributes if needed.
replace: function (fullMatch, slash, tagName, attributes) {
// Convert the tag name to lowercase.
tagName = tagName.toLowerCase();
// If the slash group is empty then it's the opening tag.
if (slash === '') {
attributes = filterAttributes(attributes, tagName);
return '<' + tagName + attributes + '>';
}
// The closing tag.
else {
return '</' + tagName + '>';
}
}
},
{
// Optional: inject <br /> tags at each new lines to preserve user input.
search: /(\r?\n)/gm,
replace: '<br />$1'
}
];
let output = input;
steps.forEach((step, i) => {
output = output.replace(step.search, step.replace);
});
document.getElementById('input').innerText = input;
document.getElementById('output').innerText = output;
document.getElementById('rendered-output').innerHTML = output;
<!-- language: lang-css -->
body {
font-family: Georgia, "Times New Roman", serif;
margin: 0;
padding: 0 1em 1em 1em;
}
p:first-child { margin-top: 0; }
pre {
background: #f8f8f8;
border: 1px solid gray;
padding: .5em;
overflow-x: scroll;
}
.box {
background: white;
box-shadow: 0 0 .5em rgba(0, 0, 0, 0.1);
padding: 1em;
}
.invalid-url {
color: darkred;
}
<!-- language: lang-html -->
<h1>Quick &amp; dirty HTML filtering with regular expressions</h1>
<p>Input:</p>
<pre id="input"></pre>
<p>Output:</p>
<pre id="output"></pre>
<p>Rendered output:</p>
<div id="rendered-output" class="box"></div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论