mirror of
https://github.com/bitwarden/browser
synced 2025-12-11 22:03:36 +00:00
[PM-19798] [PM-18807] Fix base64 encoding/decoding with special characters (#14089)
* Refactor base64 encoding/decoding to use BufferLib * Add tests for base64 encoding and decoding functions --------- Co-authored-by: Justin Baur <19896123+justindbaur@users.noreply.github.com>
This commit is contained in:
@@ -706,4 +706,73 @@ describe("Utils Service", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("fromUtf8ToB64(...)", () => {
|
||||||
|
const originalIsNode = Utils.isNode;
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
Utils.isNode = originalIsNode;
|
||||||
|
});
|
||||||
|
|
||||||
|
runInBothEnvironments("should handle empty string", () => {
|
||||||
|
const str = Utils.fromUtf8ToB64("");
|
||||||
|
expect(str).toBe("");
|
||||||
|
});
|
||||||
|
|
||||||
|
runInBothEnvironments("should convert a normal b64 string", () => {
|
||||||
|
const str = Utils.fromUtf8ToB64(asciiHelloWorld);
|
||||||
|
expect(str).toBe(b64HelloWorldString);
|
||||||
|
});
|
||||||
|
|
||||||
|
runInBothEnvironments("should convert various special characters", () => {
|
||||||
|
const cases = [
|
||||||
|
{ input: "»", output: "wrs=" },
|
||||||
|
{ input: "¦", output: "wqY=" },
|
||||||
|
{ input: "£", output: "wqM=" },
|
||||||
|
{ input: "é", output: "w6k=" },
|
||||||
|
{ input: "ö", output: "w7Y=" },
|
||||||
|
{ input: "»»", output: "wrvCuw==" },
|
||||||
|
];
|
||||||
|
cases.forEach((c) => {
|
||||||
|
const utfStr = c.input;
|
||||||
|
const str = Utils.fromUtf8ToB64(utfStr);
|
||||||
|
expect(str).toBe(c.output);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("fromB64ToUtf8(...)", () => {
|
||||||
|
const originalIsNode = Utils.isNode;
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
Utils.isNode = originalIsNode;
|
||||||
|
});
|
||||||
|
|
||||||
|
runInBothEnvironments("should handle empty string", () => {
|
||||||
|
const str = Utils.fromB64ToUtf8("");
|
||||||
|
expect(str).toBe("");
|
||||||
|
});
|
||||||
|
|
||||||
|
runInBothEnvironments("should convert a normal b64 string", () => {
|
||||||
|
const str = Utils.fromB64ToUtf8(b64HelloWorldString);
|
||||||
|
expect(str).toBe(asciiHelloWorld);
|
||||||
|
});
|
||||||
|
|
||||||
|
runInBothEnvironments("should handle various special characters", () => {
|
||||||
|
const cases = [
|
||||||
|
{ input: "wrs=", output: "»" },
|
||||||
|
{ input: "wqY=", output: "¦" },
|
||||||
|
{ input: "wqM=", output: "£" },
|
||||||
|
{ input: "w6k=", output: "é" },
|
||||||
|
{ input: "w7Y=", output: "ö" },
|
||||||
|
{ input: "wrvCuw==", output: "»»" },
|
||||||
|
];
|
||||||
|
|
||||||
|
cases.forEach((c) => {
|
||||||
|
const b64Str = c.input;
|
||||||
|
const str = Utils.fromB64ToUtf8(b64Str);
|
||||||
|
expect(str).toBe(c.output);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -233,7 +233,7 @@ export class Utils {
|
|||||||
if (Utils.isNode) {
|
if (Utils.isNode) {
|
||||||
return Buffer.from(utfStr, "utf8").toString("base64");
|
return Buffer.from(utfStr, "utf8").toString("base64");
|
||||||
} else {
|
} else {
|
||||||
return decodeURIComponent(escape(Utils.global.btoa(utfStr)));
|
return BufferLib.from(utfStr, "utf8").toString("base64");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,7 +245,7 @@ export class Utils {
|
|||||||
if (Utils.isNode) {
|
if (Utils.isNode) {
|
||||||
return Buffer.from(b64Str, "base64").toString("utf8");
|
return Buffer.from(b64Str, "base64").toString("utf8");
|
||||||
} else {
|
} else {
|
||||||
return decodeURIComponent(escape(Utils.global.atob(b64Str)));
|
return BufferLib.from(b64Str, "base64").toString("utf8");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user