ChuckerTeam / chucker

🔎 An HTTP inspector for Android & OkHTTP (like Charles but on device)
Apache License 2.0
3.92k stars 341 forks source link

Ignore domains and paths from interceptor #1236

Closed VenomVendor closed 2 months ago

VenomVendor commented 3 months ago

:warning: Is your feature request related to a problem? Please describe

:bulb: Describe the solution you'd like

:bar_chart: Describe alternatives you've considered

:page_facing_up: Additional context

Initially I considered String over regex. But, regex gives more flexibility to the user to ignore more with less. ex: ".*(jpg|jpeg|png|gif|webp)$".toRegex() in one. If this was string .jpg, .jpeg, .png, .gif, .webp would need 5 loops.

* "not/very/important/in/here" // <-- Ingore * "not/very/important/in/here/not/important/at/all", // <-- Ignore * "not/very/important/in/here/but/important" // <-- DO NOT INGORE * "not/very/important/in/here/also/but/many-be/important/req" // <-- DO NOT INGORE

Regex("""^not/very/important/in/here(?!/but.*important).*""")

:raising_hand: Do you want to develop this feature yourself?

cortinico commented 3 months ago

Provide fun skipDomains(vararg pattern: kotlin.text.Regex) to skip the domains user are not intereseted in tapping.

This sounds reasonable to add.

overload fun skipPaths(vararg pattern: kotlin.text.Regex) with Regex, this way user can skip parts of path that they are not interested.

This feels like an overkill to me and not something we probably want to maintain/test, etc.

VenomVendor commented 3 months ago

Ignoring the path is more important for me than domian.

/dynamic/path/from/dam/assets/my-image.jpg /dynamic/path/from/dam/assets/my-image.png

  • This is where it all started, couldn't find an api to ignore image requests.

We would be receiving the Regex from App, hence we only call regex.matches on path. Did an initial draft, we're adding 5 lines in source for path check.

diff --git a/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt b/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt
index 10a6e9c..7e0f03f 100644
--- a/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt
+++ b/library/src/main/kotlin/com/chuckerteam/chucker/api/ChuckerInterceptor.kt
@@ -56,6 +56,7 @@ public class ChuckerInterceptor private constructor(
         )

     private val skipPaths = builder.skipPaths.toSet()
+    private val skipPathsRegex = builder.skipPathsRegex.toSet()

     init {
         if (builder.createShortcut) {
@@ -72,8 +73,9 @@ public class ChuckerInterceptor private constructor(
     override fun intercept(chain: Interceptor.Chain): Response {
         val transaction = HttpTransaction()
         val request = chain.request()
-        val shouldProcessTheRequest = !skipPaths.any { it == request.url.encodedPath }
-        if (shouldProcessTheRequest) {
+        val shouldSkipPath = skipPaths.contains(request.url.encodedPath) || skipPathsRegex.any { it.matches(request.url.encodedPath) }
+
+        if (!shouldSkipPath) {
             requestProcessor.process(request, transaction)
         }
         val response =
@@ -84,7 +86,7 @@ public class ChuckerInterceptor private constructor(
                 collector.onResponseReceived(transaction)
                 throw e
             }
-        return if (shouldProcessTheRequest) {
+        return if (!shouldSkipPath) {
             responseProcessor.process(response, transaction)
         } else {
             response
@@ -105,7 +107,8 @@ public class ChuckerInterceptor private constructor(
         internal var headersToRedact = emptySet<String>()
         internal var decoders = emptyList<BodyDecoder>()
         internal var createShortcut = true
-        internal var skipPaths = mutableSetOf<String>()
+        internal val skipPaths = mutableSetOf<String>()
+        internal val skipPathsRegex = mutableSetOf<Regex>()

         /**
          * Sets the [ChuckerCollector] to customize data retention.
@@ -195,6 +198,11 @@ public class ChuckerInterceptor private constructor(
                 }
             }

+        public fun skipPaths(vararg skipPaths: Regex): Builder =
+            apply {
+                this@Builder.skipPathsRegex.addAll(skipPaths.toSet())
+            }
+
         /**
          * Creates a new [ChuckerInterceptor] instance with values defined in this builder.
          */

regex-path-domain.patch

cortinico commented 3 months ago

Ignoring the path is more important for me than domian.

Can you give me some real world examples of paths you want to ignore with Regex just to understand your use case?

VenomVendor commented 3 months ago

Some of the domain for these paths are well known, sometimes they are dynamically named by cloud provider. ex: CloudFront

Possible Paths for the images.

/assets/lob-type/module-name/year/feature-name/tile_type_tile_name-sizeX-image-name.jpg
/assets/lob-type/module-name/year/feature-name/tile_type_tile_name-sizeX-image-name.png
/assets/lob-type/module-name/year/feature-name/tile_type_tile_name-sizeX-image-name.gif

/content/dam/my-company/creative/type/feature/name_lang_mobile_2x.jpeg

/contentful/random/folder/name_lang_mobile_2x.webp

/b5912c198d289129248318eaeb9c4000ba42ac6e/2018/10/15/my-intro-image-1024x624.png

/lob/product-pages/tile_type_tile_name_01_image-name.b5912c198d289129248318eaeb9c4000ba42ac6e.png

Ignore the below info, it is not relavant. ~~"not/very/important/in/here" // <-- Ingore "not/very/important/in/here/not/important/at/all", // <-- Ignore "not/very/important/in/here/but/important" // <-- DO NOT INGORE "not/very/important/in/here/also/but/many-be/important/req" // <-- DO NOT INGORE Regex("""^not/very/important/in/here(?!/but.important).""")~~

cortinico commented 3 months ago

Other than:

/b5912c198d289129248318eaeb9c4000ba42ac6e/2018/10/15/my-intro-image-1024x624.png

all the others can be easily achieved with skipPaths, no?

What would be your skipPaths regex then? Something like *.png?

VenomVendor commented 3 months ago

all the others can be easily achieved with skipPaths, no?

To confirm this statement, do you mean existing skipPaths(... String) or proposed skipPaths(... Regex)?

Regex for skipPaths(... Regex) would be ".*(jpg|jpeg|png|gif|webp)$".toRegex()

cortinico commented 3 months ago

To confirm this statement, do you mean existing skipPaths(... String) or proposed skipPaths(... Regex)?

I meant the already existing skipPaths(... String)

Anyway, I think it's reasonable to add skipPaths(... Regex) to handle scenarios like the one you presented 👍 Would you be up for sending a PR?