Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

fix: expand variables in http hook header values - #704

Open
lbarrosby wants to merge 1 commit into
creativeprojects:masterfrom
lbarrosby:expand-env-in-http-hook-headers
Open

lbarrosby wants to merge 1 commit into
creativeprojects:masterfrom
lbarrosby:expand-env-in-http-hook-headers

Conversation

@lbarrosby

Copy link
Copy Markdown
Contributor

Fixes #663

The issue tries to send Authorization: Basic <secret> from a send-after hook with the secret in an env-file. Template syntax ({{ .Env.SECRET }}) runs before the env file is loaded, and the runtime ${SECRET} syntax does not work either: Sender.Send expands the url (resolveURL) and the body (resolveBody) with the profile environment, but adds every header with header.Value.Value() as written.

Change:

  • header values go through resolveBody, so they get the same variables as the body: the profile environment (OS, env-file, env), PROFILE_NAME, PROFILE_COMMAND and, in send-after-fail/send-finally, the ERROR* ones. $$ still gives a literal $.
  • the masking of confidential headers in the dry-run and debug output used to compare the sent value with the configured one. After expansion they differ, which would have printed the expanded secret, so stringifyHeaders now takes a map from the expanded value to its masked form, built while the headers are added.
  • http_hooks.md says the header values are expanded too.

End to end with a stub restic, env-file containing HOOK_CREDENTIALS=dXNlcjpwYXNz and a send-before header value: 'Basic ${HOOK_CREDENTIALS}', the header received by a local HTTP server:

# master
Authorization: Basic ${HOOK_CREDENTIALS}
# this branch
Authorization: Basic dXNlcjpwYXNz

and --dry-run backup still prints Authorization: ×××.

Test: TestHeaderEnv checks an env variable and $PROFILE_NAME in header values, then runs the same hook in dry-run with a memory logger and checks the secret is not in the logs. It fails on master (actual: "Basic ${TEST_SECRET}").

$ go test -short ./monitor/... ./config/...
$ go vet ./monitor/... && golangci-lint run ./monitor/...

I used an AI assistant (Claude) to help with this change; I reviewed it and ran the checks above myself.

The url and the body of send-* hooks were expanded with the profile
environment at runtime, but header values were sent as written, so a
header such as "Authorization: Basic ${SECRET}" could not take its
secret from env or env-file. Expand header values like the body, and
mask confidential headers after expansion so the expanded secret does
not show in dry-run and debug output.

Signed-off-by: Lucas Barros <lucasbarros.kam@gmail.com>
@codecov

codecov Bot commented Nov 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.41%. Comparing base (6f9a1a9) to head (0cc6fba).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #704   +/-   ##
=======================================
  Coverage   85.41%   85.41%           
=======================================
  Files         161      161           
  Lines       10336    10338    +2     
=======================================
+ Hits         8828     8830    +2     
  Misses       1508     1508           
Flag Coverage Δ
unittests 83.32% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Expanded secrets can leak through custom-header logging, while multiline substitutions can prevent hook delivery and unconditional expansion can break existing credentials.

3 open issues
What changed in this PR

This PR enables runtime variable expansion in HTTP hook headers, addressing the header-related case in #663 so hooks can use secrets loaded from profile environment files.

Changes:

  • Expand header values using the same runtime variables as request bodies.
  • Track expanded confidential values for masking in dry-run and debug logs.
  • Add header expansion and dry-run masking tests, and document the syntax.
File Description
monitor/​hook/​sender.go Expands header values and updates confidential-value masking.
monitor/​hook/​sender_test.go Tests environment and profile-name expansion plus dry-run secret masking.
docs/​content/​configuration/​http_hooks.md Documents runtime header expansion and dollar-sign escaping.

Review effort: Balanced

Comment thread monitor/hook/sender.go
Comment on lines +119 to +120
if header.Value.IsConfidential() {
masked[value] = header.Value.String()
Comment thread monitor/hook/sender.go
continue
}
req.Header.Add(header.Name, header.Value.Value())
value := resolveBody(header.Value.Value(), ctx, env)
Comment thread monitor/hook/sender.go
continue
}
req.Header.Add(header.Name, header.Value.Value())
value := resolveBody(header.Value.Value(), ctx, env)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Runtime variable not resolved within run- and send-

2 participants