@@ -18,6 +18,9 @@ import (
18
18
"github.com/Azure/azure-sdk-for-go/sdk/internal/errorinfo"
19
19
)
20
20
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
+
21
24
// CtxWithHTTPHeaderKey is used as a context key for adding/retrieving http.Header.
22
25
type CtxWithHTTPHeaderKey struct {}
23
26
@@ -110,6 +113,25 @@ func ExtractModuleName(clientName string) (string, string, error) {
110
113
return matches [3 ], matches [2 ], nil
111
114
}
112
115
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
+
113
135
// NonRetriableError marks the specified error as non-retriable.
114
136
func NonRetriableError (err error ) error {
115
137
return & nonRetriableError {err }
0 commit comments