Skip to content

Commit 99fc46a

Browse files
committed
Removing direct call to InternetGetCookieEx2 API in IE
Versions of IE prior to 11 do not have access to this API. Linking directly to it via the Windows 8.1 SDK creates an import entry in the IEDriver.dll library used by the driver. So that the driver can continue to be used against earlier releases, we will call the API using GetModuleHandle and GetProcAddress only if it exists in the version of WinINet being used by the browser. Fixes issue #5603.
1 parent 13d8f8b commit 99fc46a

File tree

1 file changed

+39
-20
lines changed

1 file changed

+39
-20
lines changed

cpp/iedriver/CookieManager.cpp

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,24 @@ unsigned int WINAPI CookieManager::ThreadProc(LPVOID lpParameter) {
452452
extern "C" {
453453
#endif
454454

455+
// In order to run the IE driver against versions of IE that do not include
456+
// a version of WinINet.dll that supports the InternetGetCookiesEx2 API,
457+
// we must access the API in a way that does not import it into our DLL.
458+
// To that end, we duplicate the INTERNET_COOKIE2 structure here, and will
459+
// call the API (if it exists) via GetModuleHandle and GetProcAddress.
460+
typedef struct {
461+
PWSTR pwszName;
462+
PWSTR pwszValue;
463+
PWSTR pwszDomain;
464+
PWSTR pwszPath;
465+
DWORD dwFlags;
466+
FILETIME ftExpires;
467+
BOOL fExpiresSet;
468+
} INTERNETCOOKIE2;
469+
470+
typedef void* (__stdcall *InternetFreeCookiesProc)(INTERNETCOOKIE2*, DWORD);
471+
typedef DWORD(__stdcall *InternetGetCookieEx2Proc)(PCWSTR, PCWSTR, DWORD, INTERNETCOOKIE2**, PDWORD);
472+
455473
LRESULT CALLBACK CookieWndProc(int nCode, WPARAM wParam, LPARAM lParam) {
456474
CWPSTRUCT* call_window_proc_struct = reinterpret_cast<CWPSTRUCT*>(lParam);
457475
if (WM_COPYDATA == call_window_proc_struct->message) {
@@ -472,29 +490,30 @@ LRESULT CALLBACK CookieWndProc(int nCode, WPARAM wParam, LPARAM lParam) {
472490
CComBSTR path_bstr;
473491
uri_pointer->GetPath(&path_bstr);
474492

475-
// Get only the cookies for the base URL, omitting port, if there is one.
476-
// N.B., we only return cookies secure cookies when browsing a site using
477-
// SSL. The browser won't see cookies with the 'secure' flag for sites
478-
// visited using plain http.
479-
std::wstring parsed_uri = L"http";
480-
if ((WD_GET_SECURE_COOKIES == call_window_proc_struct->message ||
481-
WD_GET_SCRIPTABLE_COOKIES == call_window_proc_struct->message) &&
482-
URL_SCHEME_HTTPS == scheme) {
483-
parsed_uri.append(L"s");
484-
}
493+
std::wstring parsed_uri = scheme_bstr;
485494
parsed_uri.append(L"://");
486495
parsed_uri.append(host_bstr);
487496
parsed_uri.append(path_bstr);
488497

489-
// Allocate a static array for cookies, since IE is documented
490-
// to limit the number of cookies to 50.
498+
InternetGetCookieEx2Proc get_cookie_proc = NULL;
499+
InternetFreeCookiesProc free_cookies_proc = NULL;
500+
HMODULE wininet_handle = ::GetModuleHandle(L"wininet");
501+
if (wininet_handle) {
502+
get_cookie_proc = reinterpret_cast<InternetGetCookieEx2Proc>(::GetProcAddress(wininet_handle, "InternetGetCookieEx2"));
503+
free_cookies_proc = reinterpret_cast<InternetFreeCookiesProc>(::GetProcAddress(wininet_handle, "InternetFreeCookies"));
504+
}
505+
491506
DWORD cookie_count = 0;
492-
INTERNET_COOKIE2* cookie_pointer;
493-
DWORD success = ::InternetGetCookieEx2(parsed_uri.c_str(),
494-
NULL,
495-
INTERNET_COOKIE_NON_SCRIPT,
496-
&cookie_pointer,
497-
&cookie_count);
507+
INTERNETCOOKIE2* cookie_pointer = NULL;
508+
DWORD success = 1;
509+
if (get_cookie_proc) {
510+
success = get_cookie_proc(parsed_uri.c_str(),
511+
NULL,
512+
INTERNET_COOKIE_NON_SCRIPT,
513+
&cookie_pointer,
514+
&cookie_count);
515+
}
516+
498517
if (success == 0) {
499518
// Mimic the format of the old persistent cookie files for ease of
500519
// transmission back to the driver and parsing.
@@ -503,7 +522,7 @@ LRESULT CALLBACK CookieWndProc(int nCode, WPARAM wParam, LPARAM lParam) {
503522
if (all_cookies.size() > 0) {
504523
all_cookies.append(L"\n*\n");
505524
}
506-
INTERNET_COOKIE2* current_cookie = cookie_pointer + cookie_index;
525+
INTERNETCOOKIE2* current_cookie = cookie_pointer + cookie_index;
507526
std::wstring cookie_name = current_cookie->pwszName;
508527
std::wstring cookie_value = current_cookie->pwszValue;
509528
std::wstring cookie_domain = current_cookie->pwszDomain;
@@ -524,7 +543,7 @@ LRESULT CALLBACK CookieWndProc(int nCode, WPARAM wParam, LPARAM lParam) {
524543
all_cookies.append(L"\n\n");
525544
}
526545
}
527-
::InternetFreeCookies(cookie_pointer, cookie_count);
546+
free_cookies_proc(cookie_pointer, cookie_count);
528547
webdriver::HookProcessor::CopyWStringToBuffer(all_cookies);
529548
} else {
530549
webdriver::HookProcessor::SetDataBufferSize(sizeof(wchar_t));

0 commit comments

Comments
 (0)