Skip to content

Commit 89614fe

Browse files
koops76Hainish
authored andcommitted
Add normalize-securecookie.js (EFForg#12302)
1 parent 140d18c commit 89614fe

1 file changed

Lines changed: 123 additions & 0 deletions

File tree

‎utils/normalize-securecookie.js‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
'use strict'
2+
3+
const crypto = require('crypto')
4+
5+
const fs = require('fs')
6+
7+
const path = require('path')
8+
9+
const util = require('util')
10+
11+
const readFile = util.promisify(fs.readFile)
12+
13+
const writeFile = util.promisify(fs.writeFile)
14+
15+
const readdir = util.promisify(fs.readdir)
16+
17+
const regexes = [
18+
{
19+
from: /<securecookie\s+(host|name)=\s*["']\^?\.[+*]?\$?["']\s+(host|name)=\s*["']\^?\.[+*]?\$?["']\s*\/>/g,
20+
to: '<securecookie host=".+" name=".+" />'
21+
},
22+
{
23+
from: /<securecookie\s+host=\s*["'](.+)["']\s+name=\s*["']\^?\.[+*]?\$?["']\s*\/>/g,
24+
to: '<securecookie host="$1" name=".+" />'
25+
},
26+
{
27+
from: /<securecookie\s+name=\s*["']\^?\.[+*]?\$?["']\s+host=\s*["'](.+)["']\s*\/>/g,
28+
to: '<securecookie host="$1" name=".+" />'
29+
},
30+
{
31+
from: /<securecookie\s+host=\s*["']\^?\.[+*]?\$?["']\s+name=\s*["'](.+)["']\s*\/>/g,
32+
to: '<securecookie host=".+" name="$1" />'
33+
},
34+
{
35+
from: /<securecookie\s+name=\s*["'](.+)["']\s+host=\s*["']\^?\.[+*]?\$?["']\s*\/>/g,
36+
to: '<securecookie host=".+" name="$1" />'
37+
}
38+
]
39+
40+
function parseWhitelist (data) {
41+
const lines = data.split('\n').slice(1)
42+
const result = {}
43+
44+
for (const line of lines) {
45+
const lineSplit = line.split(',')
46+
47+
if (lineSplit.length !== 4) continue
48+
49+
const key = lineSplit[3].toLowerCase()
50+
51+
const value = lineSplit[0]
52+
53+
result[key] = value
54+
}
55+
56+
return result
57+
}
58+
59+
const rulesDir = path.resolve(__dirname, '../src/chrome/content/rules')
60+
const rulesetWhitelistFile = path.join(__dirname, 'ruleset-whitelist.csv')
61+
62+
async function main () {
63+
const rulesetWhitelistContents = await readFile(rulesetWhitelistFile, 'utf8')
64+
let rulesetWhitelistNewContents = rulesetWhitelistContents
65+
66+
const rulesetWhitelist = parseWhitelist(rulesetWhitelistContents)
67+
68+
const files = (await readdir(rulesDir)).filter(name => name.endsWith('.xml')).sort()
69+
let changedCount = 0
70+
71+
for (const fileName of files) {
72+
const fileNameLowerCase = fileName.toLowerCase()
73+
74+
const file = path.join(rulesDir, fileName)
75+
76+
const originalContents = await readFile(file, 'utf8')
77+
78+
let fixHash = false
79+
let originalHash
80+
81+
if (fileNameLowerCase in rulesetWhitelist) {
82+
originalHash = crypto.createHash('sha256').update(originalContents).digest('hex')
83+
84+
if (rulesetWhitelist[fileNameLowerCase] === originalHash) {
85+
fixHash = true
86+
console.log(`"${fileName}" is in the whitelist and the hash matches. Fixing the hash if the file will be modified.`)
87+
} else {
88+
console.log(`"${fileName}" is in the whitelist and the hash DOESN'T MATCH!!! Actual hash is "${originalHash}". Not fixing the hash if the file will be modified.`)
89+
}
90+
}
91+
92+
let newContents = originalContents
93+
94+
for (const regex of regexes) {
95+
newContents = newContents.replace(regex.from, regex.to)
96+
}
97+
98+
if (newContents !== originalContents) {
99+
if (fixHash) {
100+
const newHash = crypto.createHash('sha256').update(newContents).digest('hex')
101+
rulesetWhitelistNewContents = rulesetWhitelistNewContents.replace(originalHash, newHash)
102+
103+
console.log(`"${fileName}" has been modified, its new hash is "${newHash}".`)
104+
} else {
105+
console.log(`"${fileName}" has been modified.`)
106+
}
107+
108+
await writeFile(file, newContents, 'utf8')
109+
110+
changedCount++
111+
}
112+
}
113+
114+
console.log(`${changedCount} of ${files.length} files have changed (${changedCount / files.length * 100}%).`)
115+
116+
if (rulesetWhitelistNewContents !== rulesetWhitelistContents) {
117+
await writeFile(rulesetWhitelistFile, rulesetWhitelistNewContents, 'utf8')
118+
119+
console.log('Ruleset whitelist has been updated.')
120+
}
121+
}
122+
123+
main()

0 commit comments

Comments
 (0)