feat(http): respect annotations in unsafe mode#7044
Conversation
Apply request annotations during `unsafe` raw request generation so @Host/@timeout/etc affect the effective target and execution context. Strip leading annotation directives from `unsafe` wire bytes before sending, since `rawhttp` transmits raw payload verbatim and annotation lines can produce malformed HTTP requests. Close #6747 Signed-off-by: Dwi Siswanto <git@dw1.io>
WalkthroughThe pull request adds support for request annotations in unsafe/raw HTTP mode. Changes include fetching annotation overrides, applying host and header adjustments, stripping leading annotation lines from raw requests, and merging annotation-provided interactsh URLs into the final unsafe request object. Changes
Sequence DiagramsequenceDiagram
participant Client
participant RawParser as Raw Parser
participant AnnotationFetcher as Annotation Handler
participant RequestBuilder as Request Builder
participant Server
Client->>RawParser: Parse unsafe raw request with annotations
RawParser->>RawParser: Strip leading `@annotations`
RawParser-->>Client: rawRequest (without annotations)
Client->>RequestBuilder: Generate unsafe request
RequestBuilder->>AnnotationFetcher: Fetch annotation overrides<br/>(if annotationURL available)
AnnotationFetcher->>AnnotationFetcher: Apply Host header<br/>from original request
AnnotationFetcher->>Server: Context-bound request
Server-->>AnnotationFetcher: Annotation metadata
AnnotationFetcher->>AnnotationFetcher: Extract cancelFunc<br/>& interactsh URLs
AnnotationFetcher-->>RequestBuilder: Overrides
RequestBuilder->>RequestBuilder: Apply host overrides
RequestBuilder->>RequestBuilder: Merge interactsh URLs
RequestBuilder->>RequestBuilder: Attach cancelFunc
RequestBuilder-->>Client: Final unsafe request
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@pkg/protocols/http/raw/raw.go`:
- Around line 268-283: stripLeadingAnnotations fails to detect annotation
directives that are indented (e.g., " `@Host`: ..."); update the annotation check
in stripLeadingAnnotations to ignore leading whitespace before testing for "@"
so indented annotation lines are treated the same as non-indented ones.
Specifically, change the condition that calls stringsutil.HasPrefixAny on line
to instead call it on a left-trimmed version of line (trim spaces and tabs) so
lines starting with optional indentation followed by "@" are recognized and
skipped; keep the rest of the logic (requestLineFound, buffer writes, and
bufferPool handling) unchanged and only alter the annotation-detection
expression in stripLeadingAnnotations.
ℹ️ Review info
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
pkg/protocols/http/build_request.gopkg/protocols/http/build_request_test.gopkg/protocols/http/raw/raw.gopkg/protocols/http/raw/raw_test.gopkg/protocols/http/request.go
| func stripLeadingAnnotations(request string) []byte { | ||
| reader := bufio.NewReader(strings.NewReader(request)) | ||
| buffer := bufferPool.Get().(*bytes.Buffer) | ||
| buffer.Reset() | ||
| defer func() { | ||
| buffer.Reset() | ||
| bufferPool.Put(buffer) | ||
| }() | ||
|
|
||
| requestLineFound := false | ||
| for { | ||
| line, err := reader.ReadString('\n') | ||
| if len(line) > 0 { | ||
| if requestLineFound || !stringsutil.HasPrefixAny(line, "@") { | ||
| requestLineFound = true | ||
| _, _ = buffer.WriteString(line) |
There was a problem hiding this comment.
Handle indented annotation directives when stripping.
Annotation detection is currently prefix-only on the raw line. Inputs like " @host: ..." are retained, so unsafe raw bytes can still carry annotation text.
🔧 Proposed fix
diff --git a/pkg/protocols/http/raw/raw.go b/pkg/protocols/http/raw/raw.go
@@
- if stringsutil.HasPrefixAny(s, "@") {
+ if stringsutil.HasPrefixAny(strings.TrimLeft(s, " \t"), "@") {
goto read_line
}
@@
- if requestLineFound || !stringsutil.HasPrefixAny(line, "@") {
+ trimmed := strings.TrimLeft(line, " \t")
+ if requestLineFound || !stringsutil.HasPrefixAny(trimmed, "@") {
requestLineFound = true
_, _ = buffer.WriteString(line)
}🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@pkg/protocols/http/raw/raw.go` around lines 268 - 283,
stripLeadingAnnotations fails to detect annotation directives that are indented
(e.g., " `@Host`: ..."); update the annotation check in stripLeadingAnnotations
to ignore leading whitespace before testing for "@" so indented annotation lines
are treated the same as non-indented ones. Specifically, change the condition
that calls stringsutil.HasPrefixAny on line to instead call it on a left-trimmed
version of line (trim spaces and tabs) so lines starting with optional
indentation followed by "@" are recognized and skipped; keep the rest of the
logic (requestLineFound, buffer writes, and bufferPool handling) unchanged and
only alter the annotation-detection expression in stripLeadingAnnotations.
Neo - PR Security ReviewNo security issues found Highlights
Hardening Notes
Comment |
Proposed changes
Apply request annotations during
unsaferawrequest generation so @Host/@timeout/etc affect
the effective target and execution context.
Strip leading annotation directives from
unsafewire bytes before sending, since
rawhttptransmits raw payload verbatim and annotation
lines can produce malformed HTTP requests.
Close #6747
Proof
Checklist
Summary by CodeRabbit
Release Notes