【问题标题】:How to fix Log4J Vulnerability in Gradle Project如何修复 Gradle 项目中的 Log4J 漏洞
【发布时间】:2022-01-18 16:47:50
【问题描述】:

我可以在我的项目的 Gradle 依赖树中看到 org.apache.logging.log4j:log4j-core:2.14.0 库。

我们还没有从外部添加 log4j 版本。这个版本是来自其他 jars 或 spring-boot-starter 的传递依赖的一部分。

如何在 Gradle 中覆盖 log4j 版本?

【问题讨论】:

标签: spring spring-boot gradle log4j2


【解决方案1】:
dependencies {
    constraints {
        implementation('org.apache.logging.log4j:log4j-api') {
            version {
                strictly('[2.17, 3[')
                prefer('2.17.0')
            }
            because('CVE-2021-44228, CVE-2021-45046, CVE-2021-45105: Log4j vulnerable to remote code execution and other critical security vulnerabilities')
        }

        implementation('org.apache.logging.log4j:log4j-core') {
            version {
                strictly('[2.17, 3[')
                prefer('2.17.0')
            }
            because('CVE-2021-44228, CVE-2021-45046, CVE-2021-45105: Log4j vulnerable to remote code execution and other critical security vulnerabilities')
        }

        implementation('org.apache.logging.log4j:log4j-slf4j-impl') {
            version {
                strictly('[2.17, 3[')
                prefer('2.17.0')
            }
            because('CVE-2021-44228, CVE-2021-45046, CVE-2021-45105: Log4j vulnerable to remote code execution and other critical security vulnerabilities')
        }

        implementation('org.apache.logging.log4j:log4j-web') {
            version {
                strictly('[2.17, 3[')
                prefer('2.17.0')
            }
            because('CVE-2021-44228, CVE-2021-45046, CVE-2021-45105: Log4j vulnerable to remote code execution and other critical security vulnerabilities')
        }
    }
}

【讨论】:

    【解决方案2】:

    首先,找出您真正使用的 log4j 相关库,例如由

     .\gradlew dependencies --configuration=testRuntimeClasspath | find "log4j"
    

    然后用当前版本(docs)覆盖它们,放在dependencies 块之后:

    configurations.all {
        resolutionStrategy {
            force 'org.apache.logging.log4j:log4j-api:2.17.0'
            force 'org.apache.logging.log4j:log4j-core:2.17.0'
            force 'org.apache.logging.log4j:log4j-slf4j-impl:2.17.0'
            force 'org.apache.logging.log4j:log4j-jul:2.17.0'
        }
    }
    

    您可能需要向该块添加更多/更少的库,具体取决于开始时的检查结果。

    由于你使用的是Spring Boot,你也可以使用Spring-Boot-specific feature来设置Log4J版本:

    ext['log4j2.version'] = '2.17.0'
    

    【讨论】:

    • 不幸的是,这并没有强制在依赖项内部进行升级。我仍然可以通过gradlew dependencies 看到有旧版本
    • 这个方法也特别适用于传递依赖。
    • 对于 build.gradle.kts / KotlinDSL 应该如何看待?至少对我来说不是:-(
    • 我以不同的方式修复了它,它现在可以在我自己的答案中使用 stackoverflow.com/a/70367520/863403
    【解决方案3】:

    我在 MacOS 上并使用子项目:

    首先我运行:./gradlew projects,它将列出我的子项目:

    输出:

    :projects
    
    ------------------------------------------------------------
    Root project
    ------------------------------------------------------------
    
    Root project 'test-backend'
    +--- Project ':test-suite'
    +--- Project ':test-suite-services'
    \--- Project ':test-utils'
    

    使用输出我们可以一一检查依赖关系:

    ./gradlew test-suite:dependencies | grep "log4j"
    ./gradlew test-suite-services:dependencies | grep "log4j"
    ./gradlew test-utils:dependencies | grep "log4j"
    

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 2022-01-18
      • 2022-01-20
      • 2021-08-14
      相关资源
      最近更新 更多