SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
29.78k stars 8.02k forks source link

[🐛 Bug]: Selenium Java API: In the [Cookie] class, the 'hashCode' method only incorporates [name], but 'equals' also checks [value] #14041

Closed sbabcoc closed 1 month ago

sbabcoc commented 1 month ago

What happened?

The contract for equals and hashCode states that two objects considered to be equal should have the same hash code. In the current implementation, equals compares both [name] and [value], but hashCode is based only on [name]. This will result in bad behavior in collections where object hash code is significant (e.g. - HashMap, HashSet).

How can we reproduce the issue?

Crack open Cookie.java and examine the implementations of equals and hashCode:

  /** Two cookies are equal if the name and value match */
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof Cookie)) {
      return false;
    }

    Cookie cookie = (Cookie) o;

    if (!name.equals(cookie.name)) {
      return false;
    }
    return Objects.equals(value, cookie.value);
  }

  @Override
  public int hashCode() {
    return name.hashCode();
  }

Relevant log output

There's no specific log output for this issue. The existing implementation will just produce unexpected results in contexts where object hash codes are significant.

Operating System

Immaterial

Selenium version

Java 4.21.0

What are the browser(s) and version(s) where you see this issue?

N/A

What are the browser driver(s) and version(s) where you see this issue?

N/A

Are you using Selenium Grid?

N/A

github-actions[bot] commented 1 month ago

@sbabcoc, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

diemol commented 1 month ago

This is kind of a duplicate of #13840

The spec only matches by name; hence, the hash only has the name.

sbabcoc commented 1 month ago

The issue is not that the hash code checks just the name; the issue is that the equals method also checks the value. Consequently, two cookies with the same name but different values will have the same hash code, but they will not be considered equal. We either need to add the value to the hash code computation, or remove it from equals.

sbabcoc commented 1 month ago

https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/Object.html#hashCode()

titusfortner commented 1 month ago

Is that the common pattern for what is added to the hash? It doesn't make intuitive sense to me that two objects get considered equal based on how the spec indexes it.

github-actions[bot] commented 4 days ago

This issue has been automatically locked since there has not been any recent activity since it was closed. Please open a new issue for related bugs.