Skip to content

[BUG] Recursive call and logical errors in Dynamic. UnmarshalJSON implementation #6267

@chenjb04

Description

@chenjb04

Is there an existing issue for this?

  • I have searched the existing issues.

Current Behavior

I found that the implementation of the UnmarshalJSON method for the Dynamic struct in pkg/authprovider/authx/dynamic. go contains a recursive call issue.

func (d *Dynamic) UnmarshalJSON(data []byte) error {
	if err := json.Unmarshal(data, &d); err != nil {
		return err
	}
	var s Secret
	if err := json.Unmarshal(data, &s); err != nil {
		return err
	}
	d.Secret = &s
	return nil
}

Problems:
Using json. Unmarshal(data, &d) will trigger an infinite recursion because it will call UnmarshalJSON on d repeatedly, leading to stack overflow.
The correct way to avoid recursion is to use a type alias that does not implement UnmarshalJSON.
Double unmarshalling of the same data is redundant and may cause confusion or data inconsistency.

Expected Behavior

Suggested Fix: You can avoid recursion by introducing a type alias, for example:

func (d *Dynamic) UnmarshalJSON(data []byte) error {
	
	type DynamicAlias Dynamic
	aux := &struct {
		*DynamicAlias
	}{
		DynamicAlias: (*DynamicAlias)(d),
	}

	if err := json.Unmarshal(data, aux); err != nil {
		return err
	}

	var s Secret
	if err := json.Unmarshal(data, &s); err != nil {
		return err
	}
	d.Secret = &s
	return nil
}

Steps To Reproduce

N/A

Relevant log output

N/A

Environment

N/A

Anything else?

No response

Metadata

Metadata

Assignees

Labels

Type: BugInconsistencies or issues which will cause an issue or problem for users or implementors.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions