@@ -18,6 +18,9 @@ import (
1818 "github.com/Azure/azure-sdk-for-go/sdk/internal/errorinfo"
1919)
2020
21+ // NOTE: when adding a new context key type, it likely needs to be
22+ // added to the deny-list of key types in ContextWithDeniedValues
23+
2124// CtxWithHTTPHeaderKey is used as a context key for adding/retrieving http.Header.
2225type CtxWithHTTPHeaderKey struct {}
2326
@@ -110,6 +113,25 @@ func ExtractModuleName(clientName string) (string, string, error) {
110113 return matches [3 ], matches [2 ], nil
111114}
112115
116+ // ContextWithDeniedValues wraps an existing [context.Context], denying access to certain context values.
117+ // Pipeline policies that create new requests to be sent down their own pipeline MUST wrap the caller's
118+ // context with an instance of this type. This is to prevent context values from flowing across disjoint
119+ // requests which can have unintended side-effects.
120+ type ContextWithDeniedValues struct {
121+ context.Context
122+ }
123+
124+ // Value implements part of the [context.Context] interface.
125+ // It acts as a deny-list for certain context keys.
126+ func (c * ContextWithDeniedValues ) Value (key any ) any {
127+ switch key .(type ) {
128+ case CtxAPINameKey , CtxWithCaptureResponse , CtxWithHTTPHeaderKey , CtxWithRetryOptionsKey , CtxWithTracingTracer :
129+ return nil
130+ default :
131+ return c .Context .Value (key )
132+ }
133+ }
134+
113135// NonRetriableError marks the specified error as non-retriable.
114136func NonRetriableError (err error ) error {
115137 return & nonRetriableError {err }
0 commit comments