-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathvalidate-no-cdn-refs.mts
More file actions
232 lines (204 loc) · 5.46 KB
/
validate-no-cdn-refs.mts
File metadata and controls
232 lines (204 loc) · 5.46 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* @file Validates that there are no CDN references in the codebase. This is a
* preventative check to ensure no hardcoded CDN URLs are introduced. The
* project deliberately avoids CDN dependencies for security and reliability.
* Blocked CDN domains:
*
* - unpkg.com
* - cdn.jsdelivr.net
* - esm.sh
* - cdn.skypack.dev
* - ga.jspm.io
*/
import { promises as fs } from 'node:fs'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { getDefaultLogger } from '@socketsecurity/lib-stable/logger/default'
const logger = getDefaultLogger()
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const rootPath = path.join(__dirname, '..')
// CDN domains to block
const CDN_PATTERNS = [
/unpkg\.com/i,
/cdn\.jsdelivr\.net/i,
/esm\.sh/i,
/cdn\.skypack\.dev/i,
/ga\.jspm\.io/i,
]
// Directories to skip
const SKIP_DIRS = new Set([
'.cache',
'.git',
'.next',
'.nuxt',
'.output',
'.turbo',
'.type-coverage',
'.yarn',
'build',
'coverage',
'dist',
'node_modules',
])
// File extensions to check
const TEXT_EXTENSIONS = new Set([
'.bash',
'.cjs',
'.css',
'.cts',
'.htm',
'.html',
'.js',
'.json',
'.jsx',
'.md',
'.mjs',
'.mts',
'.sh',
'.svg',
'.ts',
'.tsx',
'.txt',
'.xml',
'.yaml',
'.yml',
])
interface CdnViolation {
file: string
line: number
content: string
cdnDomain: string
}
/**
* Check file contents for CDN references.
*/
async function checkFileForCdnRefs(
filePath: string,
): Promise<CdnViolation[]> {
// Skip this validator script itself (it mentions CDN domains by necessity)
if (filePath.endsWith('validate-no-cdn-refs.mts')) {
return []
}
try {
const content = await fs.readFile(filePath, 'utf8')
const lines = content.split('\n')
const violations: CdnViolation[] = []
for (let i = 0; i < lines.length; i++) {
const line = lines[i]
const lineNumber = i + 1
for (let i = 0, { length } = CDN_PATTERNS; i < length; i += 1) {
const pattern = CDN_PATTERNS[i]
if (pattern.test(line)) {
const match = line.match(pattern)
violations.push({
file: path.relative(rootPath, filePath),
line: lineNumber,
content: line.trim(),
cdnDomain: match[0],
})
}
}
}
return violations
} catch (e) {
// Skip files we can't read (likely binary despite extension)
const error = e as NodeJS.ErrnoException
if (error.code === 'EISDIR' || error.message?.includes('ENOENT')) {
return []
}
// For other errors, try to continue
return []
}
}
/**
* Recursively find all text files to scan.
*/
async function findTextFiles(
dir: string,
files: string[] = [],
): Promise<string[]> {
try {
const entries = await fs.readdir(dir, { withFileTypes: true })
for (let i = 0, { length } = entries; i < length; i += 1) {
const entry = entries[i]
const fullPath = path.join(dir, entry.name)
if (entry.isDirectory()) {
// Skip certain directories and hidden directories (except .github)
if (
!SKIP_DIRS.has(entry.name) &&
(!entry.name.startsWith('.') || entry.name === '.github')
) {
await findTextFiles(fullPath, files)
}
} else if (entry.isFile() && shouldScanFile(entry.name)) {
files.push(fullPath)
}
}
} catch {
// Skip directories we can't read
}
return files
}
/**
* Check if file should be scanned.
*/
function shouldScanFile(filename: string): boolean {
const ext = path.extname(filename).toLowerCase()
return TEXT_EXTENSIONS.has(ext)
}
/**
* Validate all files for CDN references.
*/
async function validateNoCdnRefs(): Promise<CdnViolation[]> {
const files = await findTextFiles(rootPath)
const allViolations = []
for (let i = 0, { length } = files; i < length; i += 1) {
const file = files[i]
const violations = await checkFileForCdnRefs(file)
allViolations.push(...violations)
}
return allViolations
}
async function main(): Promise<void> {
try {
const violations = await validateNoCdnRefs()
if (violations.length === 0) {
logger.success('No CDN references found')
process.exitCode = 0
return
}
logger.fail(`Found ${violations.length} CDN reference(s)`)
logger.log('')
logger.log('CDN URLs are not allowed in this codebase for security and')
logger.log('reliability reasons. Please use npm packages instead.')
logger.log('')
logger.log('Blocked CDN domains:')
logger.log(' - unpkg.com')
logger.log(' - cdn.jsdelivr.net')
logger.log(' - esm.sh')
logger.log(' - cdn.skypack.dev')
logger.log(' - ga.jspm.io')
logger.log('')
logger.log('Violations:')
logger.log('')
for (let i = 0, { length } = violations; i < length; i += 1) {
const violation = violations[i]
logger.log(` ${violation.file}:${violation.line}`)
logger.log(` Domain: ${violation.cdnDomain}`)
logger.log(` Content: ${violation.content}`)
logger.log('')
}
logger.log('Remove CDN references and use npm dependencies instead.')
logger.log('')
process.exitCode = 1
} catch (e) {
const message = e instanceof Error ? e.message : String(e)
logger.fail(`Validation failed: ${message}`)
process.exitCode = 1
}
}
main().catch((e: unknown) => {
const message = e instanceof Error ? e.message : String(e)
logger.fail(`Unexpected error: ${message}`)
process.exitCode = 1
})