260 if (aCookie) { |
260 if (aCookie) { |
261 PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("----------------\n")); |
261 PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("----------------\n")); |
262 PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("name: %s\n", aCookie->Name().get())); |
262 PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("name: %s\n", aCookie->Name().get())); |
263 PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("value: %s\n", aCookie->Value().get())); |
263 PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("value: %s\n", aCookie->Value().get())); |
264 PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("%s: %s\n", aCookie->IsDomain() ? "domain" : "host", aCookie->Host().get())); |
264 PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("%s: %s\n", aCookie->IsDomain() ? "domain" : "host", aCookie->Host().get())); |
|
265 PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("orighost: %s\n", aCookie->Origin().get())); |
265 PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("path: %s\n", aCookie->Path().get())); |
266 PR_LOG(GetCookieLog(), PR_LOG_DEBUG,("path: %s\n", aCookie->Path().get())); |
266 |
267 |
267 PR_ExplodeTime(aCookie->Expiry() * int64_t(PR_USEC_PER_SEC), |
268 PR_ExplodeTime(aCookie->Expiry() * int64_t(PR_USEC_PER_SEC), |
268 PR_GMTParameters, &explodedTime); |
269 PR_GMTParameters, &explodedTime); |
269 PR_FormatTimeUSEnglish(timeString, 40, "%c GMT", &explodedTime); |
270 PR_FormatTimeUSEnglish(timeString, 40, "%c GMT", &explodedTime); |
1632 NS_GetAppInfo(aChannel, &appId, &inBrowserElement); |
1633 NS_GetAppInfo(aChannel, &appId, &inBrowserElement); |
1633 } |
1634 } |
1634 |
1635 |
1635 bool isPrivate = aChannel && NS_UsePrivateBrowsing(aChannel); |
1636 bool isPrivate = aChannel && NS_UsePrivateBrowsing(aChannel); |
1636 |
1637 |
|
1638 nsCOMPtr<nsIURI> firstPartyURI; |
|
1639 mThirdPartyUtil->GetFirstPartyURI(aChannel, nullptr, getter_AddRefs(firstPartyURI)); |
|
1640 bool requireHostMatch; |
|
1641 nsAutoCString origDomain; |
|
1642 nsresult rv = GetBaseDomain(firstPartyURI, origDomain, requireHostMatch); |
|
1643 if (NS_FAILED(rv)) { |
|
1644 COOKIE_LOGFAILURE(GET_COOKIE, aHostURI, nullptr, |
|
1645 "couldn't get base domain from URI"); |
|
1646 } |
|
1647 |
1637 nsAutoCString result; |
1648 nsAutoCString result; |
1638 GetCookieStringInternal(aHostURI, isForeign, aHttpBound, appId, |
1649 GetCookieStringInternal(aHostURI, isForeign, aHttpBound, appId, |
1639 inBrowserElement, isPrivate, result); |
1650 inBrowserElement, isPrivate, origDomain, result); |
1640 *aCookie = result.IsEmpty() ? nullptr : ToNewCString(result); |
1651 *aCookie = result.IsEmpty() ? nullptr : ToNewCString(result); |
1641 return NS_OK; |
1652 return NS_OK; |
1642 } |
1653 } |
1643 |
1654 |
1644 NS_IMETHODIMP |
1655 NS_IMETHODIMP |
1714 serverTime, aFromHttp, appId, inBrowserElement, |
1725 serverTime, aFromHttp, appId, inBrowserElement, |
1715 isPrivate, aChannel); |
1726 isPrivate, aChannel); |
1716 return NS_OK; |
1727 return NS_OK; |
1717 } |
1728 } |
1718 |
1729 |
|
1730 // FIXME:MSvB DEBUG DEBUG - DELETEME DELETEME - debug debug - deleteme deleteme |
|
1731 // FIXME:MSvB Setting a 3rd party cookie (on third.tld) for URL bar browsed |
|
1732 // FIXME:MSvB site first.tld causes aHostURI (and later the origin var) to |
|
1733 // FIXME:MSvB contain 'third.tld' |
1719 void |
1734 void |
1720 nsCookieService::SetCookieStringInternal(nsIURI *aHostURI, |
1735 nsCookieService::SetCookieStringInternal(nsIURI *aHostURI, |
1721 bool aIsForeign, |
1736 bool aIsForeign, |
1722 nsDependentCString &aCookieHeader, |
1737 nsDependentCString &aCookieHeader, |
1723 const nsCString &aServerTime, |
1738 const nsCString &aServerTime, |
1790 serverTime = tempServerTime / int64_t(PR_USEC_PER_SEC); |
1805 serverTime = tempServerTime / int64_t(PR_USEC_PER_SEC); |
1791 } else { |
1806 } else { |
1792 serverTime = PR_Now() / PR_USEC_PER_SEC; |
1807 serverTime = PR_Now() / PR_USEC_PER_SEC; |
1793 } |
1808 } |
1794 |
1809 |
|
1810 // double keyed cookie boilerplate |
|
1811 nsCOMPtr<nsIURI> firstPartyURI; |
|
1812 mThirdPartyUtil->GetFirstPartyURI(aChannel, nullptr, getter_AddRefs(firstPartyURI)); |
|
1813 nsAutoCString origDomain; |
|
1814 rv = GetBaseDomain(firstPartyURI, origDomain, requireHostMatch); |
|
1815 if (NS_FAILED(rv)) { |
|
1816 COOKIE_LOGFAILURE(GET_COOKIE, aHostURI, nullptr, |
|
1817 "couldn't get base domain from URI"); |
|
1818 } |
|
1819 |
1795 // process each cookie in the header |
1820 // process each cookie in the header |
1796 while (SetCookieInternal(aHostURI, key, requireHostMatch, cookieStatus, |
1821 while (SetCookieInternal(aHostURI, key, requireHostMatch, origDomain, |
1797 aCookieHeader, serverTime, aFromHttp, aChannel)) { |
1822 cookieStatus, aCookieHeader, serverTime, |
|
1823 aFromHttp, aChannel)) { |
1798 // document.cookie can only set one cookie at a time |
1824 // document.cookie can only set one cookie at a time |
1799 if (!aFromHttp) |
1825 if (!aFromHttp) |
1800 break; |
1826 break; |
1801 } |
1827 } |
1802 } |
1828 } |
2024 NS_ENSURE_SUCCESS(rv, rv); |
2050 NS_ENSURE_SUCCESS(rv, rv); |
2025 |
2051 |
2026 int64_t currentTimeInUsec = PR_Now(); |
2052 int64_t currentTimeInUsec = PR_Now(); |
2027 |
2053 |
2028 nsRefPtr<nsCookie> cookie = |
2054 nsRefPtr<nsCookie> cookie = |
2029 nsCookie::Create(aName, aValue, host, aPath, |
2055 nsCookie::Create(aName, aValue, host, baseDomain, aPath, |
2030 aExpiry, |
2056 aExpiry, |
2031 currentTimeInUsec, |
2057 currentTimeInUsec, |
2032 nsCookie::GenerateUniqueCreationTime(currentTimeInUsec), |
2058 nsCookie::GenerateUniqueCreationTime(currentTimeInUsec), |
2033 aIsSession, |
2059 aIsSession, |
2034 aIsSecure, |
2060 aIsSecure, |
2062 NS_ENSURE_SUCCESS(rv, rv); |
2088 NS_ENSURE_SUCCESS(rv, rv); |
2063 |
2089 |
2064 nsListIter matchIter; |
2090 nsListIter matchIter; |
2065 nsRefPtr<nsCookie> cookie; |
2091 nsRefPtr<nsCookie> cookie; |
2066 if (FindCookie(nsCookieKey(baseDomain, aAppId, aInBrowserElement), |
2092 if (FindCookie(nsCookieKey(baseDomain, aAppId, aInBrowserElement), |
|
2093 baseDomain, |
2067 host, |
2094 host, |
2068 PromiseFlatCString(aName), |
2095 PromiseFlatCString(aName), |
2069 PromiseFlatCString(aPath), |
2096 PromiseFlatCString(aPath), |
2070 matchIter)) { |
2097 matchIter)) { |
2071 cookie = matchIter.Cookie(); |
2098 cookie = matchIter.Cookie(); |
2189 int64_t lastAccessed = aRow->AsInt64(IDX_LAST_ACCESSED); |
2216 int64_t lastAccessed = aRow->AsInt64(IDX_LAST_ACCESSED); |
2190 int64_t creationTime = aRow->AsInt64(IDX_CREATION_TIME); |
2217 int64_t creationTime = aRow->AsInt64(IDX_CREATION_TIME); |
2191 bool isSecure = 0 != aRow->AsInt32(IDX_SECURE); |
2218 bool isSecure = 0 != aRow->AsInt32(IDX_SECURE); |
2192 bool isHttpOnly = 0 != aRow->AsInt32(IDX_HTTPONLY); |
2219 bool isHttpOnly = 0 != aRow->AsInt32(IDX_HTTPONLY); |
2193 |
2220 |
|
2221 nsAutoCString baseDomain; |
|
2222 rv = GetBaseDomainFromHost(host, baseDomain); |
|
2223 NS_ASSERT_SUCCESS(rv); |
|
2224 |
2194 // Create a new nsCookie and assign the data. |
2225 // Create a new nsCookie and assign the data. |
2195 return nsCookie::Create(name, value, host, path, |
2226 return nsCookie::Create(name, value, host, baseDomain, path, |
2196 expiry, |
2227 expiry, |
2197 lastAccessed, |
2228 lastAccessed, |
2198 creationTime, |
2229 creationTime, |
2199 false, |
2230 false, |
2200 isSecure, |
2231 isSecure, |
2577 // creation time, so just use the current time to generate a unique one. |
2608 // creation time, so just use the current time to generate a unique one. |
2578 nsRefPtr<nsCookie> newCookie = |
2609 nsRefPtr<nsCookie> newCookie = |
2579 nsCookie::Create(Substring(buffer, nameIndex, cookieIndex - nameIndex - 1), |
2610 nsCookie::Create(Substring(buffer, nameIndex, cookieIndex - nameIndex - 1), |
2580 Substring(buffer, cookieIndex, buffer.Length() - cookieIndex), |
2611 Substring(buffer, cookieIndex, buffer.Length() - cookieIndex), |
2581 host, |
2612 host, |
|
2613 baseDomain, |
2582 Substring(buffer, pathIndex, secureIndex - pathIndex - 1), |
2614 Substring(buffer, pathIndex, secureIndex - pathIndex - 1), |
2583 expires, |
2615 expires, |
2584 lastAccessedCounter, |
2616 lastAccessedCounter, |
2585 nsCookie::GenerateUniqueCreationTime(currentTimeInUsec), |
2617 nsCookie::GenerateUniqueCreationTime(currentTimeInUsec), |
2586 false, |
2618 false, |
2730 // iterate the cookies! |
2763 // iterate the cookies! |
2731 const nsCookieEntry::ArrayType &cookies = entry->GetCookies(); |
2764 const nsCookieEntry::ArrayType &cookies = entry->GetCookies(); |
2732 for (nsCookieEntry::IndexType i = 0; i < cookies.Length(); ++i) { |
2765 for (nsCookieEntry::IndexType i = 0; i < cookies.Length(); ++i) { |
2733 cookie = cookies[i]; |
2766 cookie = cookies[i]; |
2734 |
2767 |
|
2768 // Check the origin key. We only continue if the |
|
2769 // saved origin matches matches the origin domain. |
|
2770 // FIXME:MSvB, other places iterate cookies too, handle them likewise? |
|
2771 if (cookie->Origin() != aOrigDomain) { |
|
2772 continue; |
|
2773 } |
|
2774 |
2735 // check the host, since the base domain lookup is conservative. |
2775 // check the host, since the base domain lookup is conservative. |
2736 // first, check for an exact host or domain cookie match, e.g. "google.com" |
2776 // first, check for an exact host or domain cookie match, e.g. "google.com" |
2737 // or ".google.com"; second a subdomain match, e.g. |
2777 // or ".google.com"; second a subdomain match, e.g. |
2738 // host = "mail.google.com", cookie domain = ".google.com". |
2778 // host = "mail.google.com", cookie domain = ".google.com". |
2739 if (cookie->RawHost() != hostFromURI && |
2779 if (cookie->RawHost() != hostFromURI && |
2852 // to be processed |
2892 // to be processed |
2853 bool |
2893 bool |
2854 nsCookieService::SetCookieInternal(nsIURI *aHostURI, |
2894 nsCookieService::SetCookieInternal(nsIURI *aHostURI, |
2855 const nsCookieKey &aKey, |
2895 const nsCookieKey &aKey, |
2856 bool aRequireHostMatch, |
2896 bool aRequireHostMatch, |
|
2897 const nsCString &aOrigin, |
2857 CookieStatus aStatus, |
2898 CookieStatus aStatus, |
2858 nsDependentCString &aCookieHeader, |
2899 nsDependentCString &aCookieHeader, |
2859 int64_t aServerTime, |
2900 int64_t aServerTime, |
2860 bool aFromHttp, |
2901 bool aFromHttp, |
2861 nsIChannel *aChannel) |
2902 nsIChannel *aChannel) |
2908 COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, savedCookieHeader, "failed the path tests"); |
2949 COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, savedCookieHeader, "failed the path tests"); |
2909 return newCookie; |
2950 return newCookie; |
2910 } |
2951 } |
2911 |
2952 |
2912 // create a new nsCookie and copy attributes |
2953 // create a new nsCookie and copy attributes |
|
2954 //FIXME:MSvB, The name and value vars are neither host nor key |
|
2955 //FIXME:MSvB, host shows up in cookie inspector, as a index key |
2913 nsRefPtr<nsCookie> cookie = |
2956 nsRefPtr<nsCookie> cookie = |
2914 nsCookie::Create(cookieAttributes.name, |
2957 nsCookie::Create(cookieAttributes.name, |
2915 cookieAttributes.value, |
2958 cookieAttributes.value, |
2916 cookieAttributes.host, |
2959 cookieAttributes.host, |
|
2960 aOrigin, |
2917 cookieAttributes.path, |
2961 cookieAttributes.path, |
2918 cookieAttributes.expiryTime, |
2962 cookieAttributes.expiryTime, |
2919 currentTimeInUsec, |
2963 currentTimeInUsec, |
2920 nsCookie::GenerateUniqueCreationTime(currentTimeInUsec), |
2964 nsCookie::GenerateUniqueCreationTime(currentTimeInUsec), |
2921 cookieAttributes.isSession, |
2965 cookieAttributes.isSession, |
2973 "cookie is httponly; coming from script"); |
3017 "cookie is httponly; coming from script"); |
2974 return; |
3018 return; |
2975 } |
3019 } |
2976 |
3020 |
2977 nsListIter matchIter; |
3021 nsListIter matchIter; |
2978 bool foundCookie = FindCookie(aKey, aCookie->Host(), |
3022 bool foundCookie = FindCookie(aKey, aCookie->Origin(), |
2979 aCookie->Name(), aCookie->Path(), matchIter); |
3023 aCookie->Host(), aCookie->Name(), aCookie->Path(), matchIter); |
2980 |
3024 |
2981 nsRefPtr<nsCookie> oldCookie; |
3025 nsRefPtr<nsCookie> oldCookie; |
2982 nsCOMPtr<nsIArray> purgedList; |
3026 nsCOMPtr<nsIArray> purgedList; |
2983 if (foundCookie) { |
3027 if (foundCookie) { |
2984 oldCookie = matchIter.Cookie(); |
3028 oldCookie = matchIter.Cookie(); |
3883 if (!mDBState) { |
3927 if (!mDBState) { |
3884 NS_WARNING("No DBState! Profile already closed?"); |
3928 NS_WARNING("No DBState! Profile already closed?"); |
3885 return NS_ERROR_NOT_AVAILABLE; |
3929 return NS_ERROR_NOT_AVAILABLE; |
3886 } |
3930 } |
3887 |
3931 |
3888 nsAutoCString host, name, path; |
3932 nsAutoCString host, origin, name, path; |
3889 nsresult rv = aCookie->GetHost(host); |
3933 nsresult rv = aCookie->GetHost(host); |
|
3934 NS_ENSURE_SUCCESS(rv, rv); |
|
3935 rv = aCookie->GetOrigin(origin); |
3890 NS_ENSURE_SUCCESS(rv, rv); |
3936 NS_ENSURE_SUCCESS(rv, rv); |
3891 rv = aCookie->GetName(name); |
3937 rv = aCookie->GetName(name); |
3892 NS_ENSURE_SUCCESS(rv, rv); |
3938 NS_ENSURE_SUCCESS(rv, rv); |
3893 rv = aCookie->GetPath(path); |
3939 rv = aCookie->GetPath(path); |
3894 NS_ENSURE_SUCCESS(rv, rv); |
3940 NS_ENSURE_SUCCESS(rv, rv); |
3896 nsAutoCString baseDomain; |
3942 nsAutoCString baseDomain; |
3897 rv = GetBaseDomainFromHost(host, baseDomain); |
3943 rv = GetBaseDomainFromHost(host, baseDomain); |
3898 NS_ENSURE_SUCCESS(rv, rv); |
3944 NS_ENSURE_SUCCESS(rv, rv); |
3899 |
3945 |
3900 nsListIter iter; |
3946 nsListIter iter; |
3901 *aFoundCookie = FindCookie(DEFAULT_APP_KEY(baseDomain), host, name, path, iter); |
3947 *aFoundCookie = FindCookie(DEFAULT_APP_KEY(baseDomain), origin, host, name, path, iter); |
3902 return NS_OK; |
3948 return NS_OK; |
3903 } |
3949 } |
3904 |
3950 |
3905 // For a given base domain, find either an expired cookie or the oldest cookie |
3951 // For a given base domain, find either an expired cookie or the oldest cookie |
3906 // by lastAccessed time. |
3952 // by lastAccessed time. |
4103 } |
4149 } |
4104 |
4150 |
4105 // find an exact cookie specified by host, name, and path that hasn't expired. |
4151 // find an exact cookie specified by host, name, and path that hasn't expired. |
4106 bool |
4152 bool |
4107 nsCookieService::FindCookie(const nsCookieKey &aKey, |
4153 nsCookieService::FindCookie(const nsCookieKey &aKey, |
|
4154 const nsAFlatCString &aOrigin, |
4108 const nsAFlatCString &aHost, |
4155 const nsAFlatCString &aHost, |
4109 const nsAFlatCString &aName, |
4156 const nsAFlatCString &aName, |
4110 const nsAFlatCString &aPath, |
4157 const nsAFlatCString &aPath, |
4111 nsListIter &aIter) |
4158 nsListIter &aIter) |
4112 { |
4159 { |
4118 |
4165 |
4119 const nsCookieEntry::ArrayType &cookies = entry->GetCookies(); |
4166 const nsCookieEntry::ArrayType &cookies = entry->GetCookies(); |
4120 for (nsCookieEntry::IndexType i = 0; i < cookies.Length(); ++i) { |
4167 for (nsCookieEntry::IndexType i = 0; i < cookies.Length(); ++i) { |
4121 nsCookie *cookie = cookies[i]; |
4168 nsCookie *cookie = cookies[i]; |
4122 |
4169 |
4123 if (aHost.Equals(cookie->Host()) && |
4170 if (aOrigin.Equals(cookie->Origin()) && |
|
4171 aHost.Equals(cookie->Host()) && |
4124 aPath.Equals(cookie->Path()) && |
4172 aPath.Equals(cookie->Path()) && |
4125 aName.Equals(cookie->Name())) { |
4173 aName.Equals(cookie->Name())) { |
4126 aIter = nsListIter(entry, i); |
4174 aIter = nsListIter(entry, i); |
4127 return true; |
4175 return true; |
4128 } |
4176 } |