From d1256b9323be6161574f81d62442c4d8120fa29a Mon Sep 17 00:00:00 2001 From: richie Date: Tue, 15 Jan 2019 10:56:07 +0800 Subject: [PATCH] Initial Commit --- .gitignore | 5 + build.xml | 130 ++++++++++++++++++ plugin.xml | 23 ++++ pom.xml | 35 +++++ .../function/RemoteEvalFunctionDefender.java | 38 +++++ .../function/RemoteEvalInitializeMonitor.java | 22 +++ .../function/RemoteEvalLocaleFinder.java | 15 ++ .../function/conf/RemoteEvalConfig.java | 70 ++++++++++ .../function/holder/HolderFunction.java | 29 ++++ .../signature/SignatureGenerator.java | 46 +++++++ .../com/fr/security/function/ref.properties | 6 + .../fr/security/function/ref_zh_CN.properties | 6 + 12 files changed, 425 insertions(+) create mode 100644 .gitignore create mode 100644 build.xml create mode 100755 plugin.xml create mode 100644 pom.xml create mode 100644 src/main/java/com/fr/security/function/RemoteEvalFunctionDefender.java create mode 100644 src/main/java/com/fr/security/function/RemoteEvalInitializeMonitor.java create mode 100644 src/main/java/com/fr/security/function/RemoteEvalLocaleFinder.java create mode 100644 src/main/java/com/fr/security/function/conf/RemoteEvalConfig.java create mode 100644 src/main/java/com/fr/security/function/holder/HolderFunction.java create mode 100644 src/main/java/com/fr/security/function/signature/SignatureGenerator.java create mode 100644 src/main/resources/com/fr/security/function/ref.properties create mode 100644 src/main/resources/com/fr/security/function/ref_zh_CN.properties diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3189420 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +*.iml +.idea/ +.DS_Store +.classpath +lib/report/* \ No newline at end of file diff --git a/build.xml b/build.xml new file mode 100644 index 0000000..c3fb472 --- /dev/null +++ b/build.xml @@ -0,0 +1,130 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/plugin.xml b/plugin.xml new file mode 100755 index 0000000..1f12bdd --- /dev/null +++ b/plugin.xml @@ -0,0 +1,23 @@ + + + com.fr.security.function + com.fr.security.function + + yes + 1.2 + 10.0 + 2019-01-11 + richie + + + [2019-01-12]初始化插件。
+ ]]>
+ + + + + + + +
\ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..9b11897 --- /dev/null +++ b/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + + + com.fr.maven + finereport-maven + 10.0 + + jar + demo-function-defender + + + com.fr.core + fine-core-sdk + 10.0 + + + + + ${web-inf-path}/plugins/plugin-com.fr.security.function-1.0/classes + + + org.apache.maven.plugins + maven-compiler-plugin + + 6 + 6 + + + + + \ No newline at end of file diff --git a/src/main/java/com/fr/security/function/RemoteEvalFunctionDefender.java b/src/main/java/com/fr/security/function/RemoteEvalFunctionDefender.java new file mode 100644 index 0000000..320782e --- /dev/null +++ b/src/main/java/com/fr/security/function/RemoteEvalFunctionDefender.java @@ -0,0 +1,38 @@ +package com.fr.security.function; + +import com.fr.intelli.record.Focus; +import com.fr.record.analyzer.EnableMetrics; +import com.fr.security.function.conf.RemoteEvalConfig; +import com.fr.security.function.holder.HolderFunction; +import com.fr.stable.fun.impl.AbstractFunctionDefendProvider; +import com.fr.stable.script.CalculatorProvider; +import com.fr.stable.script.Function; + +import java.lang.annotation.Annotation; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-01-12 + */ +@EnableMetrics +public class RemoteEvalFunctionDefender extends AbstractFunctionDefendProvider { + + @Override + @Focus(id = "com.fr.security.function", text = "defender") + public Function replacer(CalculatorProvider calculator, Class clazz) { + return HolderFunction.ONE; + } + + @Override + public boolean match(CalculatorProvider calculator, Class clazz) { + if (RemoteEvalConfig.getInstance().isEnable()) { + Boolean mark = calculator.getAttribute(RestrictScriptKey.KEY); + if (mark != null && mark) { + Annotation restrict = clazz.getAnnotation(RestrictScript.class); + return restrict != null; + } + } + return false; + } +} diff --git a/src/main/java/com/fr/security/function/RemoteEvalInitializeMonitor.java b/src/main/java/com/fr/security/function/RemoteEvalInitializeMonitor.java new file mode 100644 index 0000000..0bdb115 --- /dev/null +++ b/src/main/java/com/fr/security/function/RemoteEvalInitializeMonitor.java @@ -0,0 +1,22 @@ +package com.fr.security.function; + +import com.fr.plugin.context.PluginContext; +import com.fr.plugin.observer.inner.AbstractPluginLifecycleMonitor; +import com.fr.security.function.conf.RemoteEvalConfig; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-01-12 + */ +public class RemoteEvalInitializeMonitor extends AbstractPluginLifecycleMonitor { + @Override + public void afterRun(PluginContext context) { + RemoteEvalConfig.getInstance(); + } + + @Override + public void beforeStop(PluginContext context) { + + } +} diff --git a/src/main/java/com/fr/security/function/RemoteEvalLocaleFinder.java b/src/main/java/com/fr/security/function/RemoteEvalLocaleFinder.java new file mode 100644 index 0000000..9cba375 --- /dev/null +++ b/src/main/java/com/fr/security/function/RemoteEvalLocaleFinder.java @@ -0,0 +1,15 @@ +package com.fr.security.function; + +import com.fr.stable.fun.impl.AbstractLocaleFinder; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-01-12 + */ +public class RemoteEvalLocaleFinder extends AbstractLocaleFinder { + @Override + public String find() { + return "com/fr/security/function/ref"; + } +} diff --git a/src/main/java/com/fr/security/function/conf/RemoteEvalConfig.java b/src/main/java/com/fr/security/function/conf/RemoteEvalConfig.java new file mode 100644 index 0000000..c65e378 --- /dev/null +++ b/src/main/java/com/fr/security/function/conf/RemoteEvalConfig.java @@ -0,0 +1,70 @@ +package com.fr.security.function.conf; + +import com.fr.config.ConfigContext; +import com.fr.config.DefaultConfiguration; +import com.fr.config.Identifier; +import com.fr.config.Status; +import com.fr.config.Visualization; +import com.fr.config.holder.Conf; +import com.fr.config.holder.factory.Holders; +import com.fr.stable.StringUtils; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-01-12 + */ +@Visualization(category = "Plugin-Security") +public class RemoteEvalConfig extends DefaultConfiguration { + + private static volatile RemoteEvalConfig config = null; + + public static RemoteEvalConfig getInstance() { + if (config == null) { + config = ConfigContext.getConfigInstance(RemoteEvalConfig.class); + } + return config; + } + + @Identifier(value = "enable", name = "Plugin-Remote_Eval_Restrict", description = "Plugin-Remote_Eval_Restrict_Description", status = Status.SHOW) + private Conf enable = Holders.simple(true); + + @Identifier(value = "text", name = "Plugin-Remote_Eval_Restrict_Text", description = "Plugin-Remote_Eval_Restrict_Text_Description", status = Status.SHOW) + private Conf text = Holders.simple(StringUtils.EMPTY); + + @Identifier(value = "signatureText", name = "Plugin-Remote_Signature_Key", description = "Plugin-Remote_Signature_Key", status = Status.SHOW) + private Conf signatureText = Holders.simple(StringUtils.EMPTY); + + public boolean isEnable() { + return enable.get(); + } + + public void setEnable(boolean enable) { + this.enable.set(enable); + } + + public String getText() { + return text.get(); + } + + public void setText(String text) { + this.text.set(text); + } + + public String getSignatureText() { + return signatureText.get(); + } + + public void setSignatureText(String signatureText) { + this.signatureText.set(signatureText); + } + + @Override + public Object clone() throws CloneNotSupportedException { + RemoteEvalConfig cloned = (RemoteEvalConfig) super.clone(); + cloned.enable = (Conf) enable.clone(); + cloned.text = (Conf) text.clone(); + cloned.signatureText = (Conf) signatureText.clone(); + return cloned; + } +} diff --git a/src/main/java/com/fr/security/function/holder/HolderFunction.java b/src/main/java/com/fr/security/function/holder/HolderFunction.java new file mode 100644 index 0000000..03e14a9 --- /dev/null +++ b/src/main/java/com/fr/security/function/holder/HolderFunction.java @@ -0,0 +1,29 @@ +package com.fr.security.function.holder; + +import com.fr.script.AbstractFunction; +import com.fr.security.function.conf.RemoteEvalConfig; +import com.fr.stable.StringUtils; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-01-12 + */ +public class HolderFunction extends AbstractFunction { + + public static final HolderFunction ONE = new HolderFunction(); + + private HolderFunction() { + + } + + @Override + public Object run(Object[] args) { + String text = RemoteEvalConfig.getInstance().getText(); + if (StringUtils.isBlank(text)) { + throw new IllegalStateException("FR.remoteEvaluate cannot call this formula because of safety."); + } else { + return text; + } + } +} diff --git a/src/main/java/com/fr/security/function/signature/SignatureGenerator.java b/src/main/java/com/fr/security/function/signature/SignatureGenerator.java new file mode 100644 index 0000000..fd79e3e --- /dev/null +++ b/src/main/java/com/fr/security/function/signature/SignatureGenerator.java @@ -0,0 +1,46 @@ +package com.fr.security.function.signature; + +import com.fr.cert.token.JwtBuilder; +import com.fr.cert.token.Jwts; +import com.fr.cert.token.SignatureAlgorithm; +import com.fr.general.GeneralUtils; +import com.fr.script.AbstractFunction; +import com.fr.security.function.conf.RemoteEvalConfig; +import com.fr.stable.ArrayUtils; +import com.fr.stable.Primitive; + +import java.util.Date; + +/** + * @author richie + * @version 10.0 + * Created by richie on 2019-01-14 + */ +public class SignatureGenerator extends AbstractFunction { + + private static final long VALIDATE_TIME = 10 * 60 * 1000; + @Override + public Object run(Object[] args) { + if (ArrayUtils.isEmpty(args)) { + return Primitive.NULL; + } + return createJwt(GeneralUtils.objectToString(args[0]), RemoteEvalConfig.getInstance().getSignatureText()); + } + + private String createJwt(String subject, String key) { + SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; + Date currentTime = new Date(); + Date expirationTime = new Date(currentTime.getTime() + VALIDATE_TIME); + JwtBuilder builder = Jwts.builder() + .setSubject(subject) + .setIssuedAt(currentTime) + .setExpiration(expirationTime) + .signWith(signatureAlgorithm, key); + return builder.compact(); + } + + public static void main(String... args) { + SignatureGenerator generator = new SignatureGenerator(); + System.out.println(generator.createJwt("/习题9.cpt", "GhzUsb4fmGwmSk57d3uBZ9jVFCRgZ9CVMT3WoomMHi4=")); + } +} diff --git a/src/main/resources/com/fr/security/function/ref.properties b/src/main/resources/com/fr/security/function/ref.properties new file mode 100644 index 0000000..98f2276 --- /dev/null +++ b/src/main/resources/com/fr/security/function/ref.properties @@ -0,0 +1,6 @@ +Plugin-Remote_Eval_Restrict=Remote Eval Enable +Plugin-Remote_Eval_Restrict_Description=Allow Remote Function Eval +Plugin-Security=Security +Plugin-Remote_Eval_Restrict_Text=Return Error Text +Plugin-Remote_Eval_Restrict_Text_Description=Replace the real formula +Plugin-Remote_Signature_Key=Signature Key \ No newline at end of file diff --git a/src/main/resources/com/fr/security/function/ref_zh_CN.properties b/src/main/resources/com/fr/security/function/ref_zh_CN.properties new file mode 100644 index 0000000..be86e0f --- /dev/null +++ b/src/main/resources/com/fr/security/function/ref_zh_CN.properties @@ -0,0 +1,6 @@ +Plugin-Remote_Eval_Restrict=\u542F\u7528\u811A\u672C\u4E2D\u516C\u5F0F\u8C03\u7528\u9650\u5236 +Plugin-Remote_Eval_Restrict_Description=\u4E0D\u5141\u8BB8\u5728\u811A\u672C\u51FD\u6570\u4E2D\u8C03\u7528\u6570\u636E\u5E93\u76F8\u5173\u7684\u51FD\u6570 +Plugin-Security=\u5B89\u5168 +Plugin-Remote_Eval_Restrict_Text=\u51FD\u6570\u8FD4\u56DE\u503C +Plugin-Remote_Eval_Restrict_Text_Description=\u66FF\u6362\u6B63\u786E\u7684\u516C\u5F0F\u6267\u884C\u540E\u7684\u8FD4\u56DE\u503C +Plugin-Remote_Signature_Key=\u6570\u5B57\u7B7E\u540D\u79D8\u94A5 \ No newline at end of file