Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create new release #2

Open
wants to merge 52 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
93251e7
dummy empty commit
shogo82148 Mar 27, 2019
aaf1628
move to shogo82148's repo
shogo82148 Apr 17, 2019
d153662
migrate to go mod
shogo82148 Apr 17, 2019
59a5ab2
go mod tidy
shogo82148 Apr 17, 2019
8fd85a8
Merge pull request #1 from shogo82148/fork-to-shogo82148
shogo82148 Apr 17, 2019
f56e3ee
Merge commit 'a90fd5817e0c4b961458a670565f557be28d7991'
shogo82148 Apr 17, 2019
79cf1a6
fix golint warnings.
shogo82148 Apr 17, 2019
896dfe1
remove GoaCORS prefix
shogo82148 Apr 17, 2019
1851d53
Url should be URL
shogo82148 Apr 17, 2019
b3cb7cc
fix comment
shogo82148 Apr 17, 2019
75a2b82
avoid to rewrite Skipper.
shogo82148 Apr 17, 2019
5a7d821
avoid to rewrite AllowMethods and change the default.
shogo82148 Apr 17, 2019
b1fc4ec
avoid to rewrite DomainStrategy
shogo82148 Apr 17, 2019
e67f129
strict check of DomainStrategy
shogo82148 Apr 17, 2019
4ecb872
avoid to refer to conf.MaxAge in the middleware
shogo82148 Apr 17, 2019
a397171
add spaces.
shogo82148 Apr 17, 2019
d3efe41
fix comments
shogo82148 Apr 17, 2019
ba0d84e
reduce multiple call of rw.Header()
shogo82148 Apr 17, 2019
7eccdf6
add some comments for the Config.
shogo82148 Apr 17, 2019
dea6a39
remove default config.
shogo82148 Apr 17, 2019
f428e5d
remove AllowIntermediateMatch
shogo82148 Apr 17, 2019
412de32
the cofig is required.
shogo82148 Apr 17, 2019
dad1ac7
Merge pull request #2 from shogo82148/prevent-rewrite-configure
shogo82148 Apr 17, 2019
39934f7
Create LICENSE.md
shogo82148 Apr 17, 2019
ef725a0
move ot v2 directory
shogo82148 Apr 17, 2019
2ad4fc0
move go.mod to v2
shogo82148 Apr 17, 2019
8d4848b
fix module name
shogo82148 Apr 17, 2019
5d218da
fix module structure
shogo82148 Jun 14, 2019
fc6f879
fix test
shogo82148 Jun 14, 2019
5a7961e
fix url of travis-ci badge
shogo82148 Jun 16, 2019
8a92603
Hello GitHub Actions
shogo82148 Nov 15, 2019
bf122dd
Merge pull request #3 from shogo82148/hello-github-actions
shogo82148 Nov 15, 2019
c7908d6
matrix test
shogo82148 Nov 15, 2019
e840721
Merge pull request #4 from shogo82148/enable-matrix-test
shogo82148 Nov 15, 2019
18eb3a1
update deps
shogo82148 Nov 15, 2019
6085856
Update README.md
shogo82148 Nov 25, 2019
fbd333e
thanks travis-ci
shogo82148 Nov 26, 2019
255bcb4
send coverage from github actions
shogo82148 Nov 26, 2019
8e45f26
fix build badge
shogo82148 Nov 26, 2019
2dc8f04
run test by goveralls
shogo82148 Nov 26, 2019
2a0447f
missing LF
shogo82148 Nov 26, 2019
d3bbe83
Merge pull request #6 from shogo82148/thanks-travis-ci
shogo82148 Nov 26, 2019
b909e0b
more flexible and correct origin match
shogo82148 Nov 26, 2019
8af1cbb
fix invalid configure in tests
shogo82148 Nov 26, 2019
39147d9
allow credentials and any origin
shogo82148 Nov 26, 2019
8e21a2e
drop TestNeitherOriginHeaderAndAllowOriginGiven
shogo82148 Nov 26, 2019
be3c512
Revert "drop TestNeitherOriginHeaderAndAllowOriginGiven"
shogo82148 Nov 26, 2019
b60791c
do not allow credentials for these test cases
shogo82148 Nov 26, 2019
4c7e464
Merge pull request #7 from shogo82148/more-flexible-origin-match
shogo82148 Nov 26, 2019
ae560c2
use http.MethodGet instead of goacors.GET
shogo82148 Nov 26, 2019
99a2a36
Bump golang.org/x/net from 0.0.0-20190415214537-1da14a5a36f2 to 0.7.0
dependabot[bot] Feb 24, 2023
757dd89
Merge pull request #9 from shogo82148/dependabot/go_modules/golang.or…
shogo82148 Feb 24, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 44 additions & 0 deletions .github/workflows/go.yml
@@ -0,0 +1,44 @@
name: Test
on: [push, pull_request]

jobs:

test:
strategy:
matrix:
os:
- ubuntu-latest
- windows-latest
- macos-latest
go:
- "1.12"
- "1.13"

name: Test Go ${{ matrix.go }} in ${{ matrix.os }}
runs-on: ${{ matrix.os }}
steps:

- name: Set up Go ${{ matrix.go }}
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go }}

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Test and send coverage
uses: shogo82148/actions-goveralls@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel: true
job-number: ${{ strategy.job-index }}

# notifies that all test jobs are finished.
finish:
needs: test
runs-on: ubuntu-latest
steps:
- uses: shogo82148/actions-goveralls@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

10 changes: 10 additions & 0 deletions LICENSE.md
@@ -0,0 +1,10 @@
The MIT License (MIT)

Copyright (c) 2019 Ichinose Shogo
Copyright (c) 2018 deadcheat

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47 changes: 11 additions & 36 deletions README.md
@@ -1,48 +1,23 @@
[![Build Status](https://travis-ci.org/deadcheat/goacors.svg?branch=master)](https://travis-ci.org/deadcheat/goacors)
[![Coverage Status](https://coveralls.io/repos/github/deadcheat/goacors/badge.svg?branch=master&service=github)](https://coveralls.io/github/deadcheat/goacors?branch=master) [![GoDoc](https://godoc.org/github.com/deadcheat/goacors?status.svg)](https://godoc.org/github.com/deadcheat/goacors)
[![Build Status](https://github.com/shogo82148/goacors/workflows/Test/badge.svg?branch=master)](https://github.com/shogo82148/goacors/actions)
[![Coverage Status](https://coveralls.io/repos/github/shogo82148/goacors/badge.svg?branch=master&service=github)](https://coveralls.io/github/shogo82148/goacors?branch=master) [![GoDoc](https://godoc.org/github.com/shogo82148/goacors?status.svg)](https://godoc.org/github.com/shogo82148/goacors)

# goacors
a cors-header middleware for goa(https://github.com/goadesign/goa)
a cors-header middleware for goa(https://github.com/goadesign/goa).
This is a fork of https://github.com/istyle-inc/goacors

# how to use
1. first, import this from glide, or `go get github.com/deadcheat/goacors`
1. `go get github.com/shogo82148/goacors`
2. write your main.go generated automatically from goagen.

```
service.Use(goacors.WithConfig(service, &goacors.DefaultGoaCORSConfig))
```

or

```
service.Use(goacors.WithConfig(service, &goacors.GoaCORSConfig{
AllowOrigins: []string{"http://example.com"},
AllowMethods: []string{goacors.GET},
}))
```

# Intermediate Match Mode

Intermediate Match Mode is using match logic allow wildcard in host, like `*.example.com`.

```go
service.Use(goacors.WithConfig(service, &goacors.DefaultGoaCORSConfig))
```
NOTIFY

Note that using wild card is not correct for specification of CORS.
And this mode is not recommended for production use.
I implemented this for only testing.
```

## how to use Intermediate Match Mode
To use this mode, you can use goacors.WithConfig like below,
or

```
```go
service.Use(goacors.WithConfig(service, &goacors.GoaCORSConfig{
AllowOrigins: []string{"http://example.com"},
AllowMethods: []string{goacors.GET},
DomainStrategy: goacors.AllowIntermediateMatch,
AllowOrigins: []string{"http://example.com"},
AllowMethods: []string{http.MethodGet},
}))
```

`DomainStrategy` option is added for this. default is `goacors.AllowStrict` and you need to change this to `goacors.AllowIntermediateMatch`

119 changes: 66 additions & 53 deletions cors.go
Expand Up @@ -9,82 +9,95 @@ import (
"github.com/goadesign/goa"
)

// New return middleware implements checking cors with default config
func New(service *goa.Service) goa.Middleware {
return WithConfig(service, &DefaultGoaCORSConfig)
}

// WithConfig create middleware with configure for this
func WithConfig(service *goa.Service, conf *GoaCORSConfig) goa.Middleware {
if conf == nil {
conf = &DefaultGoaCORSConfig
}
if conf.Skipper == nil {
conf.Skipper = DefaultGoaCORSConfig.Skipper
}
if len(conf.AllowOrigins) == 0 {
conf.AllowOrigins = DefaultGoaCORSConfig.AllowOrigins
}
if len(conf.AllowMethods) == 0 {
conf.AllowMethods = DefaultGoaCORSConfig.AllowMethods
}
if conf.DomainStrategy != AllowIntermediateMatch {
conf.DomainStrategy = AllowStrict
// New creates middleware with configure for this
func New(service *goa.Service, conf *Config) goa.Middleware {
// validate allowed origin configure
allowAnyOrigin := false
allowOrigins := make([]originType, len(conf.AllowOrigins))
for i, origin := range conf.AllowOrigins {
if origin == "*" {
allowAnyOrigin = true
break
}
o, err := parseOrigin(origin)
if err != nil {
panic("invalid allowed origin: " + origin)
}
allowOrigins[i] = o
}
allowMethods := strings.Join(conf.AllowMethods, ",")
allowHeaders := strings.Join(conf.AllowHeaders, ",")
exposeHeaders := strings.Join(conf.ExposeHeaders, ",")
maxAge := strconv.Itoa(conf.MaxAge)

var om OriginMatcher
switch conf.DomainStrategy {
case AllowIntermediateMatch:
om = newInterMediateMatcher(conf)
default:
om = newStrictOriginMatcher(conf)
skipper := conf.Skipper
allowMethods := strings.Join(conf.AllowMethods, ", ")
allowHeaders := strings.Join(conf.AllowHeaders, ", ")
exposeHeaders := strings.Join(conf.ExposeHeaders, ", ")
allowCredentials := conf.AllowCredentials
var maxAge string
if conf.MaxAge > 0 {
maxAge = strconv.Itoa(conf.MaxAge)
}

return func(next goa.Handler) goa.Handler {
return func(c context.Context, rw http.ResponseWriter, req *http.Request) error {
// Skipper
if conf.Skipper(c, rw, req) {
if skipper != nil && skipper(c, rw, req) {
return next(c, rw, req)
}
origin := req.Header.Get(HeaderOrigin)
// Check allowed origins
allowedOrigin, _ := om.FindMatchedOrigin(conf.AllowOrigins, origin)

// Simple request
h := rw.Header()

// Check the origin of the request is allowed
var allowedOrigin string
if allowAnyOrigin {
if allowCredentials {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
// When responding to a credentialed request, the server must specify an origin in the value of
// the Access-Control-Allow-Origin header, instead of specifying the "*" wildcard.
allowedOrigin = req.Header.Get(HeaderOrigin)
} else {
allowedOrigin = "*"
}
} else {
origin := req.Header.Get(HeaderOrigin)
if allowed(origin, allowOrigins, allowCredentials) {
allowedOrigin = origin
}
}

if req.Method != http.MethodOptions {
rw.Header().Add(HeaderVary, HeaderOrigin)
rw.Header().Set(HeaderAccessControlAllowOrigin, allowedOrigin)
if conf.AllowCredentials && allowedOrigin != "*" && allowedOrigin != "" {
rw.Header().Set(HeaderAccessControlAllowCredentials, "true")
// handle normal requests
h.Add(HeaderVary, HeaderOrigin)
if allowedOrigin != "" {
h.Set(HeaderAccessControlAllowOrigin, allowedOrigin)
}
if allowCredentials {
h.Set(HeaderAccessControlAllowCredentials, "true")
}
if exposeHeaders != "" {
rw.Header().Set(HeaderAccessControlExposeHeaders, exposeHeaders)
h.Set(HeaderAccessControlExposeHeaders, exposeHeaders)
}
return next(c, rw, req)
}
// Preflight request
rw.Header().Add(HeaderVary, HeaderOrigin)
rw.Header().Add(HeaderVary, HeaderAccessControlRequestMethod)
rw.Header().Add(HeaderVary, HeaderAccessControlRequestHeaders)
rw.Header().Set(HeaderAccessControlAllowOrigin, allowedOrigin)
rw.Header().Set(HeaderAccessControlAllowMethods, allowMethods)
if conf.AllowCredentials && allowedOrigin != "*" && allowedOrigin != "" {
rw.Header().Set(HeaderAccessControlAllowCredentials, "true")

// handle preflight requests
h.Add(HeaderVary, HeaderOrigin)
h.Add(HeaderVary, HeaderAccessControlRequestMethod)
h.Add(HeaderVary, HeaderAccessControlRequestHeaders)
h.Set(HeaderAccessControlAllowOrigin, allowedOrigin)
h.Set(HeaderAccessControlAllowMethods, allowMethods)
if allowCredentials {
h.Set(HeaderAccessControlAllowCredentials, "true")
}
if allowHeaders != "" {
rw.Header().Set(HeaderAccessControlAllowHeaders, allowHeaders)
h.Set(HeaderAccessControlAllowHeaders, allowHeaders)
} else {
header := req.Header.Get(HeaderAccessControlRequestHeaders)
if header != "" {
rw.Header().Set(HeaderAccessControlAllowHeaders, header)
h.Set(HeaderAccessControlAllowHeaders, header)
}
}

if conf.MaxAge > 0 {
rw.Header().Set(HeaderAccessControlMaxAge, maxAge)
if maxAge != "" {
h.Set(HeaderAccessControlMaxAge, maxAge)
}
rw.WriteHeader(http.StatusNoContent)
return nil
Expand Down