From da5b64abb08400c1dc8262e69a045ad132c56bee Mon Sep 17 00:00:00 2001 From: Pacalini <141402887+Pacalini@users.noreply.github.com> Date: Sat, 29 Nov 2025 14:21:30 +0800 Subject: [PATCH] interceptor: mask log (#618) --- assets/init.js | 33 +++++++++++++++++++++------------ lib/foundation/js_engine.dart | 6 +++++- lib/network/app_dio.dart | 21 +++++++++++++++++++-- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/assets/init.js b/assets/init.js index 1196181..a2e1da8 100644 --- a/assets/init.js +++ b/assets/init.js @@ -465,9 +465,10 @@ let Network = { * @param {string} url - The URL to send the request to. * @param {Object} headers - The headers to include in the request. * @param data - The data to send with the request. + * @param {Object} extra - Extra options to pass to the interceptor. * @returns {Promise<{status: number, headers: {}, body: ArrayBuffer}>} The response from the request. */ - async fetchBytes(method, url, headers, data) { + async fetchBytes(method, url, headers, data, extra) { let result = await sendMessage({ method: 'http', http_method: method, @@ -475,6 +476,7 @@ let Network = { url: url, headers: headers, data: data, + extra: extra, }); if (result.error) { @@ -490,15 +492,17 @@ let Network = { * @param {string} url - The URL to send the request to. * @param {Object} headers - The headers to include in the request. * @param data - The data to send with the request. + * @param {Object} extra - Extra options to pass to the interceptor. * @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request. */ - async sendRequest(method, url, headers, data) { + async sendRequest(method, url, headers, data, extra) { let result = await sendMessage({ method: 'http', http_method: method, url: url, headers: headers, data: data, + extra: extra, }); if (result.error) { @@ -512,10 +516,11 @@ let Network = { * Sends an HTTP GET request. * @param {string} url - The URL to send the request to. * @param {Object} headers - The headers to include in the request. + * @param {Object} extra - Extra options to pass to the interceptor. * @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request. */ - async get(url, headers) { - return this.sendRequest('GET', url, headers); + async get(url, headers, extra) { + return this.sendRequest('GET', url, headers, extra); }, /** @@ -523,10 +528,11 @@ let Network = { * @param {string} url - The URL to send the request to. * @param {Object} headers - The headers to include in the request. * @param data - The data to send with the request. + * @param {Object} extra - Extra options to pass to the interceptor. * @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request. */ - async post(url, headers, data) { - return this.sendRequest('POST', url, headers, data); + async post(url, headers, data, extra) { + return this.sendRequest('POST', url, headers, data, extra); }, /** @@ -534,10 +540,11 @@ let Network = { * @param {string} url - The URL to send the request to. * @param {Object} headers - The headers to include in the request. * @param data - The data to send with the request. + * @param {Object} extra - Extra options to pass to the interceptor. * @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request. */ - async put(url, headers, data) { - return this.sendRequest('PUT', url, headers, data); + async put(url, headers, data, extra) { + return this.sendRequest('PUT', url, headers, data, extra); }, /** @@ -545,20 +552,22 @@ let Network = { * @param {string} url - The URL to send the request to. * @param {Object} headers - The headers to include in the request. * @param data - The data to send with the request. + * @param {Object} extra - Extra options to pass to the interceptor. * @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request. */ - async patch(url, headers, data) { - return this.sendRequest('PATCH', url, headers, data); + async patch(url, headers, data, extra) { + return this.sendRequest('PATCH', url, headers, data, extra); }, /** * Sends an HTTP DELETE request. * @param {string} url - The URL to send the request to. * @param {Object} headers - The headers to include in the request. + * @param {Object} extra - Extra options to pass to the interceptor. * @returns {Promise<{status: number, headers: {}, body: string}>} The response from the request. */ - async delete(url, headers) { - return this.sendRequest('DELETE', url, headers); + async delete(url, headers, extra) { + return this.sendRequest('DELETE', url, headers, extra); }, /** diff --git a/lib/foundation/js_engine.dart b/lib/foundation/js_engine.dart index 290e4fb..efe6c32 100644 --- a/lib/foundation/js_engine.dart +++ b/lib/foundation/js_engine.dart @@ -217,6 +217,7 @@ class JsEngine with _JSEngineApi, JsUiApi, Init { try { var headers = Map.from(req["headers"] ?? {}); + var extra = Map.from(req["extra"] ?? {}); if (headers["user-agent"] == null && headers["User-Agent"] == null) { headers["User-Agent"] = webUA; } @@ -244,7 +245,10 @@ class JsEngine with _JSEngineApi, JsUiApi, Init { responseType: req["bytes"] == true ? ResponseType.bytes : ResponseType.plain, - headers: headers)); + headers: headers, + extra: extra, + ) + ); } catch (e) { error = e.toString(); } diff --git a/lib/network/app_dio.dart b/lib/network/app_dio.dart index c134ffe..4d5549d 100644 --- a/lib/network/app_dio.dart +++ b/lib/network/app_dio.dart @@ -96,11 +96,28 @@ class MyLogInterceptor implements Interceptor { @override void onRequest(RequestOptions options, RequestInterceptorHandler handler) { + const String headerMask = "********"; + const String dataMask = "****** DATA_PROTECTED ******"; Log.info( "Network", "${options.method} ${options.uri}\n" - "headers:\n${options.headers}\n" - "data:\n${options.data}"); + "headers:\n${ + options.extra.containsKey("maskHeadersInLog") + ? options.headers.map((key, value) => + MapEntry( + key, + options.extra["maskHeadersInLog"].contains(key) + ? headerMask + : value + )) + : options.headers + }\n" + "data:\n${ + options.extra["maskDataInLog"] == true + ? dataMask + : options.data + }" + ); options.connectTimeout = const Duration(seconds: 15); options.receiveTimeout = const Duration(seconds: 15); options.sendTimeout = const Duration(seconds: 15);