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