From a2362fc08c31b23fb971279f8b160555123ad2f6 Mon Sep 17 00:00:00 2001 From: srdusr <99972264+srdusr@users.noreply.github.com> Date: Tue, 9 Jun 2026 15:46:00 +0200 Subject: Body match-and-replace rules Extends match-and-replace rules to request/response bodies, not just headers. A body rule materializes the body into memory (bounded by the same maxCaptureBytes cap as history capture) instead of streaming it straight through - the opposite of the normal path, so it's only paid when a body rule is actually configured. A body over the cap passes through byte-exact and unmodified rather than being partially rewritten. Response Content-Length is recomputed explicitly when a rule changes body length: unlike http.Request.Write, http.ResponseWriter doesn't derive it from resp.ContentLength on its own, so a stale header would otherwise corrupt response framing for the client. The history audit trail still shows the original, pre-rule bytes on both legs; only the wire traffic reflects the rewrite. Verified live against a real daemon: origin receives the rewritten request body, client receives the rewritten response body with correct Content-Length, and history keeps the unmodified bytes. Adds a Part selector (header/body) to the Rules add/edit form and table in the TUI. --- cmd/mitmux/main.go | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) (limited to 'cmd') diff --git a/cmd/mitmux/main.go b/cmd/mitmux/main.go index f248ef0..d11d18b 100644 --- a/cmd/mitmux/main.go +++ b/cmd/mitmux/main.go @@ -134,6 +134,7 @@ const ( fieldMatch fieldReplace fieldScope + fieldPart fieldRegex ) @@ -177,6 +178,7 @@ type model struct { ruleMatch textinput.Model ruleReplace textinput.Model ruleScope string // "request" or "response" + rulePart string // "header" or "body" ruleRegex bool ruleField ruleField @@ -298,6 +300,7 @@ func newModel(client *ipc.Client, subCh <-chan store.Summary, socketPath string) {Title: "On", Width: 3}, {Title: "Name", Width: 16}, {Title: "Scope", Width: 9}, + {Title: "Part", Width: 7}, {Title: "Match", Width: 24}, {Title: "Replace", Width: 24}, {Title: "Regex", Width: 5}, @@ -374,6 +377,7 @@ func newModel(client *ipc.Client, subCh <-chan store.Summary, socketPath string) ruleMatch: matchIn, ruleReplace: replaceIn, ruleScope: "request", + rulePart: "header", intruderTemplate: itmpl, intruderPayloads: ipayloads, intruderResults: iresults, @@ -808,6 +812,7 @@ func (m *model) enterRuleForm(r *rules.Rule) { m.ruleMatch.SetValue("") m.ruleReplace.SetValue("") m.ruleScope = "request" + m.rulePart = "header" m.ruleRegex = false } else { m.ruleEditingID = r.ID @@ -816,6 +821,10 @@ func (m *model) enterRuleForm(r *rules.Rule) { m.ruleMatch.SetValue(r.Match) m.ruleReplace.SetValue(r.Replace) m.ruleScope = r.Scope + m.rulePart = r.Part + if m.rulePart == "" { + m.rulePart = "header" + } m.ruleRegex = r.IsRegex } m.ruleName.Focus() @@ -829,7 +838,7 @@ func (m *model) ruleFromForm() rules.Rule { Enabled: m.ruleEnabled, Name: m.ruleName.Value(), Scope: m.ruleScope, - Part: "header", + Part: m.rulePart, Match: m.ruleMatch.Value(), Replace: m.ruleReplace.Value(), IsRegex: m.ruleRegex, @@ -1546,24 +1555,31 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case "ctrl+s": return m, m.saveRule(m.ruleFromForm()) case "tab": - m.ruleField = (m.ruleField + 1) % 5 + m.ruleField = (m.ruleField + 1) % 6 m.focusRuleField() return m, nil case "shift+tab": - m.ruleField = (m.ruleField + 4) % 5 + m.ruleField = (m.ruleField + 5) % 6 m.focusRuleField() return m, nil } - if m.ruleField == fieldScope || m.ruleField == fieldRegex { + if m.ruleField == fieldScope || m.ruleField == fieldPart || m.ruleField == fieldRegex { switch msg.String() { case "left", "right", "enter", " ": - if m.ruleField == fieldScope { + switch m.ruleField { + case fieldScope: if m.ruleScope == "request" { m.ruleScope = "response" } else { m.ruleScope = "request" } - } else { + case fieldPart: + if m.rulePart == "header" { + m.rulePart = "body" + } else { + m.rulePart = "header" + } + default: m.ruleRegex = !m.ruleRegex } return m, nil @@ -2204,7 +2220,7 @@ func viModeLabel(v *viTextarea) string { func (m *model) rulesView() string { var b strings.Builder - b.WriteString(titleStyle.Render(fmt.Sprintf(" match/replace rules (%d) - headers only for now ", len(m.ruleRows)))) + b.WriteString(titleStyle.Render(fmt.Sprintf(" match/replace rules (%d) ", len(m.ruleRows)))) b.WriteString("\n") b.WriteString(m.rulesTable.View()) b.WriteString("\n") @@ -2241,6 +2257,8 @@ func (m *model) ruleFormView() string { scopeText := fmt.Sprintf("Scope: %s (◀▶ to change)", m.ruleScope) b.WriteString(label(fieldScope, scopeText) + "\n\n") + partText := fmt.Sprintf("Part: %s (◀▶ to change)", m.rulePart) + b.WriteString(label(fieldPart, partText) + "\n\n") regexText := "Regex: off (◀▶ to change)" if m.ruleRegex { regexText = "Regex: on (◀▶ to change)" @@ -2300,7 +2318,7 @@ func rulesRowsFor(rs []rules.Rule) []table.Row { if r.IsRegex { regex = "yes" } - rows[i] = table.Row{on, r.Name, r.Scope, r.Match, r.Replace, regex} + rows[i] = table.Row{on, r.Name, r.Scope, r.Part, r.Match, r.Replace, regex} } return rows } -- cgit v1.2.3