zaproxy / community-scripts

A collection of ZAP scripts and tips provided by the community - pull requests very welcome!
Apache License 2.0
786 stars 238 forks source link

Normalise and enforce script formatting/indentation #170

Open thc202 opened 5 years ago

thc202 commented 5 years ago

Per title, might be asking too much given the number of languages...

sourabhdeshmukh commented 4 years ago

@thc202 could you please give more details regarding which scripts needs formating.

thc202 commented 4 years ago

The ones under the script type directories (e.g. active).

kingthorin commented 2 years ago

I started to look at this based on spotless. I figured first I should look at implementing it for JavaScript. Using the eclipse web tool platform formatter fails (it does run but bombs with syntax errors).

Stack & Console output ```console $ ./gradlew spotlessApply > Task :spotlessJs FAILED Skipping 'C:\Users\thorin\Desktop\zap-ws\community-scripts\active\Cross Site WebSocket Hijacking.js' because it does not converge. Run `spotlessDiagnose` to understand why Skipping 'C:\Users\thorin\Desktop\zap-ws\community-scripts\active\JWT None Exploit.js' because it does not converge. Run `spotlessDiagnose` to understand why Skipping 'C:\Users\thorin\Desktop\zap-ws\community-scripts\active\User defined attacks.js' because it does not converge. Run `spotlessDiagnose` to understand why Step 'eclipse wtp formatters - JS' found problem in 'extender\HTTP Message Logger.js': Invalid JavaScript syntax for formatting. java.lang.IllegalArgumentException: Invalid JavaScript syntax for formatting. at com.diffplug.spotless.extra.eclipse.wtp.EclipseJsFormatterStepImpl.format(EclipseJsFormatterStepImpl.java:96) at jdk.internal.reflect.GeneratedMethodAccessor21.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at com.diffplug.spotless.extra.wtp.EclipseWtpFormatterStep.lambda$applyWithoutFile$1(EclipseWtpFormatterStep.java:64) at com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:32) at com.diffplug.spotless.FormatterStepImpl$Standard.format(FormatterStepImpl.java:78) at com.diffplug.spotless.FormatterStep$Strict.format(FormatterStep.java:76) at com.diffplug.spotless.Formatter.compute(Formatter.java:230) ... ```

So then I thought to just use some of the default methods for formatting: trimTrailingWhitespace(), indentWithSpaces(), endWithNewline(). However, the indent method doesn't replace existing indentation :cry: It will changes tabs to spaces, but it doesn't replace existing space indentation.