Malicious code vulnerability - May expose internal representation by incorporating reference to mutable object

后端 未结 8 1501
遇见更好的自我
遇见更好的自我 2020-11-27 05:00

I have the following code in my dto class.

public void setBillDate(Date billDate) {
    this.billDate = billDate;
}

And I get an error in s

相关标签:
8条回答
  • 2020-11-27 06:01

    A counter argument can be, why one would one unintentionally modify the date? If client sets the value and then modifies it, then our code should reflect it, isn't it? If not then is it not confusing?

    I prefer to just ignore this FindBugs warning.

    In case if you want to do that, just add following Maven dependencies in your pom.xml:

    <!-- Findbugs -->
            <dependency>
                <groupId>com.google.code.findbugs</groupId>
                <artifactId>annotations</artifactId>
                <version>3.0.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.google.code.findbugs</groupId>
                <artifactId>annotations</artifactId>
                <version>3.0.1</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>com.google.code.findbugs</groupId>
                <artifactId>jsr305</artifactId>
                <version>3.0.1</version>
                <scope>provided</scope>
            </dependency>
    

    and then these annotations at class or member field level in your POJO:

    @SuppressFBWarnings(value = { "EI_EXPOSE_REP", "EI_EXPOSE_REP2" }, justification = "I prefer to suppress these FindBugs warnings")
    

    Cheers

    Akshay

    0 讨论(0)
  • 2020-11-27 06:02

    I wonder why none of the solutions takes null into consideration. A general, null-safe solution should look like this:

    public void setBillDate(Date billDate) {
        this.billDate = billDate != null ? new Date(billDate.getTime()) : null;
    }
    
    0 讨论(0)
提交回复
热议问题