Skip to content

Commit 90e5c49

Browse files
committed
Correcting IE cookie add/delete logic
1 parent 728e038 commit 90e5c49

File tree

10 files changed

+110
-28
lines changed

10 files changed

+110
-28
lines changed

Diff for: cpp/iedriver/CommandHandlers/DeleteAllCookiesCommandHandler.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ class DeleteAllCookiesCommandHandler : public IECommandHandler {
4747
browser_wrapper->GetCookies(&cookies);
4848
std::vector<BrowserCookie>::const_iterator it = cookies.begin();
4949
for (; it != cookies.end(); ++it) {
50-
std::string cookie_name = it->name();
51-
status_code = browser_wrapper->DeleteCookie(cookie_name);
50+
//std::string cookie_name = it->name();
51+
//status_code = browser_wrapper->DeleteCookie(cookie_name);
52+
status_code = browser_wrapper->DeleteCookie(*it);
5253
if (status_code != WD_SUCCESS) {
5354
response->SetErrorResponse(status_code,
5455
"Unable to delete cookie with name '" + it->name() + "'");

Diff for: cpp/iedriver/CommandHandlers/DeleteCookieCommandHandler.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ class DeleteCookieCommandHandler : public IECommandHandler {
4848
response->SetErrorResponse(status_code, "Unable to get browser");
4949
return;
5050
}
51-
status_code = browser_wrapper->DeleteCookie(cookie_name);
51+
//status_code = browser_wrapper->DeleteCookie(cookie_name);
52+
BrowserCookie cookie;
53+
cookie.set_name(cookie_name);
54+
status_code = browser_wrapper->DeleteCookie(cookie);
5255
if (status_code != WD_SUCCESS) {
5356
response->SetErrorResponse(status_code, "Unable to delete cookie");
5457
return;

Diff for: cpp/iedriver/CookieManager.cpp

+75-18
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,18 @@ void CookieManager::Initialize(HWND window_handle) {
4747

4848
bool CookieManager::SetCookie(const std::string& url,
4949
const std::string& cookie_data) {
50+
return this->SetCookie(url, cookie_data, false);
51+
}
52+
53+
bool CookieManager::SetCookie(const std::string& url,
54+
const std::string& cookie_data,
55+
const bool is_httponly) {
5056
std::string full_data = url + "|" + cookie_data;
57+
WPARAM set_flags = 0;
58+
if (is_httponly) {
59+
set_flags = INTERNET_COOKIE_HTTPONLY;
60+
}
61+
5162
HookSettings hook_settings;
5263
hook_settings.hook_procedure_name = "CookieWndProc";
5364
hook_settings.hook_procedure_type = WH_CALLWNDPROC;
@@ -57,7 +68,7 @@ bool CookieManager::SetCookie(const std::string& url,
5768
HookProcessor hook;
5869
hook.Initialize(hook_settings);
5970
hook.PushData(StringUtilities::ToWString(full_data));
60-
::SendMessage(this->window_handle_, WD_SET_COOKIE, NULL, NULL);
71+
::SendMessage(this->window_handle_, WD_SET_COOKIE, set_flags, NULL);
6172
int status = HookProcessor::GetDataBufferSize();
6273
if (status != 0) {
6374
return false;
@@ -153,8 +164,7 @@ int CookieManager::GetCookies(const std::string& url,
153164
}
154165

155166
bool CookieManager::DeleteCookie(const std::string& url,
156-
const std::string& cookie_name) {
157-
167+
const BrowserCookie& cookie) {
158168
std::wstring wide_url = StringUtilities::ToWString(url);
159169
CComPtr<IUri> uri_pointer;
160170
::CreateUri(wide_url.c_str(), Uri_CREATE_ALLOW_RELATIVE, 0, &uri_pointer);
@@ -169,56 +179,98 @@ bool CookieManager::DeleteCookie(const std::string& url,
169179

170180
std::string domain = StringUtilities::ToString(wide_domain);
171181
std::string path = StringUtilities::ToString(wide_path);
172-
return this->RecursivelyDeleteCookie(url, cookie_name, domain, path);
182+
return this->RecursivelyDeleteCookie(url,
183+
cookie.name(),
184+
domain,
185+
path,
186+
cookie.is_httponly());
173187
}
174188

189+
//bool CookieManager::DeleteCookie(const std::string& url,
190+
// const std::string& cookie_name) {
191+
//
192+
// std::wstring wide_url = StringUtilities::ToWString(url);
193+
// CComPtr<IUri> uri_pointer;
194+
// ::CreateUri(wide_url.c_str(), Uri_CREATE_ALLOW_RELATIVE, 0, &uri_pointer);
195+
//
196+
// CComBSTR host_bstr;
197+
// uri_pointer->GetHost(&host_bstr);
198+
// std::wstring wide_domain = host_bstr;
199+
//
200+
// CComBSTR path_bstr;
201+
// uri_pointer->GetPath(&path_bstr);
202+
// std::wstring wide_path = path_bstr;
203+
//
204+
// std::string domain = StringUtilities::ToString(wide_domain);
205+
// std::string path = StringUtilities::ToString(wide_path);
206+
// return this->RecursivelyDeleteCookie(url, cookie_name, domain, path, false);
207+
//}
208+
175209
bool CookieManager::RecursivelyDeleteCookie(const std::string& url,
176210
const std::string& name,
177211
const std::string& domain,
178-
const std::string& path) {
212+
const std::string& path,
213+
const bool is_httponly) {
179214
// TODO: Optimize this path from the recursive to only
180215
// call setting the cookie as often as needed.
181-
return this->RecurseCookiePath(url, name, "." + domain, path);
216+
return this->RecurseCookiePath(url, name, "." + domain, path, is_httponly);
182217
}
183218

184219
bool CookieManager::RecurseCookiePath(const std::string& url,
185220
const std::string& name,
186221
const std::string& domain,
187-
const std::string& path) {
222+
const std::string& path,
223+
const bool is_httponly) {
188224
size_t number_of_characters = 0;
189225
size_t slash_index = path.find_last_of('/');
190226
size_t final_index = path.size() - 1;
191-
if (slash_index == final_index && slash_index != 0) {
227+
if (slash_index == final_index) {
192228
number_of_characters = slash_index;
229+
} else {
230+
number_of_characters = slash_index + 1;
193231
}
194232

195233
if (slash_index != std::string::npos) {
196-
bool deleted = this->RecurseCookiePath(url, name, domain, path.substr(0, number_of_characters));
234+
bool deleted = this->RecurseCookiePath(url,
235+
name,
236+
domain,
237+
path.substr(0, number_of_characters),
238+
is_httponly);
197239
}
198-
return this->RecurseCookieDomain(url, name, domain, path);
240+
return this->RecurseCookieDomain(url, name, domain, path, is_httponly);
199241
}
200242

201243
bool CookieManager::RecurseCookieDomain(const std::string& url,
202244
const std::string& name,
203245
const std::string& domain,
204-
const std::string& path) {
205-
bool deleted = this->DeleteCookie(url, name, domain, path);
246+
const std::string& path,
247+
const bool is_httponly) {
248+
bool deleted = this->DeleteCookie(url, name, domain, path, is_httponly);
206249
if (!deleted) {
207250
size_t dot_index = domain.find_first_of('.');
208251
if (dot_index == 0) {
209-
return this->RecurseCookieDomain(url, name, domain.substr(1), path);
252+
return this->RecurseCookieDomain(url,
253+
name,
254+
domain.substr(1),
255+
path,
256+
is_httponly);
210257
} else if (dot_index != std::string::npos) {
211-
return this->RecurseCookieDomain(url, name, domain.substr(dot_index), path);
258+
return this->RecurseCookieDomain(url,
259+
name,
260+
domain.substr(dot_index),
261+
path,
262+
is_httponly);
212263
}
213-
deleted = this->DeleteCookie(url, name, "", path);
264+
deleted = this->DeleteCookie(url, name, "", path, is_httponly);
214265
}
215266
return deleted;
216267
}
217268

218269
bool CookieManager::DeleteCookie(const std::string& url,
219270
const std::string& name,
220271
const std::string& domain,
221-
const std::string& path) {
272+
const std::string& path,
273+
const bool is_httponly) {
222274
// N.B., We can hard-code the value and expiration time, since
223275
// we are deleting the cookie.
224276
std::string cookie_data = name;
@@ -232,7 +284,7 @@ bool CookieManager::DeleteCookie(const std::string& url,
232284
cookie_data.append(path);
233285
}
234286
cookie_data.append("; expires=Thu 1 Jan 1970 00:00:01 GMT");
235-
return this->SetCookie(url, cookie_data);
287+
return this->SetCookie(url, cookie_data, is_httponly);
236288
}
237289

238290
void CookieManager::ReadPersistentCookieFile(const std::wstring& file_name,
@@ -526,6 +578,7 @@ LRESULT CALLBACK CookieWndProc(int nCode, WPARAM wParam, LPARAM lParam) {
526578
webdriver::HookProcessor::CopyWStringToBuffer(file_list);
527579
webdriver::HookProcessor::WriteBufferToPipe(driver_process_id);
528580
} else if (WD_SET_COOKIE == call_window_proc_struct->message) {
581+
DWORD set_cookie_flags = static_cast<DWORD>(call_window_proc_struct->wParam);
529582
std::wstring cookie_data = webdriver::HookProcessor::CopyWStringFromBuffer();
530583
size_t url_separator_pos = cookie_data.find_first_of(L"|");
531584
std::wstring url = cookie_data.substr(0, url_separator_pos);
@@ -543,7 +596,11 @@ LRESULT CALLBACK CookieWndProc(int nCode, WPARAM wParam, LPARAM lParam) {
543596

544597
// Leverage the shared data buffer size to return the error code
545598
// back to the driver, if necessary.
546-
DWORD cookie_set = ::InternetSetCookieEx(parsed_uri.c_str(), NULL, cookie.c_str(), INTERNET_COOKIE_HTTPONLY, NULL);
599+
DWORD cookie_set = ::InternetSetCookieEx(parsed_uri.c_str(),
600+
NULL,
601+
cookie.c_str(),
602+
set_cookie_flags,
603+
NULL);
547604
if (cookie_set) {
548605
webdriver::HookProcessor::SetDataBufferSize(0);
549606
} else {

Diff for: cpp/iedriver/CookieManager.h

+12-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CookieManager {
3636
int GetCookies(const std::string& url,
3737
std::vector<BrowserCookie>* all_cookies);
3838
bool SetCookie(const std::string& url, const std::string& cookie_data);
39+
bool DeleteCookie(const std::string& url, const BrowserCookie& cookie);
3940
bool DeleteCookie(const std::string& url, const std::string& cookie_name);
4041

4142
private:
@@ -52,19 +53,26 @@ class CookieManager {
5253
bool RecursivelyDeleteCookie(const std::string& url,
5354
const std::string& name,
5455
const std::string& domain,
55-
const std::string& path);
56+
const std::string& path,
57+
const bool is_httponly);
5658
bool RecurseCookiePath(const std::string& url,
5759
const std::string& name,
5860
const std::string& domain,
59-
const std::string& path);
61+
const std::string& path,
62+
const bool is_httponly);
6063
bool RecurseCookieDomain(const std::string& url,
6164
const std::string& name,
6265
const std::string& domain,
63-
const std::string& path);
66+
const std::string& path,
67+
const bool is_httponly);
6468
bool DeleteCookie(const std::string& url,
6569
const std::string& name,
6670
const std::string& domain,
67-
const std::string& path);
71+
const std::string& path,
72+
const bool is_httponly);
73+
bool SetCookie(const std::string& url,
74+
const std::string& cookie_data,
75+
const bool is_httponly);
6876

6977
HWND window_handle_;
7078
};

Diff for: cpp/iedriver/DocumentHost.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,19 @@ int DocumentHost::AddCookie(const std::string& cookie,
250250
return WD_SUCCESS;
251251
}
252252

253-
int DocumentHost::DeleteCookie(const std::string& cookie_name) {
253+
//int DocumentHost::DeleteCookie(const std::string& cookie_name) {
254+
// LOG(TRACE) << "Entering DocumentHost::DeleteCookie";
255+
// // TODO: Optimize and return legitimate error conditions.
256+
// bool deletesucceeded = this->cookie_manager_->DeleteCookie(this->GetCurrentUrl(),
257+
// cookie_name);
258+
// return WD_SUCCESS;
259+
//}
260+
261+
int DocumentHost::DeleteCookie(const BrowserCookie& cookie) {
254262
LOG(TRACE) << "Entering DocumentHost::DeleteCookie";
255263
// TODO: Optimize and return legitimate error conditions.
256264
bool deletesucceeded = this->cookie_manager_->DeleteCookie(this->GetCurrentUrl(),
257-
cookie_name);
265+
cookie);
258266
return WD_SUCCESS;
259267
}
260268

Diff for: cpp/iedriver/DocumentHost.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ class DocumentHost {
6868

6969
void GetCookies(std::vector<BrowserCookie>* cookies);
7070
int AddCookie(const std::string& cookie, const bool validate_document_type);
71-
int DeleteCookie(const std::string& cookie_name);
71+
//int DeleteCookie(const std::string& cookie_name);
72+
int DeleteCookie(const BrowserCookie& cookie);
7273

7374
int SetFocusedFrameByIndex(const int frame_index);
7475
int SetFocusedFrameByName(const std::string& frame_name);

Diff for: cpp/iedriverserver/CHANGELOG

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ available via the project downloads page. Changes in "revision" field indicate
99
private releases checked into the prebuilts directory of the source tree, but
1010
not made generally available on the downloads page.
1111

12+
v2.46.0.5
13+
=========
14+
* Corrected cookie add/delete logic with respect to HTTP-only cookies.
15+
1216
v2.46.0.5
1317
=========
1418
* Updates to JavaScript automation atoms.

Diff for: cpp/iedriverserver/IEDriverServer.rc

0 Bytes
Binary file not shown.

Diff for: cpp/prebuilt/Win32/Release/IEDriverServer.exe

512 Bytes
Binary file not shown.

Diff for: cpp/prebuilt/x64/Release/IEDriverServer.exe

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)