tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

update-safe-default-configuration.py (4840B)


      1 # This Source Code Form is subject to the terms of the Mozilla Public
      2 # License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 # You can obtain one at http://mozilla.org/MPL/2.0/.
      4 
      5 import json
      6 from urllib.request import urlretrieve
      7 
      8 REVISION = "f0b66021355352cf1e38d08e3659d0a90757d7fd"
      9 
     10 urlretrieve(
     11    f"https://raw.githubusercontent.com/WICG/sanitizer-api/{REVISION}/builtins/safe-default-configuration.json",
     12    "safe-default-configuration.json",
     13 )
     14 config = json.load(open("safe-default-configuration.json"))
     15 
     16 # Make sure there are no unexpected keys (like removeAttributes)
     17 assert set(config.keys()) == {"elements", "attributes", "comments", "dataAttributes"}
     18 
     19 assert config["comments"] is False
     20 assert config["dataAttributes"] is False
     21 
     22 
     23 def attributes_list(attributes):
     24    result = []
     25 
     26    for attr in attributes:
     27        assert set(attr.keys()) == {"name", "namespace"}
     28        assert attr["namespace"] is None
     29 
     30        name = attr["name"].replace("-", "_")
     31        result.append("nsGkAtoms::" + name)
     32 
     33    # Should not have duplicate attributes
     34    assert len(result) == len(set(result))
     35 
     36    return result
     37 
     38 
     39 xhtml_elements = []
     40 mathml_elements = []
     41 svg_elements = []
     42 attributes = []
     43 
     44 xhtml_element_with_attributes = []
     45 mathml_element_with_attributes = []
     46 svg_element_with_attributes = []
     47 
     48 for element in config["elements"]:
     49    assert set(element.keys()) == {"name", "namespace", "attributes"}
     50 
     51    namespace = element["namespace"]
     52    atom = "nsGkAtoms::" + element["name"]
     53 
     54    element_attributes = []
     55    if element["attributes"]:
     56        assert len(element["attributes"])
     57        element_attributes.append("/* element */ " + atom)
     58        element_attributes.extend(attributes_list(element["attributes"]))
     59        element_attributes.append("nullptr")
     60 
     61    if namespace == "http://www.w3.org/1999/xhtml":
     62        xhtml_elements.append(atom)
     63        xhtml_element_with_attributes.extend(element_attributes)
     64    elif namespace == "http://www.w3.org/1998/Math/MathML":
     65        mathml_elements.append(atom)
     66        mathml_element_with_attributes.extend(element_attributes)
     67    elif namespace == "http://www.w3.org/2000/svg":
     68        svg_elements.append(atom)
     69        svg_element_with_attributes.extend(element_attributes)
     70    else:
     71        raise TypeError(f"unknown namespace: {namespace}")
     72 
     73 # Should not have duplicate elements
     74 assert len(set(xhtml_elements)) == len(xhtml_elements)
     75 assert len(set(mathml_elements)) == len(mathml_elements)
     76 assert len(set(svg_elements)) == len(svg_elements)
     77 
     78 
     79 def create_list_body(l):
     80    return "\n".join(map(lambda atom: f"    {atom},", l))
     81 
     82 
     83 xhtml_elements_body = create_list_body(xhtml_elements)
     84 mathml_elements_body = create_list_body(mathml_elements)
     85 svg_elements_body = create_list_body(svg_elements)
     86 
     87 attributes_body = create_list_body(attributes_list(config["attributes"]))
     88 
     89 xhtml_element_with_attributes_body = create_list_body(
     90    xhtml_element_with_attributes + ["/* sentinel */ nullptr"]
     91 )
     92 mathml_element_with_attributes_body = create_list_body(
     93    mathml_element_with_attributes + ["/* sentinel */ nullptr"]
     94 )
     95 svg_element_with_attributes_body = create_list_body(
     96    svg_element_with_attributes + ["/* sentinel */ nullptr"]
     97 )
     98 
     99 out = open("SanitizerDefaultConfig.h", "w")
    100 out.write(
    101    f"""/* This Source Code Form is subject to the terms of the Mozilla Public
    102 * License, v. 2.0. If a copy of the MPL was not distributed with this
    103 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
    104 
    105 #ifndef mozilla_dom_SanitizerDefaultConfig_h
    106 #define mozilla_dom_SanitizerDefaultConfig_h
    107 
    108 // This file is automatically generated by update-safe-default-configuration.py
    109 
    110 #include "mozilla/dom/SanitizerTypes.h"
    111 #include "nsGkAtoms.h"
    112 
    113 namespace mozilla::dom::sanitizer {{
    114 
    115 constexpr nsStaticAtom* kDefaultHTMLElements[] = {{
    116    // clang-format off
    117 {xhtml_elements_body}
    118    // clang-format on
    119 }};
    120 
    121 constexpr nsStaticAtom* kDefaultMathMLElements[] = {{
    122    // clang-format off
    123 {mathml_elements_body}
    124    // clang-format on
    125 }};
    126 
    127 constexpr nsStaticAtom* kDefaultSVGElements[] = {{
    128    // clang-format off
    129 {svg_elements_body}
    130    // clang-format on
    131 }};
    132 
    133 constexpr nsStaticAtom* kDefaultAttributes[] = {{
    134    // clang-format off
    135 {attributes_body}
    136    // clang-format on
    137 }};
    138 
    139 // Data is encoded as: element, attributes..., nullptr
    140 constexpr nsStaticAtom* kHTMLElementWithAttributes[] = {{
    141    // clang-format off
    142 {xhtml_element_with_attributes_body}
    143    // clang-format on
    144 }};
    145 
    146 constexpr nsStaticAtom* kMathMLElementWithAttributes[] = {{
    147    // clang-format off
    148 {mathml_element_with_attributes_body}
    149    // clang-format on
    150 }};
    151 
    152 constexpr nsStaticAtom* kSVGElementWithAttributes[] = {{
    153    // clang-format off
    154 {svg_element_with_attributes_body}
    155    // clang-format on
    156 }};
    157 
    158 }}  // namespace mozilla::dom::sanitizer
    159 
    160 #endif  // mozilla_dom_SanitizerDefaultConfig_h
    161 """
    162 )