test_remove_invalid_first_party_partitioned_cookie.js (5115B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 // The test ensure we remove first-party partitioned cookies that don't have 5 // partitioned attribute. 6 7 add_task(async function run_test() { 8 // Set up a profile. 9 let profile = do_get_profile(); 10 11 // Start the cookieservice, to force creation of a database. 12 Services.cookies.sessionCookies; 13 14 // Close the profile. 15 await promise_close_profile(); 16 17 // Remove the cookie file in order to create another database file. 18 do_get_cookie_file(profile).remove(false); 19 20 // Create a schema 14 database. 21 let schema14db = new CookieDatabaseConnection( 22 do_get_cookie_file(profile), 23 15 24 ); 25 26 let now = Math.round(Date.now() / 1000); 27 28 // Create an invalid first-party partitioned cookie. 29 let invalidFPCookie = new Cookie( 30 "invalid", 31 "bad", 32 "example.com", 33 "/", 34 now + 34560000, 35 now, 36 now, 37 false, // isSession 38 true, // isSecure 39 false, // isHttpOnly 40 false, // isBrowserElement 41 { partitionKey: "(https,example.com)" }, 42 Ci.nsICookie.SAMESITE_UNSET, 43 Ci.nsICookie.SCHEME_UNSET, 44 false // isPartitioned 45 ); 46 schema14db.insertCookie(invalidFPCookie); 47 48 // Create a valid first-party partitioned cookie(CHIPS). 49 let valid1stCHIPS = new Cookie( 50 "valid1stCHIPS", 51 "good", 52 "example.com", 53 "/", 54 now + 34560000, 55 now, 56 now, 57 false, // isSession 58 true, // isSecure 59 false, // isHttpOnly 60 false, // isBrowserElement 61 { partitionKey: "(https,example.com)" }, 62 Ci.nsICookie.SAMESITE_UNSET, 63 Ci.nsICookie.SCHEME_UNSET, 64 true // isPartitioned 65 ); 66 schema14db.insertCookie(valid1stCHIPS); 67 68 // Create a valid unpartitioned cookie. 69 let unpartitionedCookie = new Cookie( 70 "valid", 71 "good", 72 "example.com", 73 "/", 74 now + 34560000, 75 now, 76 now, 77 false, // isSession 78 true, // isSecure 79 false, // isHttpOnly 80 false, // isBrowserElement 81 {}, 82 Ci.nsICookie.SAMESITE_UNSET, 83 Ci.nsICookie.SCHEME_UNSET, 84 false // isPartitioned 85 ); 86 schema14db.insertCookie(unpartitionedCookie); 87 88 // Create valid third-party partitioned TCP cookie. 89 let valid3rdTCPCookie = new Cookie( 90 "valid3rdTCP", 91 "good", 92 "example.com", 93 "/", 94 now + 34560000, 95 now, 96 now, 97 false, // isSession 98 true, // isSecure 99 false, // isHttpOnly 100 false, // isBrowserElement 101 { partitionKey: "(https,example.org)" }, 102 Ci.nsICookie.SAMESITE_UNSET, 103 Ci.nsICookie.SCHEME_UNSET, 104 false // isPartitioned 105 ); 106 schema14db.insertCookie(valid3rdTCPCookie); 107 108 // Create valid third-party partitioned CHIPS cookie. 109 let valid3rdCHIPSCookie = new Cookie( 110 "valid3rdCHIPS", 111 "good", 112 "example.com", 113 "/", 114 now + 34560000, 115 now, 116 now, 117 false, // isSession 118 true, // isSecure 119 false, // isHttpOnly 120 false, // isBrowserElement 121 { partitionKey: "(https,example.org)" }, 122 Ci.nsICookie.SAMESITE_UNSET, 123 Ci.nsICookie.SCHEME_UNSET, 124 true // isPartitioned 125 ); 126 schema14db.insertCookie(valid3rdCHIPSCookie); 127 128 schema14db.close(); 129 schema14db = null; 130 131 // Check if we have the right testing entries 132 { 133 const dbConnection = Services.storage.openDatabase( 134 do_get_cookie_file(profile) 135 ); 136 const stmt = dbConnection.createStatement( 137 "SELECT count(name) FROM moz_cookies WHERE host = 'example.com';" 138 ); 139 const success = stmt.executeStep(); 140 Assert.ok(success); 141 142 const count = stmt.getInt32(0); 143 Assert.equal(count, 5); 144 stmt.finalize(); 145 dbConnection.close(); 146 } 147 148 // Reload profile. 149 await promise_load_profile(); 150 151 // Check the number of unpartitioned cookies is correct, and we only have 152 // good cookies. 153 let cookies = Services.cookies.getCookiesFromHost("example.com", {}); 154 Assert.equal(cookies.length, 1); 155 for (const cookie of cookies) { 156 Assert.equal(cookie.value, "good"); 157 } 158 159 // Check the number of first-party partitioned cookies is correct, and we only 160 // have good cookies. 161 cookies = Services.cookies.getCookiesFromHost("example.com", { 162 partitionKey: "(https,example.com)", 163 }); 164 Assert.equal(cookies.length, 1); 165 for (const cookie of cookies) { 166 Assert.equal(cookie.value, "good"); 167 } 168 169 // Check the number of third-party partitioned cookies is correct, and we only 170 // have good cookies. 171 cookies = Services.cookies.getCookiesFromHost("example.com", { 172 partitionKey: "(https,example.org)", 173 }); 174 Assert.equal(cookies.length, 2); 175 for (const cookie of cookies) { 176 Assert.equal(cookie.value, "good"); 177 } 178 179 // Ensure the invalid cookies is gone in the DB. 180 { 181 const dbConnection = Services.storage.openDatabase( 182 do_get_cookie_file(profile) 183 ); 184 const stmt = dbConnection.createStatement( 185 "SELECT count(name) FROM moz_cookies WHERE value = 'bad';" 186 ); 187 const success = stmt.executeStep(); 188 Assert.ok(success); 189 190 const count = stmt.getInt32(0); 191 Assert.equal(count, 0); 192 stmt.finalize(); 193 dbConnection.close(); 194 } 195 196 // Cleanup 197 Services.cookies.removeAll(); 198 do_close_profile(); 199 });