npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@midwayjs/security

v3.18.0

Published

Midway Security Component

Downloads

830

Readme

Security 安全组件

适用于 @midwayjs/faas@midwayjs/web@midwayjs/koa@midwayjs/express 多种框架的通用安全组件,支持 csrfxss 等多种安全策略。

安装使用

  1. 安装依赖
tnpm i @midwayjs/security --save
  1. 在 configuration 中引入组件,
import * as Security from '@midwayjs/security';
@Configuration({
  imports: [
    // ...other components
    Security
  ],
})
export class AutoConfiguration {}

配置

// 默认配置
export const security = {
  csrf: {
    enable: true,
    type: 'ctoken',
    useSession: false,
    cookieName: 'csrfToken',
    sessionName: 'csrfToken',
    headerName: 'x-csrf-token',
    bodyName: '_csrf',
    queryName: '_csrf',
    refererWhiteList: [],
  },
  xframe: {
    enable: true,
    value: 'SAMEORIGIN',
  },
  csp: {
    enable: false,
  },
  hsts: {
    enable: false,
    maxAge: 365 * 24 * 3600,
    includeSubdomains: false,
  },
  noopen: {
    enable: false,
  },
  nosniff: {
    enable: false,
  },
  xssProtection: {
    enable: true,
    value: '1; mode=block',
  },
}

csrf

| 配置项 | 类型 | 作用描述 | 默认值 | | --- | --- | --- | --- | | enable | boolean | 是否开启 | true | | type | 'all' / 'any' / 'ctoken' / 'referer' | csrf 校验类型,all/any 等于 ctoken + referer | 'ctoken' 从query/header/body 中获取 csrf token | | useSession | boolean | csrf token 是否存放在 session 中 | false,默认存放在 cookies 中 | | cookieName | string | token 在 cookie 中存放的 字段 | 'csrfToken' | | sessionName | string | token 在 session 中存放的 字段 | 'csrfToken' | | headerName | string | token 在 header 中存放的 字段 | 'x-csrf-token' | | bodyName | string | token 在 body 中存放的 字段 | '_csrf' | | queryName | string | token 在 query 中存放的 字段 | '_csrf' | | refererWhiteList | Array | 允许的来源白名单 | [] |

xframe

xframe 用来配置 X-Frame-Options 响应头,用来给浏览器指示允许一个页面可否在 frame, iframe, embed 或者 object 中展现的标记。站点可以通过确保网站没有被嵌入到别人的站点里面,从而避免 clickjacking 攻击。

X-Frame-Options 有三个可能的值:

  • X-Frame-Options: deny:页面不允许在 frame 中展示
  • X-Frame-Options: sameorigin:该页面可以在相同域名页面的 frame 中展示
  • X-Frame-Options: allow-from https://example.com/:该页面可以在指定来源的 frame 中展示

| 配置项 | 类型 | 作用描述 | 默认值 | | --- | --- | --- | --- | | enable | boolean | 是否开启 | true | | value | string | X-Frame-Options 值 | 'SAMEORIGIN' |

hsts

HTTP Strict Transport Security(通常简称为 HSTS )是一个安全功能,它告诉浏览器只能通过 HTTPS 访问当前资源,而不是 HTTP

| 配置项 | 类型 | 作用描述 | 默认值 | | --- | --- | --- | --- | | enable | boolean | 是否开启 | false | | maxAge | number | 在浏览器收到这个请求后的多少 时间内凡是访问这个域名下的请求都使用HTTPS请求 | 365 * 24 * 3600 即一年 | | includeSubdomains | boolean | 此规则是否适用于该网站的所有子域名 | false |

csp

HTTP 响应头 Content-Security-Policy 允许站点管理者控制指定的页面加载哪些资源。这将帮助防止跨站脚本攻击(XSS)。

| 配置项 | 类型 | 作用描述 | 默认值 | | --- | --- | --- | --- | | enable | boolean | 是否开启 | false | | policy | Object<key: string, value: string / string[] / boolean> | 策略列表 | {} | | reportOnly | boolean | 是否开启 | false | | supportIE | boolean | 是否支持IE浏览器 | false |

详细的 policy 配置可以参考: Content Security Policy (CSP) 是什么?阿里聚安全

noopen

用于指定 IE 8 以上版本的用户不打开文件而直接保存文件。在下载对话框中不显示“打开”选项。

| 配置项 | 类型 | 作用描述 | 默认值 | | --- | --- | --- | --- | | enable | boolean | 是否开启 | false |

nosniff

开启后,如果从 scriptstylesheet 读入的文件的 MIME 类型与指定 MIME 类型不匹配,不允许读取该文件。用于防止 XSS 等跨站脚本攻击。

| 配置项 | 类型 | 作用描述 | 默认值 | | --- | --- | --- | --- | | enable | boolean | 是否开启 | false |

xssProtection

用于启用浏览器的XSS过滤功能,以防止 XSS 跨站脚本攻击。

X-XSS-Protection 响应头是 IEChromeSafari 的一个特性,当检测到跨站脚本攻击 (XSS (en-US))时,浏览器将停止加载页面。若网站设置了良好的 Content-Security-Policy 来禁用内联 JavaScript ('unsafe-inline'),现代浏览器不太需要这些保护, 但其仍然可以为尚不支持 CSP 的旧版浏览器的用户提供保护。

X-XSS-Protection 可以配置下述四个值

  • 0: 禁止XSS过滤。
  • 1:启用XSS过滤(通常浏览器是默认的)。 如果检测到跨站脚本攻击,浏览器将清除页面(删除不安全的部分)。
  • 1;mode=block:启用XSS过滤。 如果检测到攻击,浏览器将不会清除页面,而是阻止页面加载。
  • 1; report=<reporting-URI>: Chromium only,启用XSS过滤。 如果检测到跨站脚本攻击,浏览器将清除页面并使用CSP report-uri (en-US)指令的功能发送违规报告。

| 配置项 | 类型 | 作用描述 | 默认值 | | --- | --- | --- | --- | | enable | boolean | 是否开启 | false | | value | string | X-XSS-Protection 配置 | 1; mode=block |