Skip to content

Commit a20cd0c

Browse files
added scrubbing to api function requestArgs (#27)
* added scrubbing to api function requestArgs * Changed iteration to go through keys, added scrubbing to scrubKeys, changed * to 8 * closed some holes and fixed some edge cases * cleaning the scrubbing * bumped version * changed Record to Partial<Record>
1 parent a31103f commit a20cd0c

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "polyapi",
3-
"version": "0.24.9",
3+
"version": "0.24.10",
44
"description": "Poly is a CLI tool to help create and manage your Poly definitions.",
55
"license": "MIT",
66
"repository": {

templates/api-index.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ const handleError = (err) => {
4949
};
5050
}
5151

52+
const scrub = (data) => {
53+
if (!data || typeof data !== 'object' ) return data;
54+
const secrets = ["x_api_key", "x-api-key", "access_token", "access-token", "authorization", "api_key", "api-key", "apikey", "accesstoken", "token", "password", "key"];
55+
if (Array.isArray(data)) {
56+
return data.map(item => scrub(item))
57+
}
58+
else {
59+
const temp = {};
60+
for (const key of Object.keys(data)) {
61+
if (typeof data[key] === 'object') {
62+
temp[key] = scrub(data[key]);
63+
} else if (secrets.includes(key.toLowerCase())) {
64+
temp[key] = "********";
65+
} else {
66+
temp[key] = data[key];
67+
}
68+
}
69+
return temp
70+
}
71+
}
72+
73+
5274
const executeApiFunction = (id, clientID, polyCustom, requestArgs) => {
5375
const requestServerStartTime = Date.now();
5476

@@ -78,7 +100,8 @@ const executeApiFunction = (id, clientID, polyCustom, requestArgs) => {
78100
try {
79101
responseData = JSON.stringify(data.data);
80102
} catch (err) {}
81-
console.error('Error executing api function with id:', id, 'Status code:', data.status, 'Request data:', requestArgs, 'Response data:', responseData);
103+
scrub(requestArgs)
104+
console.error('Error executing api function with id:', id, 'Status code:', data.status, 'Request data:', scrubbedArgs, 'Response data:', responseData);
82105
}
83106

84107
serverPreperationTimeMs = Number(polyHeaders['x-poly-execution-duration']);
@@ -96,6 +119,7 @@ const executeApiFunction = (id, clientID, polyCustom, requestArgs) => {
96119
})
97120
}).then(({ headers, data, status }) => {
98121
if (status && (status < 200 || status >= 300) && process.env.LOGS_ENABLED) {
122+
scrub(requestArgs)
99123
console.error('Error direct executing api function with id:', id, 'Status code:', status, 'Request data:', requestArgs, 'Response data:', data.data);
100124
}
101125
const apiExecutionTimeMs = Date.now() - requestApiStartTime;
@@ -127,6 +151,7 @@ const executeApiFunction = (id, clientID, polyCustom, requestArgs) => {
127151
try {
128152
responseData = JSON.stringify(data.data);
129153
} catch (err) {}
154+
scrub(requestArgs)
130155
console.error('Error executing api function with id:', id, 'Status code:', data.status, 'Request data:', requestArgs, 'Response data:', responseData);
131156
}
132157
const serverExecutionTimeMs = Number(headers['x-poly-execution-duration']);

templates/axios.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const https = require('https');
55
const dotenv = require('dotenv');
66
const polyCustom = require('./poly-custom');
77
const { API_KEY, API_BASE_URL } = require('./constants');
8+
import { scrub } from './api-index.js'
89

910
dotenv.config();
1011

@@ -43,13 +44,16 @@ axios.interceptors.request.use(
4344
);
4445

4546
const scrubKeys = (err) => {
46-
if (err.request && typeof err.request.headers === 'object' && err.request.headers.Authorization) {
47+
if (!err.request || typeof err.request.headers !== 'object') throw err
48+
const temp = scrub(err.request.headers)
49+
if (err.request.headers.Authorization) {
4750
// Scrub any credentials in the authorization header
4851
const [type, ...rest] = err.request.headers.Authorization.split(' ');
49-
err.request.headers.Authorization = rest.length && type
52+
temp.Authorization = rest.length && type
5053
? `${type} ********`
5154
: `********`;
5255
}
56+
err.request.headers = temp
5357
throw err;
5458
};
5559

templates/tabi/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ type PolyCountQuery<T extends Record<string, unknown>> = Clean<{
4343

4444
type PolySelectOneQuery<T extends Record<string, unknown>> = Clean<{
4545
where?: Where<T>;
46-
orderBy?: Record<keyof T, 'asc' | 'desc'>;
46+
orderBy?: Partial<Record<keyof T, 'asc' | 'desc'>>;
4747
}>;
4848

4949
type PolySelectManyQuery<T extends Record<string, unknown>> = Clean<{
5050
where?: Where<T>;
5151
limit?: number; // 1000 is max limit for now
5252
offset?: number;
53-
orderBy?: Record<keyof T, 'asc' | 'desc'>;
53+
orderBy?: Partial<Record<keyof T, 'asc' | 'desc'>>;
5454
}>;
5555

5656
type PolyDeleteQuery<T extends Record<string, unknown>> = Clean<{

0 commit comments

Comments
 (0)