@barkleapp/css-sanitizer

中文文档
1.0.0发布于 2024/08/11MITCommonJS零依赖近 30 天下载 2.6万

A CSS sanitizer to prevent XSS attacks

原始使用文档
文档目录14 节
  1. Installation
  2. Usage
  3. Basic Usage
  4. Custom Configuration
  5. API
  6. CssSanitizer
  7. Configuration Options
  8. maxCssLength (default: 65536)
  9. allowedProperties (default: see code)
  10. allowedAtRules (default: ['@media', '@keyframes', '@font-fa…
  11. allowedPseudoClasses (default: [':hover', ':active', ':focu…
  12. validateUrl (function)
  13. sanitizeUrl (function)
  14. License

CSS Sanitizer

A lightweight, configurable CSS sanitizer to prevent XSS attacks by filtering out potentially harmful CSS properties and values.

Installation

npm install @barkleapp/css-sanitizer

Usage

Basic Usage

import { CssSanitizer } from 'css-sanitizer';

const sanitizer = new CssSanitizer();

const unsanitizedCss = `
  body {
    background: url('https://example.com/image.jpg');
    color: red;
  }
`;

const sanitizedCss = sanitizer.sanitizeCss(unsanitizedCss);
console.log(sanitizedCss);

Custom Configuration

You can customize the sanitizer's behavior by passing a configuration object:

const customSanitizer = new CssSanitizer({
  maxCssLength: 100000,
  allowedProperties: ['custom-property'],
  allowedAtRules: ['@custom-media'],
  allowedPseudoClasses: [':has'],
  validateUrl: (url) => {
    // Custom URL validation logic
    return url.startsWith('https://');
  },
  sanitizeUrl: (url) => {
    // Custom URL sanitization logic
    const allowedDomains = ['example.com', 'trusteddomain.com'];
    const parsedUrl = new URL(url);
    if (allowedDomains.includes(parsedUrl.hostname)) {
      return url;
    }
    return '';
  }
});

API

CssSanitizer

The main class for sanitizing CSS.

Constructor

new CssSanitizer(config)
  • config (optional): An object with the following properties:
    • maxCssLength (number): Maximum allowed length of CSS string.
    • allowedProperties (Array): Additional CSS properties to allow.
    • allowedAtRules (Array): Additional at-rules to allow.
    • allowedPseudoClasses (Array): Additional pseudo-classes to allow.
    • validateUrl (function): Custom function to validate URLs.
    • sanitizeUrl (function): Custom function to sanitize URLs.

Methods

sanitizeCss(css: string): string

Sanitizes the input CSS string by removing potentially harmful properties and values.

  • css: The CSS string to sanitize.
  • Returns: The sanitized CSS string.

Configuration Options

maxCssLength (default: 65536)

Maximum allowed length of the CSS string. If the input CSS exceeds this length, it will be truncated.

allowedProperties (default: see code)

A set of allowed CSS properties. You can add to this list by providing an array of additional properties in the constructor.

allowedAtRules (default: ['@media', '@keyframes', '@font-face', '@import'])

A set of allowed at-rules. You can add to this list by providing an array of additional at-rules in the constructor.

allowedPseudoClasses (default: [':hover', ':active', ':focus', ':visited', ':first-child', ':last-child', ':nth-child', ':nth-of-type', ':not', ':before', ':after'])

A set of allowed pseudo-classes. You can add to this list by providing an array of additional pseudo-classes in the constructor.

validateUrl (function)

A function that takes a URL string and returns a boolean indicating whether the URL is valid. By default, it checks if the string can be parsed as a valid URL.

sanitizeUrl (function)

A function that takes a URL string and returns either the sanitized URL string or an empty string if the URL is not allowed. By default, it allows URL from 'fonts.googleapis.com'.

License

MIT

版本与发布记录

@barkleapp/css-sanitizer 共发布 1 个版本,首次发布于 2024/08/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2024/08/112 年前

关于 @barkleapp/css-sanitizer 的常见问题

@barkleapp/css-sanitizer 是什么?

A CSS sanitizer to prevent XSS attacks

如何安装 @barkleapp/css-sanitizer?

在项目目录运行 npm install @barkleapp/css-sanitizer,或使用 pnpm add / yarn add / bun add。

@barkleapp/css-sanitizer 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/barkleapp__css-sanitizer 是否存在。

@barkleapp/css-sanitizer 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@barkleapp/css-sanitizer 可以商用吗?

@barkleapp/css-sanitizer 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@barkleapp/css-sanitizer 的下载量有多少?

根据 npm 官方下载统计,@barkleapp/css-sanitizer 在 2026/10/08 – 2026/11/06 这 30 天内被下载 25,861 次,日均约 862 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@barkleapp/css-sanitizer 还在维护吗?

最新版本 1.0.0 发布于 2024/08/11(2 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2024/08/11。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。