blob: f3e5057e6f588bcebaa40132e47ac8efb5617459 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
package main
// Path netlify.toml file for Netlify Deploy Preview to allow some
// violation for CSP header.
import (
"fmt"
"io/ioutil"
"log"
"regexp"
"strings"
)
const netlifyConfig = "netlify.toml"
func main() {
input, err := ioutil.ReadFile(netlifyConfig)
if err != nil {
log.Fatalln(err)
}
lines := strings.Split(string(input), "\n")
for i, line := range lines {
newStr := line
// -> default-src 'self';
// <- default-src 'self' blob:;
reStr := regexp.MustCompile("(default-src) ('self')(;)")
repStr := "${1} ${2} blob:${3}"
newStr = reStr.ReplaceAllString(newStr, repStr)
// -> style-src 'self' cdn.hypothes.is;
// <- style-src 'self' 'unsafe-inline' cdn.hypothes.is;
reStr = regexp.MustCompile(`(style-src) ('self') (cdn\.hypothes\.is)(;)`)
repStr = "${1} ${2} ${3} 'unsafe-inline'${4}"
newStr = reStr.ReplaceAllString(newStr, repStr)
// -> media-src 'self';
// <- media-src 'self' blob: https://app.netlify.com;
reStr = regexp.MustCompile("(media-src) ('self')(;)")
repStr = "${1} ${2} blob: https://app.netlify.com${3}"
newStr = reStr.ReplaceAllString(newStr, repStr)
// -> frame-src 'none';
// <- frame-src app.netlify.com;
reStr = regexp.MustCompile("(frame-src) ('none')(;)")
repStr = "${1} app.netlify.com${3}"
newStr = reStr.ReplaceAllString(newStr, repStr)
// -> script-src 'self' www.googletagmanager.com hypothes.is cdn.hypothes.is;
// <- script-src 'self' www.googletagmanager.com hypothes.is cdn.hypothes.is netlify-cdp-loader.netlify.app;
reStr = regexp.MustCompile(`(script-src) ('self' www\.googletagmanager\.com hypothes\.is cdn\.hypothes\.is)(;)`)
repStr = "${1} ${2} netlify-cdp-loader.netlify.app${3}"
newStr = reStr.ReplaceAllString(newStr, repStr)
lines[i] = newStr
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(netlifyConfig, []byte(output), 0644)
if err != nil {
log.Fatalln(err)
}
fmt.Println("Done")
}
|