Skip to content

Why does this code panic when trying to set AllowedOrigins? #4195

@jarrodhroberson

Description

@jarrodhroberson

Using the following code I get a panic

func CORS() gin.HandlerFunc {
	allowedOrigins := []string{"https://example.com", "https://www.example.com"}
	if v, ok := os.LookupEnv("ALLOW_LOCAL_HOST"); ok {
		allowedOrigins = append(allowedOrigins, v)
	}
	config := cors.Config{
		AllowOrigins:     allowedOrigins,
	}

	return cors.New(config)
}

I get this panic

ERROR 2025-03-24T02:41:34.271704Z panic: bad origin: origins must contain '*' or include http://,https://

but if I slightly change it to

	config := cors.Config{
		AllowOrigins: []string{"https://example.com", "https://www.example.com"},
	}

it works as expected?

Is this a bug?

If not What am I missing here?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions