Skip to content

Commit

Permalink
improve log interceptor (cfug#1780)
Browse files Browse the repository at this point in the history
<!-- Write down your pull request descriptions. -->

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [x] I have added the required tests to prove the fix/feature I'm
adding
- [x] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package


### Motivation 
When running the app on systems focused on application analysis, such as
[BrowserStack](https://www.browserstack.com) , same in profile mode ,
the request logs can leak unwanted information, even if the developer
has the option to remove the print, most of them are not aware of these
"leaks" of requests.

This opens the way for an attacker who has an apk , aab or any other
file that the browserStack
accept, be able to run the app on the website and view the logs

So I suggest the inclusion of this little code that by default only
executes the prints in debug mode
<!-- Provide more context and info about the PR. -->

---------

Signed-off-by: Lucas Matheus <[email protected]>
Signed-off-by: Alex Li <[email protected]>
Co-authored-by: LucasMatheusDev <[email protected]>
Co-authored-by: Jonas Uekötter <[email protected]>
Co-authored-by: Alex Li <[email protected]>
  • Loading branch information
4 people authored May 9, 2023
1 parent 35d921e commit b71f8c5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

*None.*
- Make `LogInterceptor` prints in DEBUG mode (when the assertion is enabled) by default.

## 5.1.2

Expand Down
4 changes: 3 additions & 1 deletion dio/README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,9 @@ print(response.data); // 'fake data'
dio.interceptors.add(LogInterceptor(responseBody: false)); // 不输出响应内容体
```

注意:由于拦截器队列是先进先出,`LogInterceptor` 应当在最后添加至 `Dio` 实例。
**注意:** 由于拦截器队列是先进先出,`LogInterceptor` 应当在最后添加至 `Dio` 实例。

**注意:** 日志默认仅在 DEBUG 模式(启用了断言)下打印。

### 自定义拦截器

Expand Down
6 changes: 4 additions & 2 deletions dio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -507,13 +507,15 @@ For the complete code see [here](../example/lib/queued_interceptor_crsftoken.dar

#### LogInterceptor

You can apply the `LogInterceptor` to log requests and responses automatically:
You can apply the `LogInterceptor` to log requests and responses automatically in the DEBUG mode:

```dart
dio.interceptors.add(LogInterceptor(responseBody: false)); // Do not output responses body.
```

Note: `LogInterceptor` should be the last to add since the interceptors are FIFO.
**Note:** `LogInterceptor` should be the last to add since the interceptors are FIFO.

**Note:** Logs will only be printed in the DEBUG mode (when the assertion is enabled).

#### Custom Interceptor

Expand Down
9 changes: 8 additions & 1 deletion dio/lib/src/interceptors/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class LogInterceptor extends Interceptor {
this.responseHeader = true,
this.responseBody = false,
this.error = true,
this.logPrint = print,
this.logPrint = _debugPrint,
});

/// Print request [Options]
Expand Down Expand Up @@ -132,3 +132,10 @@ class LogInterceptor extends Interceptor {
msg.toString().split('\n').forEach(logPrint);
}
}

void _debugPrint(Object? object) {
assert(() {
print(object);
return true;
}());
}

0 comments on commit b71f8c5

Please sign in to comment.