Skip to content

Commit 3e13fbb

Browse files
authored
Merge pull request #6 from imgproxy/fix-url-safe-encoding
Added symbol replacement when encoding URLs
2 parents c25919c + 962a882 commit 3e13fbb

File tree

6 files changed

+47
-12
lines changed

6 files changed

+47
-12
lines changed

src/methods/generateImageInfoUrl.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, it } from "vitest";
22
import { OptionsImageInfo } from "@imgproxy/imgproxy-js-core";
33
import generateImageInfoUrl from "./generateImageInfoUrl";
44

5-
describe("generateImageInfourl", () => {
5+
describe("generateImageInfoUrl", () => {
66
it("should generate a valid URL", () => {
77
const options: OptionsImageInfo = {
88
average: { average: 1, ignore_transparent: "f" },
@@ -43,7 +43,7 @@ describe("generateImageInfourl", () => {
4343
});
4444

4545
expect(result).toBe(
46-
"https://imgproxy.example.com/info/insecure/bh:4:3/d:f/f:t/xmp:f/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZS5qcGc="
46+
"https://imgproxy.example.com/info/insecure/bh:4:3/d:f/f:t/xmp:f/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZS5qcGc"
4747
);
4848
});
4949

@@ -56,7 +56,7 @@ describe("generateImageInfourl", () => {
5656
});
5757

5858
expect(result).toBe(
59-
"https://imgproxy.example.com/info/S27LCUL9UqVzUUEh4PuP2fMuoszQetA6qj5T07tlmZ4/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZS5qcGc="
59+
"https://imgproxy.example.com/info/xOner18d7-LJwkl4bifXGbC1_4kZXxsPLnuuvsMtcWo/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZS5qcGc"
6060
);
6161
});
6262

src/methods/generateImageUrl.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("generateImageUrl", () => {
2222
});
2323

2424
expect(result).toBe(
25-
"https://imgproxy.example.com/OISSn9zHS-E-xpsQPcsvKaTLVJePQgHG3MvDBbvk5lU/el:t/f:png/g:no/h:300/rt:fit/w:300/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZS5qcGc="
25+
"https://imgproxy.example.com/zsdsCjFCqcAniKFCygMBToRh6l5jVZbL0bhrnnUGK58/el:t/f:png/g:no/h:300/rt:fit/w:300/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZS5qcGc"
2626
);
2727
});
2828

@@ -44,7 +44,7 @@ describe("generateImageUrl", () => {
4444
});
4545

4646
expect(result).toBe(
47-
"https://imgproxy.example.com/insecure/ar:t/cb:clear/ex:t/f:webp/g:noea:10:10/sa:10/w:300/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZS5qcGc="
47+
"https://imgproxy.example.com/insecure/ar:t/cb:clear/ex:t/f:webp/g:noea:10:10/sa:10/w:300/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZS5qcGc"
4848
);
4949
});
5050

@@ -57,7 +57,7 @@ describe("generateImageUrl", () => {
5757
});
5858

5959
expect(result).toBe(
60-
"https://imgproxy.example.com/S27LCUL9UqVzUUEh4PuP2fMuoszQetA6qj5T07tlmZ4/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZS5qcGc="
60+
"https://imgproxy.example.com/xOner18d7-LJwkl4bifXGbC1_4kZXxsPLnuuvsMtcWo/aHR0cHM6Ly9leGFtcGxlLmNvbS9pbWFnZS5qcGc"
6161
);
6262
});
6363

src/utils/bufferToBase64.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it } from "vitest";
2+
import bufferToBase64 from "./bufferToBase64";
3+
4+
describe("bufferToBase64", () => {
5+
it("should return a base64 string", () => {
6+
const result = bufferToBase64(Buffer.from("Hello, World!"));
7+
8+
expect(result).toBe("SGVsbG8sIFdvcmxkIQ");
9+
});
10+
11+
it("should return a base64 string without padding", () => {
12+
const result = bufferToBase64(Buffer.from("Hello, World!"));
13+
14+
expect(result).not.toContain("=");
15+
});
16+
17+
it("should return a base64 string with - instead of +", () => {
18+
const result = bufferToBase64(Buffer.from("Hello World!"));
19+
20+
expect(result).not.toContain("+");
21+
});
22+
23+
it("should return a base64 string with _ instead of /", () => {
24+
const result = bufferToBase64(Buffer.from("attached/attachment.jpg"));
25+
26+
expect(result).not.toContain("/");
27+
});
28+
});

src/utils/bufferToBase64.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const bufferToBase64 = (buffer: Buffer): string => {
2+
return buffer
3+
.toString("base64")
4+
.replace(/=/g, "")
5+
.replace(/\+/g, "-")
6+
.replace(/\//g, "_");
7+
};
8+
9+
export default bufferToBase64;

src/utils/getEncryptedUrl.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import crypto from "crypto";
22
import { ICryptPair } from "../types.js";
3+
import bufferToBase64 from "./bufferToBase64.js";
34
import withCache from "./withCache.js";
45

56
const getEncryptedUrl = (url: string, pair: ICryptPair): string => {
@@ -13,11 +14,7 @@ const getEncryptedUrl = (url: string, pair: ICryptPair): string => {
1314
"binary"
1415
);
1516

16-
return Buffer.concat([iv, encrypted])
17-
.toString("base64")
18-
.replace(/=/g, "")
19-
.replace(/\+/g, "-")
20-
.replace(/\//g, "_");
17+
return bufferToBase64(Buffer.concat([iv, encrypted]));
2118
};
2219

2320
const withCacheGetEncryptedUrl = withCache(getEncryptedUrl);

src/utils/normalizeUrl.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { URL } from "@imgproxy/imgproxy-js-core";
22
import type { IRawUrl } from "../types";
3+
import bufferToBase64 from "./bufferToBase64.js";
34
import getEncryptPair from "./getEncryptPair.js";
45
import getEncryptedUrl from "./getEncryptedUrl.js";
56

@@ -17,7 +18,7 @@ const normalizeUrl = ({ url, encryptKey, encryptIV }: INormalizeUrl): URL => {
1718

1819
//encoded url to base64
1920
if (changedUrl.type === "base64") {
20-
changedUrl.value = btoa(changedUrl.value);
21+
changedUrl.value = bufferToBase64(Buffer.from(changedUrl.value));
2122
}
2223

2324
//encrypting url

0 commit comments

Comments
 (0)