Skip to content

Commit 4559039

Browse files
Merge master into branch
2 parents dbfc4bb + 8908466 commit 4559039

File tree

8 files changed

+94
-12
lines changed

8 files changed

+94
-12
lines changed

lib-es5/api_client/execute_request.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
4+
35
// eslint-disable-next-line import/order
46
var config = require("../config");
57
var https = /^http:/.test(config().upload_prefix) ? require('http') : require('https');
@@ -74,6 +76,21 @@ function execute_request(method, params, auth, api_url, callback) {
7476
request_options.headers['Content-Length'] = Buffer.byteLength(query_params);
7577
}
7678
handle_response = function handle_response(res) {
79+
var _config = config(),
80+
_config$hide_sensitiv = _config.hide_sensitive,
81+
hide_sensitive = _config$hide_sensitiv === undefined ? false : _config$hide_sensitiv;
82+
83+
var sanitizedOptions = _extends({}, request_options);
84+
85+
if (hide_sensitive === true) {
86+
if ("auth" in sanitizedOptions) {
87+
delete sanitizedOptions.auth;
88+
}
89+
if ("Authorization" in sanitizedOptions.headers) {
90+
delete sanitizedOptions.headers.Authorization;
91+
}
92+
}
93+
7794
if (includes([200, 400, 401, 403, 404, 409, 420, 500], res.statusCode)) {
7895
var buffer = "";
7996
var error = false;
@@ -106,7 +123,7 @@ function execute_request(method, params, auth, api_url, callback) {
106123

107124
if (result.error) {
108125
deferred.reject(Object.assign({
109-
request_options,
126+
request_options: sanitizedOptions,
110127
query_params
111128
}, result));
112129
} else {
@@ -122,7 +139,7 @@ function execute_request(method, params, auth, api_url, callback) {
122139
error: {
123140
message: e,
124141
http_code: res.statusCode,
125-
request_options,
142+
request_options: sanitizedOptions,
126143
query_params
127144
}
128145
};
@@ -136,7 +153,7 @@ function execute_request(method, params, auth, api_url, callback) {
136153
error: {
137154
message: "Server returned unexpected status code - " + res.statusCode,
138155
http_code: res.statusCode,
139-
request_options,
156+
request_options: sanitizedOptions,
140157
query_params
141158
}
142159
};

lib-es5/utils/consts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@ module.exports = {
8585
UPLOAD_PREFIX,
8686
SUPPORTED_SIGNATURE_ALGORITHMS,
8787
DEFAULT_SIGNATURE_ALGORITHM
88-
};
88+
};

lib-es5/utils/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,12 @@ function generate_transformation_string(options) {
567567
options.start_offset = _split_range2[0];
568568
options.end_offset = _split_range2[1];
569569
}
570+
if (options.start_offset) {
571+
options.start_offset = normalize_expression(options.start_offset);
572+
}
573+
if (options.end_offset) {
574+
options.end_offset = normalize_expression(options.end_offset);
575+
}
570576
var overlay = process_layer(consumeOption(options, "overlay"));
571577
var radius = process_radius(consumeOption(options, "radius"));
572578
var underlay = process_layer(consumeOption(options, "underlay"));

lib/api_client/execute_request.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ function execute_request(method, params, auth, api_url, callback, options = {})
6666
request_options.headers['Content-Length'] = Buffer.byteLength(query_params);
6767
}
6868
handle_response = function (res) {
69+
const {hide_sensitive = false} = config();
70+
const sanitizedOptions = {...request_options};
71+
72+
if (hide_sensitive === true){
73+
if ("auth" in sanitizedOptions) { delete sanitizedOptions.auth; }
74+
if ("Authorization" in sanitizedOptions.headers) { delete sanitizedOptions.headers.Authorization; }
75+
}
76+
6977
if (includes([200, 400, 401, 403, 404, 409, 420, 500], res.statusCode)) {
7078
let buffer = "";
7179
let error = false;
@@ -98,7 +106,7 @@ function execute_request(method, params, auth, api_url, callback, options = {})
98106

99107
if (result.error) {
100108
deferred.reject(Object.assign({
101-
request_options,
109+
request_options: sanitizedOptions,
102110
query_params
103111
}, result));
104112
} else {
@@ -114,7 +122,7 @@ function execute_request(method, params, auth, api_url, callback, options = {})
114122
error: {
115123
message: e,
116124
http_code: res.statusCode,
117-
request_options,
125+
request_options: sanitizedOptions,
118126
query_params
119127
}
120128
};
@@ -128,7 +136,7 @@ function execute_request(method, params, auth, api_url, callback, options = {})
128136
error: {
129137
message: "Server returned unexpected status code - " + res.statusCode,
130138
http_code: res.statusCode,
131-
request_options,
139+
request_options: sanitizedOptions,
132140
query_params
133141
}
134142
};

lib/utils/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,12 @@ function generate_transformation_string(options) {
510510
if (options.offset != null) {
511511
[options.start_offset, options.end_offset] = split_range(consumeOption(options, "offset"));
512512
}
513+
if (options.start_offset) {
514+
options.start_offset = normalize_expression(options.start_offset);
515+
}
516+
if (options.end_offset) {
517+
options.end_offset = normalize_expression(options.end_offset);
518+
}
513519
let overlay = process_layer(consumeOption(options, "overlay"));
514520
let radius = process_radius(consumeOption(options, "radius"));
515521
let underlay = process_layer(consumeOption(options, "underlay"));

test/unit/config.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,35 @@ describe("config", function () {
2020
cloudinary.config(true);
2121
});
2222

23+
it("should not have a `hide_sensitive` property by default", function () {
24+
const config = cloudinary.config();
25+
expect(config).not.to.have.property("hide_sensitive");
26+
});
27+
28+
it("should support `hide_sensitive` config param", function () {
29+
const config = cloudinary.config({hide_sensitive: true});
30+
expect(config.hide_sensitive).to.eql(true)
31+
});
32+
33+
it("should hide API key and secret upon error when `hide_sensitive` is true", async function () {
34+
try {
35+
cloudinary.config({hide_sensitive: true});
36+
const result = await cloudinary.v2.api.resource("?");
37+
expect(result).fail();
38+
} catch (err) {
39+
expect(err.request_options).not.to.have.property("auth");
40+
}
41+
});
42+
43+
it("should hide Authorization header upon error when `hide_sensitive` is true", async function () {
44+
try {
45+
cloudinary.config({hide_sensitive: true});
46+
const result = await cloudinary.v2.api.resource("?", { oauth_token: 'irrelevant' });
47+
expect(result).fail();
48+
} catch (err) {
49+
expect(err.request_options.headers).not.to.have.property("Authorization");
50+
}
51+
});
2352

2453
it("should allow nested values in CLOUDINARY_URL", function () {
2554
process.env.CLOUDINARY_URL = "cloudinary://key:secret@test123?foo[bar]=value";

test/unit/normalize_expression_spec.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const cloudinary = require("../../cloudinary");
22
const createTestConfig = require('../testUtils/createTestConfig');
33
const helper = require("../spechelper");
4-
const { SIMPLE_PARAMS } = require(`../../${helper.libPath}/utils/consts`);
4+
const {SIMPLE_PARAMS} = require(`../../${helper.libPath}/utils/consts`);
5+
const {generate_transformation_string} = require("../../lib/utils");
56

67
describe("normalize_expression tests", function () {
78
beforeEach(function () {
@@ -12,7 +13,21 @@ describe("normalize_expression tests", function () {
1213
}));
1314
});
1415

15-
it("Expression normalization", function() {
16+
it('should normalize start_offset', function () {
17+
const result = generate_transformation_string({
18+
"start_offset": "idu - 5"
19+
});
20+
expect(result).to.equal("so_idu_sub_5");
21+
});
22+
23+
it('should normalize end_offset', function () {
24+
const result = generate_transformation_string({
25+
"end_offset": "idu - 5"
26+
});
27+
expect(result).to.equal("eo_idu_sub_5");
28+
});
29+
30+
it("Expression normalization", function () {
1631
const cases = {
1732
'null is not affected': [null, null],
1833
'None is not affected': ['None', 'None'],
@@ -61,8 +76,8 @@ describe("normalize_expression tests", function () {
6176
const value = "width * 2";
6277
const normalizedValue = "w_mul_2";
6378
const normalizedParams = ["angle", "aspect_ratio", "dpr", "effect", "height", "opacity", "quality", "radius",
64-
"width", "x", "y", "zoom"];
65-
const nonNormalizedParams = simpleTransformationParams.concat('overlay', 'underlay')
79+
"width", "x", "y", "zoom", "end_offset", "start_offset"];
80+
const nonNormalizedParams = simpleTransformationParams.concat('overlay', 'underlay').filter(param => !normalizedParams.includes(param));
6681
normalizedParams.forEach((param) => {
6782
it(`should normalize value in ${param}`, () => {
6883
// c_scale needed to test h_ and w_ parameters that are ignored without crop mode

types/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,7 @@ declare module 'cloudinary' {
508508
proxy?: string;
509509
public_id?: string;
510510
quality_analysis?: boolean;
511+
resource_type?: "image" | "video" | "raw" | "auto";
511512
responsive_breakpoints?: Record<any,any>;
512513
return_delete_token?: boolean
513514
timestamp?: number;
@@ -580,7 +581,7 @@ declare module 'cloudinary' {
580581
width: number;
581582
height: number;
582583
format: string;
583-
resource_type: string;
584+
resource_type?: "image" | "video" | "raw" | "auto";
584585
created_at: string;
585586
tags: Array<string>;
586587
pages: number;

0 commit comments

Comments
 (0)