chrome-cookies-secure icon indicating copy to clipboard operation
chrome-cookies-secure copied to clipboard

wrong final block length after ugprading Google Chrome 131.0.6778.6

Open oliverhuynh opened this issue 1 year ago • 5 comments

Is anyone meet following error or only me?

node:internal/crypto/cipher:199
  const ret = this[kHandle].final();
                            ^

Error: error:1C80006B:Provider routines::wrong final block length
    at Decipheriv.final (node:internal/crypto/cipher:199:29)
    at decrypt (/home/oliver/localprojects/read_cookie/node_modules/chrome-cookies-secure/index.js:41:19)
    at Statement.<anonymous> (/home/oliver/localprojects/read_cookie/node_modules/chrome-cookies-secure/index.js:416:23) {
  library: 'Provider routines',
  reason: 'wrong final block length',
  code: 'ERR_OSSL_WRONG_FINAL_BLOCK_LENGTH'
}

I would be thankful for any hints to get this fixed

oliverhuynh avatar Nov 14 '24 18:11 oliverhuynh

+1

Related: Crypto bumped to 0.10. Fixable via similar method to this: https://stackoverflow.com/questions/21292142/decrypting-aes256-with-node-js-returns-wrong-final-block-length

elzii avatar Nov 15 '24 22:11 elzii

Same here

maelp avatar Dec 07 '24 14:12 maelp

Would this work?

function decrypt(key, encryptedData) {

	var decipher,
		decoded,
		final,
		padding,
		iv = new Buffer.from(new Array(KEYLENGTH + 1).join(' '), 'binary', 'utf-8');

	decipher = crypto.createDecipheriv('aes-128-cbc', key, iv);
	decipher.setAutoPadding(false);

	encryptedData = encryptedData.slice(3);

	decoded = decipher.update(encryptedData, 'binary', 'utf-8');

	final = decipher.final('utf-8');
	final.copy(decoded, decoded.length - 1);

	padding = decoded[decoded.length - 1];
	if (padding) {
		decoded = decoded.slice(0, decoded.length - padding);
	}

	return decoded.toString('utf8');
}

maelp avatar Dec 07 '24 14:12 maelp

The following approach worked for me, but I don't really understand exactly what I did, so use it with caution.

diff --git a/node_modules/chrome-cookies-secure/index.js b/node_modules/chrome-cookies-secure/index.js
index 882e958..d145a53 100644
--- a/node_modules/chrome-cookies-secure/index.js
+++ b/node_modules/chrome-cookies-secure/index.js
@@ -42,9 +42,7 @@ function decrypt(key, encryptedData) {
 	final.copy(decoded, decoded.length - 1);
 
 	padding = decoded[decoded.length - 1];
-	if (padding) {
-		decoded = decoded.slice(0, decoded.length - padding);
-	}
+	decoded = decoded.slice(32, decoded.length - padding);
 
 	return decoded.toString('utf8');
 }
@@ -396,7 +394,7 @@ const getCookies = async (uri, format, callback, profileOrPath) => {
 					}
 
 					if (cookie.value === '' && cookie.encrypted_value.length > 0) {
-						encryptedValue = cookie.encrypted_value;
+						encryptedValue = Buffer.from(cookie.encrypted_value, 'binary');
 
 						if (process.platform === 'win32') {
 							if (encryptedValue[0] == 0x01 && encryptedValue[1] == 0x00 && encryptedValue[2] == 0x00 && encryptedValue[3] == 0x00){

borodean avatar Jan 16 '25 18:01 borodean

hey folks - did some digging and Chrome made a change here: https://chromium-review.googlesource.com/c/chromium/src/+/5792044 that prepends a SHA256 hash of the domain name to the start of the encrypted data. Slicing off 32 chars should be right in most cases i can see, so @borodean that's why that works.

codebytere avatar Feb 07 '25 20:02 codebytere

https://github.com/bertrandom/chrome-cookies-secure/pull/66

rubengmurray avatar Feb 27 '25 21:02 rubengmurray

Should be fixed in 3.0.0

rubengmurray avatar Feb 28 '25 22:02 rubengmurray

@rubengmurray still getting this error in 3.0.0

lacherogwu avatar Mar 04 '25 14:03 lacherogwu

fwiw i'm not - it's working for me now after bumping

codebytere avatar Mar 04 '25 14:03 codebytere

@codebytere I'm still getting this error

node:internal/crypto/cipher:184
  const ret = this[kHandle].final();
                            ^

Error: error:1C80006B:Provider routines::wrong final block length
    at Decipheriv.final (node:internal/crypto/cipher:184:29)
    at decrypt (/Users/asaf/Desktop/nodejs/node_modules/chrome-cookies-secure/index.js:66:18)
    at Statement.<anonymous> (/Users/asaf/Desktop/nodejs/node_modules/chrome-cookies-secure/index.js:426:23) {
  library: 'Provider routines',
  reason: 'wrong final block length',
  code: 'ERR_OSSL_WRONG_FINAL_BLOCK_LENGTH'
}

Node.js v22.11.0

lacherogwu avatar Mar 04 '25 14:03 lacherogwu

In case anyone is still encountering that error, I created my version, which works only on macOS: chrome-cookie-decrypt.

lacherogwu avatar Mar 04 '25 18:03 lacherogwu

Care to share the site, and in which format they're failing?

I haven't tested on Node 22.

rubengmurray avatar Mar 04 '25 22:03 rubengmurray

@rubengmurray I created a PR with the fix. Please take a look. #68

lacherogwu avatar Mar 05 '25 00:03 lacherogwu

This has been fixed and can close issue(s). See #65 Thanks @rubengmurray & @IskenHuang 🍻

elzii avatar Mar 08 '25 18:03 elzii

There is a report of this not working on Apple Silicon in #68.

Is anyone on this issue seeing this working on Apple Silicon?

rubengmurray avatar Mar 09 '25 21:03 rubengmurray

https://github.com/bertrandom/chrome-cookies-secure/pull/69

Apple Silicon fix rolled in as @beta

3.0.0-beta.1

rubengmurray avatar Mar 19 '25 20:03 rubengmurray

seems working now

maelp avatar Mar 21 '25 16:03 maelp