Skip to content

Commit 0bc358c

Browse files
committed
added a new param - encryptIV, removed a param - noEncription
1 parent 1e221fd commit 0bc358c

16 files changed

Lines changed: 104 additions & 99 deletions

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ It takes the following arguments:
6464
- `salt` (`string | undefined`) - (optional) the salt used to encode the URL. It must be a hex-encoded 16-byte string. This option overrides IMGPROXY_SALT environment variable from process.env for this call.
6565
- `key` (`string | undefined`) - (optional) the key used to encode the URL. It must be a hex-encoded 16-byte string. This option overrides IMGPROXY_KEY environment variable from process.env for this call.
6666
- `encryptKey` (`string | undefined`) - (optional, **PRO feature**) the key used to encrypt the URL. The key should be either 16, 24, or 32 bytes long for AES-128-CBC, AES-192-CBC, or AES-256-CBC, respectively. This option overrides IMGPROXY_SOURCE_URL_ENCRYPTION_KEY environment variable from process.env for this call. Actual only for plain url type.
67-
- `noEncription` (`boolean`) - (optional, **PRO feature**, default `false`) if true, the URL will not be encrypted. Actual only for plain url type. We strongly recommend to use encryption for url.
6867

6968
### generateImageInfoUrl
7069

@@ -83,4 +82,3 @@ It takes the following arguments:
8382
- `salt` (`string | undefined`) - (optional) the salt used to encode the URL. It must be a hex-encoded 16-byte string. This option overrides IMGPROXY_SALT from process.env for one request.
8483
- `key` (`string | undefined`) - (optional) the key used to encode the URL. It must be a hex-encoded 16-byte string. This option overrides IMGPROXY_KEY from process.env for one request.
8584
- `encryptKey` (`string | undefined`) - (optional, **PRO feature**) the key used to encrypt the URL. The key should be either 16, 24, or 32 bytes long for AES-128-CBC, AES-192-CBC, or AES-256-CBC, respectively. This option overrides IMGPROXY_SOURCE_URL_ENCRYPTION_KEY from process.env for one request. Actual only for plain url type.
86-
- `noEncription` (`boolean`) - (optional, **PRO feature**, default `false`) if true, the URL will not be encrypted. Actual only for plain url type. We strongly recommend to use encryption for url.

src/methods/generateImageInfoUrl.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import type { IGenerateImageInfoUrl } from "../types";
1919
* @param {string} [key] - (optional) hex-encoded key. This option overrides IMGPROXY_KEY from process.env for this request
2020
* @param {string} [encryptKey] - (optional, PRO feature) hex-encoded key for encrypting url. Actual only for plain url type.
2121
* This option overrides IMGPROXY_SOURCE_URL_ENCRYPTION_KEY from process.env for this request
22-
* @param {boolean} [noEncription=false] - (optional, PRO feature) actual only for plain url type. If true, url will not be encrypted.
23-
* We strongly recommend to use encryption for url. default: `false`
22+
** @param {string} [encryptIV] - (optional, PRO feature) hex-encoded 16-bytes length IV for encrypting url.
23+
* More details about IV you can read in [imgproxy docs](https://docs.imgproxy.net/usage/encrypting_source_url#iv-generation)
2424
*
2525
* @returns {string}
2626
*
@@ -46,9 +46,9 @@ const generateImageInfoUrl = ({
4646
salt,
4747
key,
4848
encryptKey,
49-
noEncription,
49+
encryptIV,
5050
}: IGenerateImageInfoUrl): string => {
51-
const changedUrl = normalizeUrl({ url, encryptKey, noEncription });
51+
const changedUrl = normalizeUrl({ url, encryptKey, encryptIV });
5252

5353
//generating url with options
5454
const optionsString = generateImageInfoUrlCore(changedUrl, options);

src/methods/generateImageUrl.test.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,4 @@ describe("generateImageUrl", () => {
8181

8282
expect(result).toContain("/enc/");
8383
});
84-
85-
it("shouldn't encrypt URL if noEncription is true", () => {
86-
const result = generateImageUrl({
87-
baseUrl: "https://imgproxy.example.com/",
88-
url: { value: "https://example.com/image.jpg", resultType: "plain" },
89-
salt: "520f986b998545b4785e0defbc4f3c1203f22de2374a3d53cb7a7fe9fea309c5",
90-
key: "943b421c9eb07c830af81030552c86009268de4e532ba2ee2eab8247c6da0881",
91-
encryptKey:
92-
"52dd01d54fcbd79ff247fcff1d2f200ce6b95546f960b084faa1d269fb95d600",
93-
noEncription: true,
94-
});
95-
96-
expect(result).toContain("/plain/");
97-
});
9884
});

src/methods/generateImageUrl.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import type { IGenerateImageUrl } from "../types";
1414
* or in Options types in imgproxy-js-core.d.ts
1515
* @param {string} [salt] - (optional) hex-encoded salt. This option overrides IMGPROXY_SALT from process.env for this request
1616
* @param {string} [key] - (optional) hex-encoded key. This option overrides IMGPROXY_KEY from process.env for this request
17-
* @param {string} [encryptKey] - (optional, PRO feature) hex-encoded key for encrypting url. Actual only for plain url type.
17+
* @param {string} [encryptKey] - (optional, PRO feature) hex-encoded key for encrypting url.
1818
* This option overrides IMGPROXY_SOURCE_URL_ENCRYPTION_KEY from process.env for this request
19-
* @param {boolean} [noEncription=false] - (optional, PRO feature) actual only for plain url type. If true, url will not be encrypted.
20-
* We strongly recommend to use encryption for url. default: `false`
19+
* @param {string} [encryptIV] - (optional, PRO feature) hex-encoded 16-bytes length IV for encrypting url.
20+
* More details about IV you can read in [imgproxy docs](https://docs.imgproxy.net/usage/encrypting_source_url#iv-generation)
2121
*
2222
* @returns {string}
2323
*
@@ -42,9 +42,9 @@ const generateImageUrl = ({
4242
salt,
4343
key,
4444
encryptKey,
45-
noEncription,
45+
encryptIV,
4646
}: IGenerateImageUrl): string => {
47-
const changedUrl = normalizeUrl({ url, encryptKey, noEncription });
47+
const changedUrl = normalizeUrl({ url, encryptKey, encryptIV });
4848

4949
//generating url with options
5050
const optionsString = generateUrl(changedUrl, options);

src/types.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@ import type {
44
URL,
55
} from "@imgproxy/imgproxy-js-core";
66

7-
export interface IMaybePair {
7+
export interface IMaybeSignPair {
88
salt: string | undefined;
99
key: string | undefined;
1010
}
1111

12-
export interface IPair {
12+
export interface ISignPair {
1313
salt: string;
1414
key: string;
1515
}
1616

17+
export interface ICryptPair {
18+
key: string;
19+
iv: string;
20+
}
21+
1722
export interface IRawUrl {
1823
value: string;
1924
resultType?: URL["type"];
@@ -25,7 +30,7 @@ interface BaseGenerateImageUrl {
2530
salt?: string;
2631
key?: string;
2732
encryptKey?: string;
28-
noEncription?: boolean;
33+
encryptIV?: string;
2934
}
3035

3136
export interface IGenerateImageInfoUrl extends BaseGenerateImageUrl {

src/utils/getEncryptKey.test.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,39 @@
1-
import { describe, expect, it } from "vitest";
2-
import getEncryptKey from "./getEncryptKey";
1+
import { describe, expect, it, vi } from "vitest";
2+
import crypto from "crypto";
3+
import getEncryptPair from "./getEncryptPair";
34

45
describe("getEncryptKey", () => {
56
it("should return undefined if key is not defined", () => {
6-
const result = getEncryptKey(undefined);
7+
const result = getEncryptPair(undefined);
78

89
expect(result).toBe(undefined);
910
});
1011

11-
it("should return String if key is defined", () => {
12-
const result = getEncryptKey(
12+
it("should return object with key and generated iv if key is defined", () => {
13+
const spy = vi.spyOn(crypto, "randomBytes");
14+
spy.mockImplementationOnce(() =>
15+
Buffer.from("e664535f505d3b6ae939fe1169b22e2d", "hex")
16+
);
17+
18+
const result = getEncryptPair(
1319
"1eb5b0e971ad7f45324c1bb15c947cb207c43152fa5c6c7f35c4f36e0c18e0f1"
1420
);
1521

16-
expect(typeof result).toBe("string");
22+
expect(result).toStrictEqual({
23+
key: "1eb5b0e971ad7f45324c1bb15c947cb207c43152fa5c6c7f35c4f36e0c18e0f1",
24+
iv: "e664535f505d3b6ae939fe1169b22e2d",
25+
});
26+
});
27+
28+
it("should return object with key and iv if they are defined", () => {
29+
const result = getEncryptPair(
30+
"1eb5b0e971ad7f45324c1bb15c947cb207c43152fa5c6c7f35c4f36e0c18e0f1",
31+
"e664535f505d3b6ae939fe1169b22e2d"
32+
);
33+
34+
expect(result).toStrictEqual({
35+
key: "1eb5b0e971ad7f45324c1bb15c947cb207c43152fa5c6c7f35c4f36e0c18e0f1",
36+
iv: "e664535f505d3b6ae939fe1169b22e2d",
37+
});
1738
});
1839
});

src/utils/getEncryptKey.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/utils/getEncryptPair.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import crypto from "crypto";
2+
import type { ICryptPair } from "../types";
3+
4+
const KEY = process.env.IMGPROXY_SOURCE_URL_ENCRYPTION_KEY;
5+
6+
const getEncryptPair = (
7+
key: string | undefined,
8+
genIv?: string | undefined
9+
): ICryptPair | undefined => {
10+
const k = key || KEY;
11+
12+
if (!k) return undefined;
13+
14+
return {
15+
key: k,
16+
iv: genIv ? genIv : crypto.randomBytes(16).toString("hex"),
17+
};
18+
};
19+
20+
export default getEncryptPair;

src/utils/getEncryptedUrl.test.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
import { describe, expect, it, vi } from "vitest";
2-
import crypto from "crypto";
1+
import { describe, expect, it } from "vitest";
32
import { getEncryptedUrl } from "./getEncryptedUrl";
43

54
describe("getEncryptedUrl", () => {
65
it("should return a valid encrypted URL", () => {
7-
const spy = vi.spyOn(crypto, "randomBytes");
8-
spy.mockImplementationOnce(() =>
9-
Buffer.from("e664535f505d3b6ae939fe1169b22e2d", "hex")
10-
);
11-
12-
const result = getEncryptedUrl(
13-
"https://example.com/image.jpg",
14-
"1eb5b0e971ad7f45324c1bb15c947cb207c43152fa5c6c7f35c4f36e0c18e0f1"
15-
);
6+
const result = getEncryptedUrl("https://example.com/image.jpg", {
7+
key: "1eb5b0e971ad7f45324c1bb15c947cb207c43152fa5c6c7f35c4f36e0c18e0f1",
8+
iv: "e664535f505d3b6ae939fe1169b22e2d",
9+
});
1610

1711
expect(result).toBe(
1812
"5mRTX1BdO2rpOf4RabIuLRo5XHgNeEqAfturvYUVzVXfh75f8b5ulIvbh2JawTzP"

src/utils/getEncryptedUrl.ts

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

4-
const getEncryptedUrl = (url: string, key: string): string => {
5-
const bufferKey = Buffer.from(key, "hex");
5+
const getEncryptedUrl = (url: string, pair: ICryptPair): string => {
6+
const bufferKey = Buffer.from(pair.key, "hex");
7+
const iv = Buffer.from(pair.iv, "hex");
68
const data = Buffer.from(url).toString("binary");
7-
const iv = crypto.randomBytes(16);
89
const cipher = crypto.createCipheriv("aes-256-cbc", bufferKey, iv);
910

1011
const encrypted = Buffer.from(

0 commit comments

Comments
 (0)