安全扩展选项

这个 ui/safe 组件提供了一种过滤页面上数学的各种属性的方法,以便对其内容实施某些限制。这样可以防止 javascript:data: 从中显示的URL href 属性,例如,否则会导致潜在的安全问题。

由MathJax处理的所有数学都被转换成一个内部MathML结构,而不管它在页面中的初始格式如何。这个 ui/safe 扩展的工作原理是遍历内部MathML树,检查树中节点的属性,以确保它们符合指定的限制。

装入 ui/safe 扩展,添加 'ui/safe'load 数组 loader MathJax配置的块。

window.MathJax = {
  loader: {load: ['ui/safe']},
};

这个 ui/safe 扩展可以过滤多个信息类:url、类名、cssid和css样式声明。这些过滤器可分别设置为三个不同值之一: 'all''safe''none' .当设置为 'all' 不执行筛选(允许所有值);设置为时 'none' 该值始终处于清除状态(无法为该属性设置值);当设置为时 'safe' 使用选项中给出的附加条件过滤这些值,如下所示。


配置块

MathJax = {
  options: {
    safeOptions: {
      allow: {
        //
        //  Values can be "all", "safe", or "none"
        //
        URLs:    'safe',   // safe are in safeProtocols below
        classes: 'safe',   // safe start with mjx- (can be set by pattern below)
        cssIDs:  'safe',   // safe start with mjx- (can be set by pattern below)
        styles:  'safe'    // safe are in safeStyles below
      },
      //
      //  Which URL protocols are allowed
      //
      safeProtocols: {
        http: true,
        https: true,
        file: true,
        javascript: false,
        data: false
      },
      //
      //  Which styles are allowed
      //
      safeStyles: {
        color: true,
        backgroundColor: true,
        border: true,
        cursor: true,
        margin: true,
        padding: true,
        textShadow: true,
        fontFamily: true,
        fontSize: true,
        fontStyle: true,
        fontWeight: true,
        opacity: true,
        outline: true
      },
      lengthMax: 3,                           // Largest padding/border/margin, etc. in em's
      scriptsizemultiplierRange: [.6, 1],     // Valid range for scriptsizemultiplier
      scriptlevelRange: [-2, 2],              // Valid range for scriptlevel
      classPattern: /^mjx-[-a-zA-Z0-9_.]+$/,  // Pattern for allowed class names
      idPattern: /^mjx-[-a-zA-Z0-9_.]+$/,     // Pattern for allowed ids
      dataPattern: /^data-mjx-/               // Pattern for data attributes
    }
  }
};

选项说明

allow: {...}

这些设置控制为所提供的每个类别执行的筛选级别。设置为时 'all' 不执行筛选(允许所有值);设置为时 'none' 该值始终处于清除状态(无法为该属性设置值);当设置为时 'safe' 使用剩余选项中给出的附加条件过滤这些值。

safeProtocols: {...}

此对象控制允许在数学中的URL中使用哪些internet协议(在 hrefsrc 属性)。一种协议,其值为 true 将被允许,一个 false 不会的。例如,默认值是允许 http:https:file: 协议,但不是 javascript:data: 协议。未列出的协议被视为 false .

safeStyles: {...}

此对象指定允许在 style MathML节点的属性。设置为时 true 允许该样式(以及该样式的任何子样式);当 false 或未列出,则不允许指定样式。例如,因为 bordertrue , the style 属性可以包括 borderborder-topborder-top-width ,以此类推。某些样式值可能会根据其他配置选项进一步过滤。

lengthMax: 3

这将指定样式允许的最大尺寸,例如 paddingbordermargin 例如,这些限制是为了防止用户创建巨大的边界。这些属性的值的绝对值必须小于此值(在ems中)。

scriptsizemultiplierRange: [.6, 1]

它指定了 scriptsizemultiplier MathML属性(用于 <math><mstyle> 节点)。它们被过滤以防止用户将超级和下标设得太大(或太小)。

scriptlevelRange: [-2, 2]

这将指定为 scriptlevel MathML属性(用于 <math><mstyle> 节点)。这些被过滤以防止用户生成过大的文本(通过负片 scriptlevel )或太小(通过大 scriptlevel

classPattern: /^mjx-[-a-zA-Z0-9_.]+$/

这将提供一个正则表达式,用于确定是否允许指定类名。默认设置是允许名称以开头 mjx- 包含字母、数字、减号、句点和下划线。

idPattern: /^mjx-[-a-zA-Z0-9_.]+$/

这将提供一个正则表达式,用于确定哪个节点 id 允许指定值。默认情况下,允许ID以 mjx- 包含字母、数字、减号、句点和下划线。

dataPattern: /^data-mjx-/

这给出了一个正则表达式,用于确定 data- 允许指定属性名。默认值是允许 data- 名称以开头的属性 data-mjx- .


开发人员选项

MathJax = {
  options: {
    safeOptions: {
      //
      //  CSS styles that have Top/Right/Bottom/Left versions
      //
      styleParts: {
        border: true,
        padding: true,
        margin: true,
        outline: true
      },
      //
      //  CSS styles that are lengths needing max/min testing
      //    A string value means test that style value;
      //    An array gives [min,max] in em's
      //    Otherwise use [-lengthMax,lengthMax] from above
      //
      styleLengths: {
        borderTop: 'borderTopWidth',
        borderRight: 'borderRightWidth',
        borderBottom: 'borderBottomWidth',
        borderLeft: 'borderLeftWidth',
        paddingTop: true,
        paddingRight: true,
        paddingBottom: true,
        paddingLeft: true,
        marginTop: true,
        marginRight: true,
        marginBottom: true,
        marginLeft: true,
        outlineTop: true,
        outlineRight: true,
        outlineBottom: true,
        outlineLeft: true,
        fontSize: [.707, 1.44]
      }
    }
  }
};
styleParts: {...}

此对象指示哪些安全样式具有 Top/Right /``底部的`/``Left``版本(以便可以正确检查子部件)。如果你延长 safeStyles 要包括具有这四个子属性的其他属性,请务必在此处添加它们。

styleLengths: {...}

此对象列出了需要测试的长度的样式。字符串值意味着测试该样式的值(例如。, borderTop 设置为 'borderTopWidth' ,因此将测试边界的宽度)。数组值给出属性可以具有的最小值和最大值(以ems为单位),以及 true 手段使用 [-lengthMax, lengthMax] 使用 lengthMax 上述选项。