-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* TeaVM related changes. Allows salvation to be compiled to JS instead of having to run a server * Add main class needed for TeaVM * fix indentation * expose functions on window so they're individually callable * Can't use named capturing groups. Seems like it doesn't work when it's transpiled * output a string for the JS to consume * use integers for matching since teavm doesn't support strings * checkstyle fixes * update readme and specify a filename for the js output * remove comments * abstract regex out to its own variable * function renaming * add js test and ci job * don't run the build for java8 * applying comment suggestions * fix ci command * rearrange ci job so java build doesn't need to happen twice
- Loading branch information
Showing
12 changed files
with
164 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/com/shapesecurity/salvation2/JSInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.shapesecurity.salvation2; | ||
|
||
import org.teavm.jso.JSBody; | ||
|
||
public class JSInterface { | ||
public static void main(String[] args) { | ||
initParseList(); | ||
initParseSingle(); | ||
} | ||
|
||
public static String getErrorsForSerializedCSPList(String policyText) { | ||
StringBuilder errorMessages = new StringBuilder(); | ||
Policy.parseSerializedCSPList(policyText, (severity, message, policyIndex, directiveIndex, valueIndex) -> { | ||
errorMessages.append(severity.name()) | ||
.append(" at directive ") | ||
.append(directiveIndex) | ||
.append(valueIndex == -1 ? "" : " at value " + valueIndex) | ||
.append(": ") | ||
.append(message) | ||
.append("\n"); | ||
}); | ||
return errorMessages.toString().trim(); | ||
} | ||
|
||
public static String getErrorsForSerializedCSP(String policyText) { | ||
StringBuilder errorMessages = new StringBuilder(); | ||
|
||
Policy.parseSerializedCSP(policyText, (severity, message, directiveIndex, valueIndex) -> { | ||
errorMessages.append(severity.name()) | ||
.append(" at directive ") | ||
.append(directiveIndex) | ||
.append(valueIndex == -1 ? "" : " at value " + valueIndex) | ||
.append(": ") | ||
.append(message) | ||
.append("\n"); | ||
}); | ||
return errorMessages.toString().trim(); | ||
} | ||
|
||
@JSBody(params = {}, script = | ||
"(window || globalThis).getErrorsForSerializedCSPList = (policyText) => {\n" + | ||
"return javaMethods.get('com.shapesecurity.salvation2.JSInterface.getErrorsForSerializedCSPList(Ljava/lang/String;)Ljava/lang/String;').invoke(policyText)\n" + | ||
"}") | ||
static native void initParseList(); | ||
|
||
@JSBody(params = {}, script = | ||
"(window || globalThis).getErrorsForSerializedCSP = (policyText) => {\n" + | ||
"return javaMethods.get('com.shapesecurity.salvation2.JSInterface.getErrorsForSerializedCSP(Ljava/lang/String;)Ljava/lang/String;').invoke(policyText)\n" + | ||
"}") | ||
static native void initParseSingle(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
const test = require('node:test'); | ||
const assert = require('node:assert'); | ||
const salvation = require('../../../target/javascript/salvation.min.js'); | ||
|
||
test('salvation initialization', () => { | ||
assert.notStrictEqual(salvation.main, undefined); | ||
salvation.main(); | ||
assert.notStrictEqual(getErrorsForSerializedCSP, undefined); | ||
assert.notStrictEqual(getErrorsForSerializedCSPList, undefined); | ||
}); | ||
|
||
test('.getErrorsForSerializedCSP() gives no errors for a valid CSP', () => { | ||
salvation.main(); | ||
const result = getErrorsForSerializedCSP('default-src \'none\';'); | ||
assert.strictEqual(result.length, 0, 'No errors should be found'); | ||
}); | ||
|
||
test('.getErrorsForSerializedCSP() provides feedback', () => { | ||
salvation.main(); | ||
const result = getErrorsForSerializedCSP('hello world'); | ||
assert.strictEqual(result, 'Warning at directive 0: Unrecognized directive hello'); | ||
}); | ||
|
||
test('.getErrorsForSerializedCSPList() gives no errors for a valid CSP', () => { | ||
salvation.main(); | ||
const result = getErrorsForSerializedCSPList('default-src \'none\',plugin-types image/png application/pdf; sandbox,style-src https: \'self\''); | ||
assert.strictEqual(result, ''); | ||
}); | ||
|
||
test('.getErrorsForSerializedCSPList() provides feedback', () => { | ||
salvation.main(); | ||
const result = getErrorsForSerializedCSPList('hello,foobar,script-src \'self\'; style-src \'self\''); | ||
assert.strictEqual(result, 'Warning at directive 0: Unrecognized directive hello\n' | ||
+ 'Warning at directive 0: Unrecognized directive foobar'); | ||
}); |