You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The Issue
We are using MockServer to forward a request and return the response from a target system. We see that the response returned from MockServer contains two Set-Cookie headers for one of the cookies set by the target system. This occurs for cookies where the first character of the cookie value is '!'. The request contains the 'correct' Set-Cookie header that originated with the target system and it contains a second Set-Cookie header where the value is the derived from the original one but with the leading '!' dropped.
This behaviour cause the interaction with the target system to fail when MockServer is placed between the client and the target system.
Sample
Set-Cookie headers returned when interaction is not via MockServer:
Note the additional. derived 'set-cookie' header at the end of the response headers. Note the value has the leading '!' dropped.
The Cause
The source of the problem has been tracked down to the method shown below:
publicclassMockServerHttpResponseToFullHttpResponse {
// Other methods removed for clarityprivatevoidsetCookies(HttpResponsehttpResponse, DefaultHttpResponseresponse) {
if (httpResponse.getCookieMap() != null) {
for (Map.Entry<NottableString, NottableString> cookie : httpResponse.getCookieMap().entrySet()) {
if (httpResponse.cookieHeaderDoesNotAlreadyExists(cookie.getKey().getValue(), cookie.getValue().getValue())) {
response.headers().add(SET_COOKIE, io.netty.handler.codec.http.cookie.ServerCookieEncoder.LAX.encode(newDefaultCookie(cookie.getKey().getValue(), cookie.getValue().getValue())));
}
}
}
}
}
Its aim is to detect where a cookie is (a) in the cookie map of httpResponse and (b) not present in the cookie header of the httpResponse or has a different value set in the header and, when this is detected, add the cookie into the cookie header of the the httpResponse.
This check malfunctions when the cookie value starts with a '!', it fails to recognise that the cookie is already there and adds the cookie again but with a value that drops the leading '!'.
The cause of the malfunction is that the candidate cookie value is held in the type:
In the case of strings starting with a '!' the Boolean not is set to true and the String value contains a string that has the '!' removed from the original string value.
The method to ensure all cookies are in the header with the correct value uses the following expression for the cookie value when checking for the cookie already being there and to add it if it is found to be missing.
cookie.getValue().getValue()
This returns the String value from the NottableString and so where the original value starts with a '!' returns a value without the leading '!'. This cause the method to miss that the set-cookie pair is already there and to add it again but with the corrupted value.
Possible Fix
A possible fix is to replace:
cookie.getValue().getValue()
with the method:
cookie.getValue().toString()
that does retain the leading '!'. This would give:
publicclassMockServerHttpResponseToFullHttpResponse {
// Other methods removed for clarityprivatevoidsetCookies(HttpResponsehttpResponse, DefaultHttpResponseresponse) {
if (httpResponse.getCookieMap() != null) {
for (Map.Entry<NottableString, NottableString> cookie : httpResponse.getCookieMap().entrySet()) {
if (httpResponse.cookieHeaderDoesNotAlreadyExists(cookie.getKey().getValue(), cookie.getValue().toString())) {
response.headers().add(SET_COOKIE, io.netty.handler.codec.http.cookie.ServerCookieEncoder.LAX.encode(newDefaultCookie(cookie.getKey().getValue(), cookie.getValue().toString())));
}
}
}
}
}
We have trialled this change and it does remove the duplication.
MockServer version
This issue has been observed in:
Docker Image containing 5.14.x
A local build based on the 5.15.x master branch cloned on 2024-05-26
To Reproduce
An expectation that exhibits the issue would be of the form:
Expected behaviour
Cookies with values starting with '!' that are already in the header should not cause a second Set-Cookie header to be added.
Cookies with values starting with '!' that are missing in the header should cause a Set-Cookie header to be added with the value retaining the leading '!', they should not be set to a value that drops the leading '!'.
The text was updated successfully, but these errors were encountered:
The Issue
We are using MockServer to forward a request and return the response from a target system. We see that the response returned from MockServer contains two Set-Cookie headers for one of the cookies set by the target system. This occurs for cookies where the first character of the cookie value is '!'. The request contains the 'correct' Set-Cookie header that originated with the target system and it contains a second Set-Cookie header where the value is the derived from the original one but with the leading '!' dropped.
This behaviour cause the interaction with the target system to fail when MockServer is placed between the client and the target system.
Sample
Set-Cookie headers returned when interaction is not via MockServer:
Set-Cookie headers returned when interaction is via MockServer:
Note the additional. derived 'set-cookie' header at the end of the response headers. Note the value has the leading '!' dropped.
The Cause
The source of the problem has been tracked down to the method shown below:
Its aim is to detect where a cookie is (a) in the cookie map of httpResponse and (b) not present in the cookie header of the httpResponse or has a different value set in the header and, when this is detected, add the cookie into the cookie header of the the httpResponse.
This check malfunctions when the cookie value starts with a '!', it fails to recognise that the cookie is already there and adds the cookie again but with a value that drops the leading '!'.
The cause of the malfunction is that the candidate cookie value is held in the type:
which treats leading '!' characters as having a specific 'NOT' meaning. There are three elements that represent the 'value' in this type:
In the case of strings starting with a '!' the Boolean not is set to true and the String value contains a string that has the '!' removed from the original string value.
The method to ensure all cookies are in the header with the correct value uses the following expression for the cookie value when checking for the cookie already being there and to add it if it is found to be missing.
This returns the String value from the NottableString and so where the original value starts with a '!' returns a value without the leading '!'. This cause the method to miss that the set-cookie pair is already there and to add it again but with the corrupted value.
Possible Fix
A possible fix is to replace:
with the method:
that does retain the leading '!'. This would give:
We have trialled this change and it does remove the duplication.
MockServer version
This issue has been observed in:
To Reproduce
An expectation that exhibits the issue would be of the form:
The system needs to return a Set-Cookie header that sets a value starting with a '!' an example being:
Expected behaviour
Cookies with values starting with '!' that are already in the header should not cause a second Set-Cookie header to be added.
Cookies with values starting with '!' that are missing in the header should cause a Set-Cookie header to be added with the value retaining the leading '!', they should not be set to a value that drops the leading '!'.
The text was updated successfully, but these errors were encountered: