Skip to content

devtools: limit size of response bodies sent to the network tab - #48909

Merged
jdm merged 1 commit into
servo:mainfrom
sabbCodes:issue-48891
Nov 11, 2026
Merged

jdm merged 1 commit into
servo:mainfrom
sabbCodes:issue-48891

Conversation

@sabbCodes

@sabbCodes sabbCodes commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Limited size of response bodies sent to the network tab similar to how firefox handles it as suggested

Testing: ./mach test-unit -p servo-net and ./mach test-unit -p servo-net -- test_devtools_response_body_truncated were clean

Fixes: #48891

@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Oct 14, 2026
@freyacodes

Copy link
Copy Markdown
Member

I'm happy to review this, though it might be a few days before I have the time

@sabbCodes

Copy link
Copy Markdown
Contributor Author

I'm happy to review this, though it might be a few days before I have the time

Take you time :)

@Narfinger

Copy link
Copy Markdown
Contributor

So I am going to keep this discussion here. I think we should combine these changes with the general resource exhaustion.
I think having in the try_fold just check the current response_body_accumalator size and then return an error if it is too large should be fine. Or am I missing something? Because that would fix the response body in general and also devtools.

@freyacodes

Copy link
Copy Markdown
Member

Did you see #48894 ?

Are you suggesting that we should have an overall size limit in fetch, resulting in a network error?

I think we should definitely have some kind of limit. For instance, we likely shouldn't have very large entries in the cache.

But a blanket limit would mean that we wouldn't be able to download very large files, which goes through fetch as well. SRI complicates the picture as well, as we'd need the full body before processing it I think. It's worth looking at what other browsers do.

I still think the special treatment for Devtools is appropriate here, though I haven't read the PR yet.

@freyacodes

Copy link
Copy Markdown
Member

I'm struggling to manually test this PR. There seems to be a preexisting issue where the response body in devtools is empty, at in some cases. Did you manually test that responses are truncated? If so, how?

@sabbCodes

sabbCodes commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

I'm struggling to manually test this PR. There seems to be a preexisting issue where the response body in devtools is empty, at in some cases. Did you manually test that responses are truncated? If so, how?

Yes I did.

I think the way I did it was, I created a temporary file that was larger than the recommended size to be truncated (1MB I think)- it contains numbers from 1 to 255.

I ran the file through Servo and open Firefox devtools network tab where I check the response tab.

The fix worked and only 1 to 133? came in response - meaning the truncation worked.

These are some of the screenshots I took then - luckily :)
IMG_20261020_133617_587
IMG_20261020_133158_794

@sabbCodes

Copy link
Copy Markdown
Contributor Author

It's been days since I worked on it and I've had to work on several other issues, so I can't remember everything exactly, as I also stepped away from my PC

@freyacodes freyacodes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yesterday I tried doing something alike to what you did, though I wasn't getting the body at all. I could see that I was getting scripts and SVGs on webpages, and I realised that I could test it using a small limit (e.g. 10 bytes)

./mach run --devtools 6080 --pref devtools_network_response_body_limit=10`
Image

However, it's clear that servo doesn't seem to expose the flag that tells Firefox that a response is truncated. The point of tracking whether a response is truncated is to be able to alert the user. I realise that this might not have been too clear from the issue description. Truncated responses should look something like this:

Image

I don't know how Firefox expects to find the truncation flag, that might require a bit of RE or reading the source. Ideally the flag would be checked by a Python devtools test, though this might be tricky with how glitchy the network tab is about showing response bodies currently.

Comment thread components/net/devtools.rs Outdated
return;
}
let remaining = (limit as usize)
.saturating_add(1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this +1?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I don't know how i missed this.

I'm not sure I remember exactly why I added it, I think it's added during debugging to increase the limit size by 1 byte.

I'm going to remove it now.

Comment thread components/net/fetch/methods.rs Outdated
request,
context,
response,
Some(capped_body_for_devtools(vec)),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with send_response_to_devtools, but at first glance it appears to not communicate whether a response is truncated

Comment thread components/net/devtools.rs Outdated
Comment on lines +175 to +176
body: Option<Vec<u8>>,
body_truncated: bool,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

body_truncated is meaningless if body is None. I think it would be fitting to merge body and body_truncated into one Option<T> where T is a struct representing both. Both here and elsewhere.

@servo-highfive servo-highfive added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Oct 21, 2026
@jdm

jdm commented Oct 21, 2026

Copy link
Copy Markdown
Member

@sabbCodes

sabbCodes commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Based on https://searchfox.org/firefox-main/source/devtools/shared/specs/network-event.js#154 I think you want to add a truncated bool to https://github.com/servo/servo/blob/main/components/devtools/actors/network_event.rs#L616

Thank you for this, was really helpful.

I've been able to implement & manually test the truncation properly. Remains the communication of the truncation flag.

image

Maybe I'll push what I have while I work on that.

@servo-highfive servo-highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Oct 22, 2026
@sabbCodes

Copy link
Copy Markdown
Contributor Author

As the banner refused to render despite servo truncating properly, I had to look into how firefox handles it so I can reverse-engineer the method.

I found how the banner is triggered here which led to discovering this and here's how it's been watched

I realised my previous approach was flawed, so I addressed both the tracking of the truncation state in net and the protocol serialization in devtools and created DevtoolsBodyBuffer.

All tests still pass, checks are clean, and banner is being rendered in the network response tab.
Screenshot from 2026-10-23 02-47-13

@sabbCodes

Copy link
Copy Markdown
Contributor Author

To manually test:

I created a large file (2MB) like so; python3 -c "open('large_response.txt', 'w').write('A' * 2 * 1024 * 1024)" then a test_truncation.html file like so;

<html>
<body>
  <h1>Devtools Truncation Test</h1>
  <button id="btn">Fetch Data</button>
  <pre id="output"></pre>
  <script>
    document.getElementById('btn').onclick = async () => {
      try {
        const res = await fetch('large_response.txt');
        const text = await res.text();
        document.getElementById('output').textContent = "Fetched " + text.length + " bytes";
      } catch (e) {
        document.getElementById('output').textContent = "Fetch failed: " + e;
      }
    };
  </script>
</body>
</html>

Then ran the servo build like so; ./mach run --devtools 7000 --pref devtools_network_response_body_limit=1000 http://localhost:8000/test_truncation.html

// When the body was truncated, report the true content size from the
// Content-Length header (i.e., the full original size) so that Firefox's
// ResponsePanel can compare `content.size >= limit` and display the
// "Response has been truncated" message, exactly as Firefox does.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// "Response has been truncated" message, exactly as Firefox does.
// "Response has been truncated" message

This is a confusing comment because it is still Firefox making that decision

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that too, but I wasn't sure, I thought we're just borrowing Firefox's devtools to render ours.

Still a lot to understand here, thank you.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. To elaborate: The Firefox desktop browser ships devtools as a client and a server. These are usually used together in the same application. However, the desktop version can also be used to connect to other running instances of Firefox as a client, such as to the android version, over the network. We take advantage of their preexisting client to communicate with our client, which is the piece of code that we are working with. This saves us from needing to develop and maintain a client of our own.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhk, so in simple terms, we can compare them to like a working space with its monitor and CPU separate but can work together when connected.

Now we (Servo) are borrowing their monitor to connect with our CPU...

Will Servo eventually have its own, or we can keep using it in production?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's say it's more akin to a debugger and a program.

A client of our own is likely out of scope for Servo, and wouldn't be part of the engine either way. I don't know what you mean by production, but keep in mind that devtools is off by default even if the compile flag is on.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I understand the relationship between them now.

What I meant by production is; when it becomes available for the public to use, just like Firefox and Chrome. Is Servo going to still incorporate Firefox's devtools?

I'm sorry my questions are getting too much, I'm just curious.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whether to include the devtools server (which mimics Firefox without being Firefox) is ultimately the choice of whomever decides to depend on Servo. We offer that choice by way of the devtools feature flag.

Either way I think we have strayed off-topic from this PR. Feel free to text me on Zulip if you have more general questions. We also have a dedicated devtools channel. Questions directly related to this PR are of course still welcome.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thank you.

Comment on lines +495 to +497
// When the body was truncated, report the true content size from the
// Content-Length header (i.e., the full original size) so that Firefox's
// ResponsePanel can compare `content.size >= limit` and display the

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"true content size" and "full original size" are ambiguous and I am not sure what you mean by these

Comment on lines +635 to +637
let transferred_size = headers
.and_then(|header| header.typed_get::<ContentLength>())
.map(|content_length_header| content_length_header.0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a correct way of representing the transmitted bytes. The content-length header is not always present or accurate. You could either:

  1. Get it from net, which might not be trivial
  2. Accept this as a limitation and file a new issue

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I just realised firefox doesn't even need it for truncation.

I think I was just trying different methods when I couldn't get it to render the banner few days ago.

Comment on lines +640 to +642
transferred_size
.map(|s| s as usize)
.unwrap_or(body.data.len())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be mixing up compressed and uncompressed sizes. I would think that this is supposed to be uncompressed size.

Comment on lines +949 to +954
#[cfg(feature = "devtools")]
let devtools_response_body =
devtools_body.map(DevtoolsBodyBuffer::into_response_body);
#[cfg(not(feature = "devtools"))]
let devtools_response_body = None;
send_response_to_devtools(request, context, response, devtools_response_body);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[cfg(feature = "devtools")]
let devtools_response_body =
devtools_body.map(DevtoolsBodyBuffer::into_response_body);
#[cfg(not(feature = "devtools"))]
let devtools_response_body = None;
send_response_to_devtools(request, context, response, devtools_response_body);
#[cfg(feature = "devtools")]
let devtools_response_body =
devtools_body.map(DevtoolsBodyBuffer::into_response_body);
#[cfg(feature = "devtools")]
send_response_to_devtools(request, context, response, devtools_response_body);

@servo-highfive servo-highfive added S-needs-code-changes Changes have not yet been made that were requested by a reviewer. and removed S-awaiting-review There is new code that needs to be reviewed. labels Oct 26, 2026
@servo-highfive servo-highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-needs-code-changes Changes have not yet been made that were requested by a reviewer. labels Oct 26, 2026
@sabbCodes

Copy link
Copy Markdown
Contributor Author

Hi @freyacodes

It's been a while, trust you're doing okay.
Wondering if you're still reviewing this?

@sabbCodes

Copy link
Copy Markdown
Contributor Author

Hi @freyacodes
I decided to address it in this PR.

I looked into Firefox's NetworkResponseListener.sys.mjs here:
https://github.com/mozilla/gecko-dev/blob/5836a062726f715fda621338a17b51aff30d0a8c/devtools/shared/network-observer/NetworkResponseListener.sys.mjs#L525-L531

and confirmed:

decodedBodySize is the full uncompressed body size and that size is an alias set directly to decodedBodySize.

In our implementation, DevtoolsBodyBuffer now accumulates the total uncompressed incoming chunk size across content_size, even when data past the truncation limit is discarded. We now pass this total count to decoded_body_size and size.

All 393 tests still pass and the uncompressed size is now shown in the devtools:

Screenshot from 2026-11-10 18-35-24

@freyacodes freyacodes left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work, I think this is ready to merge! I will try to get the attention of a maintainer

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Nov 10, 2026
@eerii eerii added the T-linux-devtools Do a try run on Linux of the devtools tests. label Nov 10, 2026
@github-actions github-actions Bot removed the T-linux-devtools Do a try run on Linux of the devtools tests. label Nov 10, 2026
@github-actions

Copy link
Copy Markdown

🔨 Triggering try run (#42923474647) for Linux (Devtools Tests)

@github-actions

Copy link
Copy Markdown

✨ Try run (#42923474647) succeeded.

@eerii eerii left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great. Thank you so much @freyacodes for reviewing, and nice work @sabbCodes :)

@eerii
eerii added this pull request to the merge queue Nov 10, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Nov 10, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 11, 2026
@servo-highfive servo-highfive added S-tests-failed The changes caused existing tests to fail. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Nov 11, 2026
@servo-highfive servo-highfive added S-awaiting-review There is new code that needs to be reviewed. and removed S-tests-failed The changes caused existing tests to fail. labels Nov 11, 2026
@sabbCodes

Copy link
Copy Markdown
Contributor Author

Hi @eerii

I've addressed the failed test that got this removed from merge queue.

@eerii
eerii added this pull request to the merge queue Nov 11, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Nov 11, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 11, 2026
@servo-highfive servo-highfive added S-tests-failed The changes caused existing tests to fail. and removed S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. labels Nov 11, 2026
@sabbCodes

Copy link
Copy Markdown
Contributor Author

Looks like that's not related to this changes, could someone please re add this to the merge queue?

@jdm
jdm added this pull request to the merge queue Nov 11, 2026
@servo-highfive servo-highfive added S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. and removed S-tests-failed The changes caused existing tests to fail. labels Nov 11, 2026
Merged via the queue into servo:main with commit d2b3279 Nov 11, 2026
44 checks passed
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-review There is new code that needs to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Devtools network tab receives arbitrarily large responses

6 participants