forked from dub-flow/veracode-javascript-packager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
442 lines (353 loc) · 10.4 KB
/
main.go
File metadata and controls
442 lines (353 loc) · 10.4 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package main
import (
"archive/zip"
"flag"
"io"
"os"
"path"
"path/filepath"
"strings"
log "github.com/sirupsen/logrus"
)
// flag to make sure a message is only logged once
var didPrintNodeModulesMsg bool = false
var didPrintTestsMsg bool = false
var didPrintDefaultTestExtensionsMsg bool = false
var didPrintDefaultTestFoldersMsg bool = false
var didPrintStylesheetsMsg bool = false
var didPrintImagesMsg bool = false
var didPrintDocumentsMsg bool = false
var didPrintFontsMsg bool = false
var didPrintIdesMsg bool = false
var didPrintBuildMsg bool = false
var didPrintDbsMsg bool = false
var didPrintGitFolderMsg bool = false
func main() {
// parse all the command line flags
sourcePtr := flag.String("source", "sample-node-project", "The path of the Node.js app you want to package")
targetPtr := flag.String("target", ".", "The path where you want the vc-output.zip to be stored to")
testsPtr := flag.String("tests", "test", "The path that contains your Node.js test files (relative to the source)")
flag.Parse()
outputZipPath := filepath.Join(*targetPtr, "vc-output.zip")
// combine that last segment of the `sourcePtr`` with the value provided via `-test`.
// Example: If `-test mytests` and `-source /some/node-project`, then `testsPath` will be: "node-project/mytests"
testsPath := filepath.Join(path.Base(*sourcePtr), *testsPtr)
log.Info("##########################################")
log.Info("# #")
log.Info("# Veracode Node Packager (Unofficial) #")
log.Info("# #")
log.Info("##########################################" + "\n\n")
// check for some "smells" (e.g. the `package-lock.json` file is missing), and print corresponding warnings/errors
log.Info("Checking for 'smells' that indicate packaging issues - Started...")
checkForPotentialSmells(*sourcePtr)
log.Info("'Smells' Check - Done")
log.Info("Creating a Zip while omitting non-required files - Started...")
log.Info("Source directory to zip up: ", *sourcePtr)
log.Info("Test directory (its content will be omitted): ", testsPath)
// generate the zip file, and omit all non-required files
if err := zipSource(*sourcePtr, outputZipPath, testsPath); err != nil {
log.Error(err)
}
log.Info("Zip Process - Done")
log.Info("Wrote archive to: ", outputZipPath)
log.Info("Please upload this archive to the Veracode Platform")
}
func checkForPotentialSmells(source string) {
doesPackageLockJsonExist := false
doesMapFileExist := false
doesPublicExist := false
doesDistExist := false
err := filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
// check for the `package-lock.json`
if strings.HasSuffix(path, "package-lock.json") && !strings.Contains(path, "node_modules") {
doesPackageLockJsonExist = true
}
// check for `.map` files
if strings.HasSuffix(path, ".map") && !strings.Contains(path, "node_modules") {
doesMapFileExist = true
}
if info.IsDir() {
// check for `/public` directory
if strings.HasSuffix(path, string(os.PathSeparator)+"public") && !strings.Contains(path, "node_modules") {
doesPublicExist = true
}
// check for `/dist` directory
if strings.HasSuffix(path, string(os.PathSeparator)+"dist") && !strings.Contains(path, "node_modules") {
doesDistExist = true
}
}
return nil
})
if err != nil {
log.Error(err)
}
if !doesPackageLockJsonExist {
log.Error("No `package-lock.json` file found.. (This file is required for Veracode SCA)")
log.Error("You may not receive Veracode SCA results")
}
if doesMapFileExist {
log.Error("The 1st party code contains `.map` files (which indicates minified JavaScript)...")
log.Error("Please pass a directory to this tool that contains the unminified/unbundled/unconcatenated JavaScript (or TypeScript)")
}
if doesPublicExist {
log.Warn("The `/public` folder exists.. This folder can likely be omitted")
log.Warn("Please verify if the `public` folder contains any actual source code and, if not, please omit it before calling this tool here")
}
if doesDistExist {
log.Warn("The `/dist` folder exists.. This folder can likely be omitted")
log.Warn("Please verify if the `dist` folder contains any actual source code and, if not, please omit it before calling this tool here")
}
}
func zipSource(source string, target string, testsPath string) error {
// 1. Create a ZIP file and zip.Writer
f, err := os.Create(target)
if err != nil {
return err
}
defer f.Close()
writer := zip.NewWriter(f)
defer writer.Close()
// 2. Go through all the files of the source
return filepath.Walk(source, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
// 3. Create a local file header
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
// set compression
header.Method = zip.Deflate
// 4. Set relative path of a file as the header name
header.Name, err = filepath.Rel(filepath.Dir(source), path)
if err != nil {
return err
}
// check if the path is required for the upload (otherwise, it will be omitted)
if !isRequired(header.Name, testsPath) {
return nil
}
if info.IsDir() {
header.Name += "/"
}
// 5. Create writer for the file header and save content of the file
headerWriter, err := writer.CreateHeader(header)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(headerWriter, f)
return err
})
}
func isRequired(path string, testsPath string) bool {
// check for the `node_modules` folder
if isNodeModules(path) {
return false
}
// check if it is a `test` path (i.e., a file that e.g. contains unit tests)
if isTestFile(path, testsPath) {
return false
}
// check for common `test`
if isCommonTest(path) {
return false
}
// check for style sheets (.css and .scss)
if isStyleSheet(path) {
return false
}
// check for images (like .jpg, .png, .jpeg)
if isImage(path) {
return false
}
// check for documents (like .pdf, .md)
if isDocument(path) {
return false
}
// check for fonts (like .woff)
if isFont(path) {
return false
}
// check for the `.git` folder
if isGitFolder(path) {
return false
}
// check for the dbs (like .db, .sqlite3)
if isDb(path) {
return false
}
// check for the `build` folder
if isBuildFolder(path) {
return false
}
// check for IDE folder (like .code, .idea)
if isIdeFolder(path) {
return false
}
// check for the "misc" not required stuff
if isMiscNotRequiredFile(path) {
return false
}
// the default is to not omit the file
return true
}
func isNodeModules(path string) bool {
if strings.Contains(path, "node_modules") {
if !didPrintNodeModulesMsg {
log.Info("Ignoring the entire `node_modules` folder")
didPrintNodeModulesMsg = true
}
return true
}
return false
}
func isTestFile(path string, testsPath string) bool {
if strings.Contains(path, testsPath) {
if !didPrintTestsMsg {
log.Info("Ignoring the entire content of the `" + testsPath + "` folder (contains test files)")
didPrintTestsMsg = true
}
return true
}
return false
}
func isCommonTest(path string) bool {
testExtensions := [2]string{".spec.ts", ".test.tsx"}
for _, element := range testExtensions {
if strings.HasSuffix(path, element) {
if !didPrintDefaultTestExtensionsMsg {
log.Info("Ignoring common test extensions (such as `.spec.ts`)")
didPrintDefaultTestExtensionsMsg = true
}
return true
}
}
testPaths := [3]string{"test", "e2e", "__tests__"}
for _, element := range testPaths {
if strings.Contains(path, element) {
if !didPrintDefaultTestFoldersMsg {
log.Info("Ignoring common test folders (such as `e2e`)")
didPrintDefaultTestFoldersMsg = true
}
return true
}
}
return false
}
func isStyleSheet(path string) bool {
if strings.HasSuffix(path, ".css") || strings.HasSuffix(path, ".scss") {
if !didPrintStylesheetsMsg {
log.Info("Ignoring style sheets (such as `.css`)")
didPrintStylesheetsMsg = true
}
return true
}
return false
}
func isImage(path string) bool {
imageExtensions := [8]string{".jpg", ".png", ".jpeg", ".gif", ".svg", ".bmp", ".ico", ".icns"}
for _, element := range imageExtensions {
if strings.HasSuffix(path, element) {
if !didPrintImagesMsg {
log.Info("Ignoring images (such as `.jpg`)")
didPrintImagesMsg = true
}
return true
}
}
return false
}
func isDocument(path string) bool {
documentExtensions := [2]string{".pdf", ".md"}
for _, element := range documentExtensions {
if strings.HasSuffix(path, element) {
if !didPrintDocumentsMsg {
log.Info("Ignoring documents (such as `.pdf`)")
didPrintDocumentsMsg = true
}
return true
}
}
return false
}
func isFont(path string) bool {
fontExtensions := [4]string{".ttf", ".otf", ".woff", ".woff2"}
for _, element := range fontExtensions {
if strings.HasSuffix(path, element) {
if !didPrintFontsMsg {
log.Info("Ignoring fonts (such as `.woff`)")
didPrintFontsMsg = true
}
return true
}
}
return false
}
func isGitFolder(path string) bool {
if strings.Contains(path, ".git") {
if !didPrintGitFolderMsg {
log.Info("Ignoring `.git`")
didPrintGitFolderMsg = true
}
return true
}
return false
}
func isDb(path string) bool {
documentExtensions := [6]string{".db", ".db3", ".sdb", ".sqlite", ".sqlite2", ".sqlite3"}
for _, element := range documentExtensions {
if strings.HasSuffix(path, element) {
if !didPrintDbsMsg {
log.Info("Ignoring dbs (such as `.sqlite3`)")
didPrintDbsMsg = true
}
return true
}
}
return false
}
func isBuildFolder(path string) bool {
if strings.Contains(path, "build") {
if !didPrintBuildMsg {
log.Info("Ignoring `build` folder")
didPrintBuildMsg = true
}
return true
}
return false
}
func isIdeFolder(path string) bool {
idePaths := [2]string{".vscode", ".idea"}
for _, element := range idePaths {
if strings.Contains(path, element) {
if !didPrintIdesMsg {
log.Info("Ignoring IDE folder (such as .code, .idea)")
didPrintIdesMsg = true
}
return true
}
}
return false
}
func isMiscNotRequiredFile(path string) bool {
notRequiredSuffices := [2]string{".DS_Store", "__MACOSX"}
for _, element := range notRequiredSuffices {
if strings.HasSuffix(path, element) {
// NOTE: At the moment, these "misc" files aren't logged to avoid logging too much
return true
}
}
return false
}