JuliusBrussee/caveman · error

awssig: request has no URL

Error message

awssig: request has no URL

What it means

Signer.Sign requires req.URL to be non-nil to derive the host and canonical request; if the *http.Request was built without a URL (e.g. manually constructed rather than via http.NewRequest), signing fails with this error. It is a malformed-input guard on the request object itself.

Solutions

  1. Build the request with http.NewRequest(http.MethodGet, rawURL, body) so URL and Host are populated.
  2. Before signing, assert req.URL != nil && req.URL.Host != "" and fail with a clear message.
  3. If the URL is assigned dynamically, ensure the assignment happens before Sign is called.

Example fix

// before
req := &http.Request{Method: "GET", Header: h}
// after
req, err := http.NewRequest(http.MethodGet, "https://s3.us-east-1.amazonaws.com/bucket/key", nil)
if err != nil { return err }
err = signer.Sign(req, creds, payloadHash, now)
Defensive patterns

Strategy: validation

Validate before calling

func signable(req *http.Request) bool { return req != nil && req.URL != nil && req.URL.Host != "" }
if !signable(req) {
    return errors.New("request must be built via http.NewRequest with a full URL before signing")
}

Try / catch

if err := signer.Sign(req, creds, hash, now); err != nil {
    if strings.Contains(err.Error(), "request has no URL") {
        return fmt.Errorf("request construction bug: %w", err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling Sign with an &http.Request{} literal (URL nil) instead of one built by http.NewRequest; a request whose URL was cleared/replaced with nil before signing.

Common situations: Hand-assembling http.Request in tests or middleware; a code path that parses a URL conditionally and leaves it nil on some branches; passing a server-side received request after stripping fields.

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/45d4397bbeefabef. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/awssig/awssig.go:74

// the Authorization, X-Amz-Date, X-Amz-Content-Sha256 (and, when present,
// X-Amz-Security-Token) headers on req. The Host header is derived from req.URL.
//
// payloadHash is the lowercase hex SHA-256 of the request body; pass
// HashPayload(body) for the common case, or UnsignedPayload() for streaming
// bodies that must not be buffered. now fixes the signing instant (use
// time.Now().UTC()); it is a parameter so tests are deterministic.
//
// Sign returns an error only for malformed inputs (no region/service, missing
// credentials, unparseable URL). It never returns the secret in the error.
func (s Signer) Sign(req *http.Request, creds Credentials, payloadHash string, now time.Time) error {
	if s.Region == "" || s.Service == "" {
		return fmt.Errorf("awssig: signer requires region and service")
	}
	if !creds.Valid() {
		return fmt.Errorf("awssig: incomplete AWS credentials")
	}
	if req.URL == nil {
		return fmt.Errorf("awssig: request has no URL")
	}
	now = now.UTC()
	amzDate := now.Format("20060102T150405Z")
	dateStamp := now.Format("20060102")

	host := req.URL.Host
	if req.Host != "" {
		host = req.Host
	}
	req.Header.Set("Host", host)
	req.Header.Set("X-Amz-Date", amzDate)
	req.Header.Set("X-Amz-Content-Sha256", payloadHash)
	if creds.SessionToken != "" {
		req.Header.Set("X-Amz-Security-Token", creds.SessionToken)
	}

	canonicalHeaders, signedHeaders := canonicalHeaderSet(req, host)
	canonicalRequest := strings.Join([]string{

View on GitHub (pinned to 3ee70a1026)