Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/fixed-proxy-dash-filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The GitHub egress proxy now rewrites em-dashes in the text fields of agent-written GitHub JSON requests (comments, reviews, PR and issue titles and bodies), using the shared filter now in `crux/prosefilter`; set `HIVE_PROXY_DASH_FILTER=off` to disable (#19103).
21 changes: 21 additions & 0 deletions src/crux/config/proxy_dash_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package config

import (
"os"
"strings"
)

// ProxyDashFilterEnv switches the GitHub egress proxy's outbound dash filter.
// The filter is on by default; set this to "off" (or "false", "0") to forward
// agent write bodies unchanged.
const ProxyDashFilterEnv = "HIVE_PROXY_DASH_FILTER"

// ProxyDashFilter reports whether the proxy rewrites em-dashes in the text
// fields of agent-originated GitHub JSON writes. Default on.
func ProxyDashFilter() bool {
switch strings.ToLower(strings.TrimSpace(os.Getenv(ProxyDashFilterEnv))) {
case "off", "false", "0", "no":
return false
}
return true
}
210 changes: 210 additions & 0 deletions src/crux/prosefilter/prosefilter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// Package prosefilter is the one place hive rewrites dashes in text that
// leaves the hive for a forge (PR and issue titles and bodies, comments,
// review bodies, commit messages).
//
// The owner's rule is that em-dashes are never acceptable in anything hive or
// its agents write. Agents are told so in the shared base prompt, but a model
// can still emit one, so every outward write path also passes its prose
// through Sanitize before it is sent.
//
// Sanitize rewrites only prose. Fenced code blocks, inline code spans and
// Markdown blockquote lines are left byte-for-byte alone, because they carry
// quoted or pasted content (a diff, a log line, a file excerpt) that must not
// be altered.
package prosefilter

import (
"regexp"
"strings"
)

const (
emDash = "\u2014"
enDash = "\u2013"

// plainDash is what a dash used as punctuation becomes.
plainDash = "-"
// spacedDash is the replacement between two words.
spacedDash = " - "

// maxFenceIndent is the Markdown limit on leading spaces for a code fence.
maxFenceIndent = 3
// minFenceRun is the shortest run of fence characters that opens a fence.
minFenceRun = 3
)

var (
// emDashRE matches an em-dash with the horizontal whitespace around it.
emDashRE = regexp.MustCompile(`[ \t]*` + emDash + `[ \t]*`)
// spacedEnDashRE matches an en-dash used as a dash (whitespace on both
// sides). An en-dash in a range such as 3-5 has no spaces and is left alone
// here; a hard-coded range is fixed at its source.
spacedEnDashRE = regexp.MustCompile(`[ \t]+` + enDash + `[ \t]+`)
)

// HasDash reports whether s contains an em-dash or an en-dash.
func HasDash(s string) bool {
return strings.Contains(s, emDash) || strings.Contains(s, enDash)
}

// Sanitize replaces every em-dash (U+2014), and every en-dash (U+2013) used
// as a spaced dash, in the prose of s with a plain hyphen. Text inside fenced
// code blocks, inline code spans and blockquote lines is untouched. A string
// with no such dash is returned unchanged.
func Sanitize(s string) string {
if !HasDash(s) {
return s
}
lines := strings.Split(s, "\n")
var fenceChar byte
var fenceLen int
for i, line := range lines {
if fenceChar != 0 {
if closesFence(line, fenceChar, fenceLen) {
fenceChar, fenceLen = 0, 0
}
continue
}
if c, n := opensFence(line); c != 0 {
fenceChar, fenceLen = c, n
continue
}
if isBlockquote(line) {
continue
}
lines[i] = sanitizeLine(line)
}
return strings.Join(lines, "\n")
}

// SanitizeTitle is Sanitize for a single-line title. A title has no code
// fence or blockquote, so only inline code spans are protected.
func SanitizeTitle(s string) string {
if !HasDash(s) {
return s
}
return sanitizeLine(s)
}

func opensFence(line string) (byte, int) {
trimmed := strings.TrimLeft(line, " ")
if len(line)-len(trimmed) > maxFenceIndent || trimmed == "" {
return 0, 0
}
c := trimmed[0]
if c != '`' && c != '~' {
return 0, 0
}
n := runLen(trimmed, 0, c)
if n < minFenceRun {
return 0, 0
}
// A backtick fence's info string may not contain a backtick; such a line
// is an inline code span, not a fence.
if c == '`' && strings.Contains(trimmed[n:], "`") {
return 0, 0
}
return c, n
}

func closesFence(line string, c byte, openLen int) bool {
trimmed := strings.TrimLeft(line, " ")
if len(line)-len(trimmed) > maxFenceIndent || trimmed == "" || trimmed[0] != c {
return false
}
n := runLen(trimmed, 0, c)
return n >= openLen && strings.TrimSpace(trimmed[n:]) == ""
}

func isBlockquote(line string) bool {
return strings.HasPrefix(strings.TrimLeft(line, " "), ">")
}

func runLen(s string, from int, c byte) int {
n := 0
for from+n < len(s) && s[from+n] == c {
n++
}
return n
}

// sanitizeLine rewrites one prose line, skipping inline code spans.
func sanitizeLine(line string) string {
if !HasDash(line) {
return line
}
indentLen := len(line) - len(strings.TrimLeft(line, " \t"))
indent, rest := line[:indentLen], line[indentLen:]

var b strings.Builder
b.WriteString(indent)
segStart := 0
flush := func(end int) {
if end > segStart {
b.WriteString(rewriteSegment(rest[segStart:end], segStart == 0, end == len(rest)))
}
}
for i := 0; i < len(rest); {
if rest[i] != '`' {
i++
continue
}
n := runLen(rest, i, '`')
closeAt := findClosingRun(rest, i+n, n)
if closeAt < 0 {
i += n
continue
}
flush(i)
spanEnd := closeAt + n
b.WriteString(rest[i:spanEnd])
segStart = spanEnd
i = spanEnd
}
flush(len(rest))
return b.String()
}

// findClosingRun returns the index of the next run of exactly n backticks at
// or after from, or -1.
func findClosingRun(s string, from, n int) int {
for i := from; i < len(s); {
if s[i] != '`' {
i++
continue
}
m := runLen(s, i, '`')
if m == n {
return i
}
i += m
}
return -1
}

// rewriteSegment replaces dashes in one non-code stretch of a line. atStart
// and atEnd say whether the segment touches the start or end of the line's
// prose, so a leading or trailing dash does not leave a stray space.
func rewriteSegment(seg string, atStart, atEnd bool) string {
seg = spacedEnDashRE.ReplaceAllString(seg, spacedDash)
locs := emDashRE.FindAllStringIndex(seg, -1)
if len(locs) == 0 {
return seg
}
var b strings.Builder
prev := 0
for _, loc := range locs {
b.WriteString(seg[prev:loc[0]])
switch {
case loc[0] == 0 && atStart:
b.WriteString(plainDash + " ")
case loc[1] == len(seg) && atEnd:
b.WriteString(" " + plainDash)
default:
b.WriteString(spacedDash)
}
prev = loc[1]
}
b.WriteString(seg[prev:])
return b.String()
}
126 changes: 126 additions & 0 deletions src/crux/proxy/dash_filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package proxy

import (
"bytes"
"encoding/json"
"io"
"net/http"
"strings"

"github.com/hivecommons/hive/crux/config"
"github.com/hivecommons/hive/crux/prosefilter"
)

// maxDashFilterBody bounds how much of a request body is buffered for
// rewriting. Larger bodies are forwarded untouched.
const maxDashFilterBody = 4 << 20

// filterOutboundDashes rewrites em-dashes in the text fields (body, title,
// message, review comments[].body, and the body/title inputs of GraphQL
// mutations) of an agent-originated GitHub JSON write, so an agent's own
// `gh api` call cannot put one on GitHub. Non-JSON bodies, other fields and
// git traffic are left alone. Content-Length is recomputed. It logs counts
// only, never the text.
func (p *GitHubProxy) filterOutboundDashes(req *http.Request, agentName string) {
if !config.ProxyDashFilter() || req.Body == nil || req.Body == http.NoBody {
return
}
switch req.Method {
case http.MethodPost, http.MethodPut, http.MethodPatch:
default:
return
}
if isGitPath(req.URL.Path) || !strings.Contains(strings.ToLower(req.Header.Get("Content-Type")), "json") {
return
}
if req.ContentLength < 0 || req.ContentLength > maxDashFilterBody {
return
}
raw, err := io.ReadAll(req.Body)
_ = req.Body.Close()
if err != nil {
// Partial read: forward what we have rather than dropping bytes.
req.Body = io.NopCloser(bytes.NewReader(raw))
return
}
out, n := rewriteJSONDashes(raw, strings.HasSuffix(req.URL.Path, "/graphql"))
if n > 0 {
raw = out
req.Header.Del("Content-Length")
if p.logger != nil {
p.logger.Debug("proxy dash filter rewrote text fields", "agent", agentName, "fields", n)
}
}
req.Body = io.NopCloser(bytes.NewReader(raw))
req.ContentLength = int64(len(raw))
}

// rewriteJSONDashes returns the body with dashes rewritten and the number of
// fields changed. Unparseable or unchanged input is returned as is.
func rewriteJSONDashes(raw []byte, graphql bool) ([]byte, int) {
dec := json.NewDecoder(bytes.NewReader(raw))
dec.UseNumber()
var doc any
if err := dec.Decode(&doc); err != nil {
return raw, 0
}
if _, err := dec.Token(); err != io.EOF {
return raw, 0
}
obj, ok := doc.(map[string]any)
if !ok {
return raw, 0
}
n := rewriteTextFields(obj, true)
if graphql {
if vars, ok := obj["variables"].(map[string]any); ok {
n += rewriteTextFields(vars, false)
for _, v := range vars {
if in, ok := v.(map[string]any); ok {
n += rewriteTextFields(in, false)
}
}
}
}
if n == 0 {
return raw, 0
}
var buf bytes.Buffer
enc := json.NewEncoder(&buf)
enc.SetEscapeHTML(false)
if err := enc.Encode(obj); err != nil {
return raw, 0
}
return bytes.TrimRight(buf.Bytes(), "\n"), n
}

func rewriteTextFields(obj map[string]any, reviewComments bool) int {
n := 0
for key, sanitize := range map[string]func(string) string{
"body": prosefilter.Sanitize,
"title": prosefilter.SanitizeTitle,
"message": prosefilter.Sanitize,
} {
if s, ok := obj[key].(string); ok {
if r := sanitize(s); r != s {
obj[key] = r
n++
}
}
}
if reviewComments {
if list, ok := obj["comments"].([]any); ok {
for _, item := range list {
if c, ok := item.(map[string]any); ok {
if s, ok := c["body"].(string); ok {
if r := prosefilter.Sanitize(s); r != s {
c["body"] = r
n++
}
}
}
}
}
}
return n
}
Loading
Loading