Giter VIP home page Giter VIP logo

Comments (11)

go-english avatar go-english commented on September 15, 2024 1

@jub0bs That i'm say,i set origin:https://www.myhome.com, expect only this url can access my server(https://www.myserver.com/), but i found i misunderstand .than i find this description in what is origin
I realize it can be null or fabricate,such as i use curl https://www.myserver.com(Not a server with [my server] deployed), and i was expecting it to not be able to access, but it can because it doesn't have an origin set.
Then i use curl -H "origin: https://www.myhome.com" https://www.myserver.com, it work too!,because i set fake origin
So it's not the code that's the problem, it's my misunderstand.
I guess I should have verified the ip of the visitor.That's what I need.
Thanks for you help

from cors.

go-english avatar go-english commented on September 15, 2024 1

@jub0bs Yean,bro!I think i finally understand CORS can do something and not can do something.Thinks for you patience,have a nice day!

from cors.

klm127 avatar klm127 commented on September 15, 2024

When you use AllowOriginFunc, AllowOrigins is ignored. So you should remove AllowOrigins and put the foo.com check inside of AllowOriginFunc, eg:

AllowOriginFunc: func(origin string) bool {
			if origin == "http://www.foo.com" {
				return true
			}
			if origin == "https://github.com" {
				return true
			}
			return false
		}

I noticed that foo.com, if you are actually testing there, is not secure, so make sure it's http not https.

Secondly, github.com has a content security policy that prevents CORs requests, so it may not be a simple matter to query your server from the dev console, for example.

from cors.

douno23 avatar douno23 commented on September 15, 2024
AllowOriginFunc: func(origin string) bool {
			if origin == "http://www.foo.com" {
				return true
			}
			if origin == "https://github.com" {
				return true
			}
			return false
		}

i have tried this, but doesn't work either.

from cors.

jub0bs avatar jub0bs commented on September 15, 2024

@douno23 The screenshot you shared shows a request that does not include any Origin header; therefore, it's not a CORS request.

from cors.

go-english avatar go-english commented on September 15, 2024

i have the same problem

from cors.

jub0bs avatar jub0bs commented on September 15, 2024

@go-english What problem? If the request doesn't contain any Origin header, it doesn't participate in the CORS protocol and you cannot expect it to contain CORS response headers (though it could, in some implementations).

from cors.

go-english avatar go-english commented on September 15, 2024

@jub0bs HI,bro.thanks for your reply.
this is my code,assume it's expose url:https://www.myserver.com

var Router = gin.Default()
Router.Use(middleware.NewCors())         
func NewCors() gin.HandlerFunc 
	return cors.New(cors.Config{
		AllowOrigins:     []string{"https://www.myhome.com"},
		AllowMethods:     []string{"POST", "GET", "OPTIONS"},
		AllowHeaders:     []string{"Content-Type", "x-token"},
		ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		AllowCredentials: true,
		MaxAge:           7 * time.Hour * 24,
		AllowAllOrigins: false,
	})
}
}

i have the issue,if i set origins:"https://www.myhome.com"
why other address can access it? (example on my personal PC,it definite not "https://www.myhome.com" ,i use curl https://www.myserver.com, It should return me a 403 forbidden but not instead of customizing response)
if any hint i appreciate!

from cors.

go-english avatar go-english commented on September 15, 2024

@jub0bs HI,bro.thanks for your reply. this is my code,assume it's expose url:https://www.myserver.com

var Router = gin.Default()
Router.Use(middleware.NewCors())         
func NewCors() gin.HandlerFunc 
	return cors.New(cors.Config{
		AllowOrigins:     []string{"https://www.myhome.com"},
		AllowMethods:     []string{"POST", "GET", "OPTIONS"},
		AllowHeaders:     []string{"Content-Type", "x-token"},
		ExposeHeaders:    []string{"Content-Length", "Access-Control-Allow-Origin", "Access-Control-Allow-Headers", "Content-Type"},
		AllowCredentials: true,
		MaxAge:           7 * time.Hour * 24,
		AllowAllOrigins: false,
	})
}
}

i have the issue,if i set origins:"https://www.myhome.com" why other address can access it? (example on my personal PC,it definite not "https://www.myhome.com" ,i use curl https://www.myserver.com, It should return me a 403 forbidden but not instead of customizing response) if any hint i appreciate!

i think i find problem,in cors.config.go
line 68

func (cors *cors) applyCors(c *gin.Context) {
	origin := c.Request.Header.Get("Origin")
	if len(origin) == 0 {
		// request is not a CORS request
		return
	}
	host := c.Request.Host

	if origin == "http://"+host || origin == "https://"+host {
		// request is not a CORS request but have origin header.
		// for example, use fetch api
		return
	}

	if !cors.validateOrigin(origin) {
		c.AbortWithStatus(http.StatusForbidden)
		return
	}

	if c.Request.Method == "OPTIONS" {
		cors.handlePreflight(c)
		defer c.AbortWithStatus(cors.optionsResponseStatusCode)
	} else {
		cors.handleNormal(c)
	}

	if !cors.allowAllOrigins {
		c.Header("Access-Control-Allow-Origin", origin)
	}
}`
allow all access,if don't set header origin.
i don't known,why?Shouldn't it be disabled by default?

from cors.

jub0bs avatar jub0bs commented on September 15, 2024

@go-english I'm not sure I understand the issue. Can you post one or more curl commands that trigger the behaviour you observe and also explain what behaviour you expect?

from cors.

jub0bs avatar jub0bs commented on September 15, 2024

@go-english I think you misunderstand the purpose of CORS. Contrary to popular belief, CORS is no substitute for server-side authorisation. Rather, CORS is a protocol that lets servers instruct browsers to relax the Same-Origin Policy's restrictions for select clients. All other things being equal, activating CORS makes your users less (not more) secure.

Besides, not all user agents implement the SOP or CORS. You shouldn't be surprised that you're able to spoof the Origin header using something like curl.

from cors.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.