@@ -281,6 +281,17 @@ func (cli *DockerCli) Initialize(opts *cliflags.ClientOptions, ops ...CLIOption)
281281 }
282282 filterResourceAttributesEnvvar ()
283283
284+ // early return if GODEBUG is already set or the docker context is
285+ // the default context, i.e. is a virtual context where we won't override
286+ // any GODEBUG values.
287+ if v := os .Getenv ("GODEBUG" ); cli .currentContext == DefaultContextName || v != "" {
288+ return nil
289+ }
290+ meta , err := cli .contextStore .GetMetadata (cli .currentContext )
291+ if err == nil {
292+ setGoDebug (meta )
293+ }
294+
284295 return nil
285296}
286297
@@ -474,6 +485,57 @@ func (cli *DockerCli) getDockerEndPoint() (ep docker.Endpoint, err error) {
474485 return resolveDockerEndpoint (cli .contextStore , cn )
475486}
476487
488+ // setGoDebug is an escape hatch that sets the GODEBUG environment
489+ // variable value using docker context metadata.
490+ //
491+ // {
492+ // "Name": "my-context",
493+ // "Metadata": { "GODEBUG": "x509negativeserial=1" }
494+ // }
495+ //
496+ // WARNING: Setting x509negativeserial=1 allows Go's x509 library to accept
497+ // X.509 certificates with negative serial numbers.
498+ // This behavior is deprecated and non-compliant with current security
499+ // standards (RFC 5280). Accepting negative serial numbers can introduce
500+ // serious security vulnerabilities, including the risk of certificate
501+ // collision or bypass attacks.
502+ // This option should only be used for legacy compatibility and never in
503+ // production environments.
504+ // Use at your own risk.
505+ func setGoDebug (meta store.Metadata ) {
506+ fieldName := "GODEBUG"
507+ godebugEnv := os .Getenv (fieldName )
508+ // early return if GODEBUG is already set. We don't want to override what
509+ // the user already sets.
510+ if godebugEnv != "" {
511+ return
512+ }
513+
514+ var cfg any
515+ var ok bool
516+ switch m := meta .Metadata .(type ) {
517+ case DockerContext :
518+ cfg , ok = m .AdditionalFields [fieldName ]
519+ if ! ok {
520+ return
521+ }
522+ case map [string ]any :
523+ cfg , ok = m [fieldName ]
524+ if ! ok {
525+ return
526+ }
527+ default :
528+ return
529+ }
530+
531+ v , ok := cfg .(string )
532+ if ! ok {
533+ return
534+ }
535+ // set the GODEBUG environment variable with whatever was in the context
536+ _ = os .Setenv (fieldName , v )
537+ }
538+
477539func (cli * DockerCli ) initialize () error {
478540 cli .init .Do (func () {
479541 cli .dockerEndpoint , cli .initErr = cli .getDockerEndPoint ()
0 commit comments