ESAPI - 使用禁止的依赖项获取NoClassDefFoundError(LoggerFactory)

Din*_*h M 3 java dependencies maven esapi

我使用 ESAPI编码字符串值来解决跨站点脚本问题,如下所示(代码片段).

String encodedString = ESAPI.encoder().encodeForHTML(value);
Run Code Online (Sandbox Code Playgroud)

异常跟踪

org.owasp.esapi.errors.ConfigurationException: java.lang.reflect.InvocationTargetException Encoder class (org.owasp.esapi.reference.DefaultEncoder) CTOR threw exception.
    at org.owasp.esapi.util.ObjFactory.make(ObjFactory.java:129)
    at org.owasp.esapi.ESAPI.encoder(ESAPI.java:99)
<bold>Caused by: java.lang.NoClassDefFoundError: org/apache/log4j/spi/LoggerFactory</bold>
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at org.owasp.esapi.util.ObjFactory.make(ObjFactory.java:74)
    at org.owasp.esapi.ESAPI.logFactory(ESAPI.java:137)
    at org.owasp.esapi.ESAPI.getLogger(ESAPI.java:154)
    at org.owasp.esapi.reference.DefaultEncoder.<init>(DefaultEncoder.java:75)
    at org.owasp.esapi.reference.DefaultEncoder.getInstance(DefaultEncoder.java:59)
    ... 71 more
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.spi.LoggerFactory
    at weblogic.utils.classloaders.GenericClassLoader.findLocalClass(GenericClassLoader.java:297)
    at weblogic.utils.classloaders.GenericClassLoader.findClass(GenericClassLoader.java:270)
    at weblogic.utils.classloaders.ChangeAwareClassLoader.findClass(ChangeAwareClassLoader.java:64)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
Run Code Online (Sandbox Code Playgroud)

Maven存储库pom.xml

<dependency>
    <groupId>org.owasp.esapi</groupId>
    <artifactId>esapi</artifactId>
    <version>${org.owasp.esapi.version}</version>
    <exclusions>
        <exclusion>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
        </exclusion>
        <exclusion>
            <groupId>xercesImpl</groupId>
            <artifactId>xercesImpl</artifactId>
        </exclusion>
        <exclusion>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
        </exclusion>
        <exclusion>
            <groupId>xml-apis</groupId>
            <artifactId>xml-apis</artifactId>
        </exclusion>
        <exclusion>
            <groupId>commons-digester</groupId>
            <artifactId>commons-digester</artifactId>
        </exclusion>
        <exclusion>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.owasp.antisamy</groupId>
            <artifactId>antisamy</artifactId>
        </exclusion>
        <exclusion>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Run Code Online (Sandbox Code Playgroud)

如果我不排除log4j那么它将抛出依赖性收敛错误,因为log4j在maven禁用依赖项中声明,如下所示

<executions>
    <execution>
        <id>enforce</id>
        <configuration>
            <rules>
                <requireJavaVersion>
                    <version>${java.version}</version>
                </requireJavaVersion>
                <requireMavenVersion>
                    <version>3.0</version>
                </requireMavenVersion>
                <DependencyConvergence />
                <bannedDependencies>
                    <excludes>
                        <!-- This should not exist as it will force SLF4J calls to be 
                            delegated to log4j -->
                        <exclude>org.slf4j:slf4j-log4j12</exclude>
                        <!-- This should not exist as it will force SLF4J calls to be 
                            delegated to jul -->
                        <exclude>org.slf4j:slf4j-jdk14</exclude>
                        <!-- Ensure only the slf4j binding for logback is on the classpath -->
                        <exclude>log4j:log4j</exclude>
                        <!-- As recommended from the slf4j guide, exclude commons-logging -->
                        <exclude>commons-logging:commons-logging</exclude>
                    </excludes>                                     
                </bannedDependencies>
            </rules>
        </configuration>
        <goals>
            <goal>enforce</goal>
        </goals>
    </execution>
</executions>
Run Code Online (Sandbox Code Playgroud)

在这方面的帮助将不胜感激.如果您需要更多详细信息,请与我们联系.提前致谢!

Car*_*rlo 8

只是一个更新:已添加对 SLF4J 的支持,因此如果您使用的是 ESAPI 版本 2.2.0.0,您可以通过将ESAPI.Logger属性设置为值来使用它org.owasp.esapi.logging.slf4j.Slf4JLogFactory


and*_*dre 8

我升级到最新版本2.2.1.1,我需要添加两个额外的属性(除了@Carlo 指出的那个)ESAPI.properties

ESAPI.Logger=org.owasp.esapi.logging.slf4j.Slf4JLogFactory
Logger.UserInfo=false
Logger.ClientInfo=false
Run Code Online (Sandbox Code Playgroud)


小智 6

ESAPI不支持SLF4J,即使它反过来支持log4j.您对ESAPI的日志记录选择是log4j或java.util.logging,通过ESAPI.properties中的ESAPI.logger属性进行控制.如果您决定使用log4j(即ESAPI.Logger = org.owasp.esapi.reference.Log4JLogFactory),那么您需要log4j.jar以及类路径中的log4j.xml或log4j.properties,具体取决于哪个版本的你正在使用的log4j.或者,您可以通过在ESAPI.properties文件中设置ESAPI.Logger = org.owasp.esapi.reference.JavaLogFactory来使用java.util.logging.

我看到你似乎正在为富国银行做这件事.如果您还有其他问题,请在Teamworks中查找"Kevin W. Wall"并发送电子邮件,因为我是安全代码审核小组的成员,也是ESAPI的项目负责人,所以考虑到更多背景,我或许可以建议其他可能性.

-kevin

  • 这不再是真的。请参阅@Carlo 的回答。 (2认同)