1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
/*
* tree/attribute/safe.rs
*
* ftml - Library to parse Wikidot text
* Copyright (C) 2019-2021 Wikijump Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use regex::Regex;
use std::collections::HashSet;
use unicase::UniCase;
macro_rules! hashset_unicase {
() => {
hashset![]
};
($($x:expr),+ $(,)?) => {
hashset![
$(
UniCase::ascii($x)
),+
]
};
}
lazy_static! {
/// List of safe attributes. All others will be filtered out.
///
/// See https://scuttle.atlassian.net/wiki/spaces/WD/pages/1030782977/Allowed+Attributes+in+Wikitext
pub static ref SAFE_ATTRIBUTES: HashSet<UniCase<&'static str>> = {
hashset_unicase![
"accept",
"align",
"alt",
"autocapitalize",
"autoplay",
"background",
"bgcolor",
"border",
"buffered",
"checked",
"cite",
"class",
"cols",
"colspan",
"contenteditable",
"controls",
"coords",
"datetime",
"decoding",
"default",
"dir",
"dirname",
"disabled",
"download",
"draggable",
"for",
"form",
"headers",
"height",
"hidden",
"high",
"href",
"hreflang",
"id",
"inputmode",
"ismap",
"itemprop",
"kind",
"label",
"lang",
"list",
"loop",
"low",
"max",
"maxlength",
"min",
"minlength",
"multiple",
"muted",
"name",
"optimum",
"pattern",
"placeholder",
"poster",
"preload",
"readonly",
"required",
"reversed",
"role",
"rows",
"rowspan",
"scope",
"selected",
"shape",
"size",
"sizes",
"span",
"spellcheck",
"src",
"srclang",
"srcset",
"start",
"step",
"style",
"tabindex",
"target",
"title",
"translate",
"type",
"usemap",
"value",
"width",
"wrap",
]
};
/// List of all HTML5 attributes with special boolean behavior.
///
/// That is, you set them to "true" by having the attribute present without
/// a value, and "false" by excluding them.
///
/// A notable example is "checked" for `<input>`:
/// * `<input type="checkbox" checked>` means the checkbox starts checked
/// * `<input type="checkbox">` means it starts unchecked
///
/// This list includes all such attributes, even if they are not part of
/// `SAFE_ATTRIBUTES`.
pub static ref BOOLEAN_ATTRIBUTES: HashSet<UniCase<&'static str>> = {
hashset_unicase![
"allowfullscreen",
"allowpaymentrequest",
"async",
"autofocus",
"autoplay",
"checked",
"controls",
"default",
"disabled",
"formnovalidate",
"hidden",
"ismap",
"itemscope",
"loop",
"multiple",
"muted",
"nomodule",
"novalidate",
"open",
"playsinline",
"readonly",
"required",
"reversed",
"selected",
"truespeed",
]
};
static ref ATTRIBUTE_SUFFIX_SAFE: Regex = Regex::new(r"[a-zA-z0-9\-]+").unwrap();
}
pub const SAFE_ATTRIBUTE_PREFIXES: [&str; 2] = ["aria-", "data-"];
pub fn is_safe_attribute(attribute: UniCase<&str>) -> bool {
if SAFE_ATTRIBUTES.contains(&attribute) {
return true;
}
for prefix in &SAFE_ATTRIBUTE_PREFIXES {
if attribute.starts_with(prefix) && ATTRIBUTE_SUFFIX_SAFE.is_match(&attribute) {
return true;
}
}
false
}