关于在CICD中的CSP的自动化设置script的hash

关于在CICD中的CSP的自动化设置script的hash

.drone.yml

kind: pipeline
type: docker
name: build

trigger:
  ref:
    - refs/tags/**

steps:
  - name: build
    image: node:14.17.0
    commands:
      - npm i
      - npm run build

  - name: drone-scp
    image: appleboy/drone-scp
    settings:
      host:
        from_secret: host
      username:
        from_secret: username
      password:
        from_secret: password
      port: 22
      target: /custom_front
      source:
        - build
      rm: true

  - name: script-hash
    image: appleboy/drone-ssh
    settings:
      host:
        from_secret: host
      username:
        from_secret: username
      password:
        from_secret: password
      port: 22
      script:
        - cd /get-front-hash
        - node index.js
        - docker restart nginx

get-front-hash

创建get-front-hash项目去计算html中的script的hash值

index.js

const jsdom = require('jsdom');
const { JSDOM }  = jsdom;


const { exec } = require("child_process");

const Hashes = require('jshashes')
const sha256 = new Hashes.SHA256

const fs = require('fs')

const getHash = () => {
    fs.readFile('/custom_front/build/index.html','utf-8', async (err,data)=>{
        const dom = new JSDOM(data);
        const script = dom.window.document.querySelectorAll('script')

        const script_content = []
        script.forEach(item=>{
            if(item.textContent){
                script_content.push(item.textContent)
            }
        })

        let script_sha256_string = ``

        script_content.forEach(item=>{
            script_sha256_string = script_sha256_string + ` 'sha256-${sha256.b64(item)}'`
        })

        const resetCOntent = () => {
            return new Promise((resolve, reject)=>{
                fs.writeFile('/nginx/conf.d/default.conf','',()=>{
                    resolve()
                })
            })
        }

        const setNginxConfCOntent = () => {
            return new Promise((resolve, reject)=>{
                const nginx_conf = `server {
    listen       80;
    server_name  localhost;
    root   /usr/share/nginx/build;
    
    add_header Content-Security-Policy "default-src 'self';script-src 'self' ${script_sha256_string} ; object-src 'none';form-action 'none'";
    add_header Vary Accept-Encoding;
    add_header X-Frame-Options deny;

    add_header     X-Content-Type-Options "nosniff";
    add_header     X-XSS-Protection "1; mode=block";
    add_header     Referrer-Policy "same-origin";

    location ~ /\\.(?!well-known) {
        deny all;
    }
}`
                fs.writeFile('/nginx/conf.d/default.conf',nginx_conf,()=>{
                    resolve()
                })
            })
        }

        await resetCOntent()
        await setNginxConfCOntent()
    })
}

getHash()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值