Skip to content

Commit c41e555

Browse files
author
Michael Bumann
committed
added support for CORS HTTP headers
Sets the Cross-Origin Resource Sharing (CORS) headers in a global express middleware. This option is configurable and disabled by default. To enable CORS use the following config entry: [http] cors: true
1 parent dce0dee commit c41e555

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

src/lib/config.coffee

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ module.exports = (dnschain) ->
7676
internalTLSPort: 2500 # Not accessible from the internet, used internally only
7777
internalAdminPort: 3000 # Not accessible from the internet, used internally only
7878
host: '0.0.0.0' # what we bind to. 0.0.0.0 for the whole internet
79+
cors: false
7980
redis:
8081
socket: '127.0.0.1:6379' # or UNIX domain socket path
8182
oldDNS:

src/lib/http.coffee

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ module.exports = (dnschain) ->
2525
@rateLimiting = gConf.get 'rateLimiting:http'
2626
app = express()
2727

28+
if gConf.get('http:cors')
29+
@log.warn "Enabling CORS HTTP headers"
30+
app.use (req, res, next) =>
31+
res.header "Access-Control-Allow-Origin", "*"
32+
res.header "Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"
33+
next()
34+
2835
# Openname spec defined here:
2936
# - https://github.com/okTurtles/openname-specifications/blob/resolvers/resolvers.md
3037
# - https://github.com/openname/openname-specifications/blob/master/resolvers.md

test/https.coffee

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ describe 'https', ->
6666
console.info "Result: #{stdout}".bold
6767
stdout.should.containEql data
6868

69+
it 'should disable CORS by default', ->
70+
getAsync("http://localhost:#{gConf.get 'http:port'}/v1/namecoin/key/d%2Fokturtles").then (res) ->
71+
assert.equal(gConf.get('http:cors'), false)
72+
assert.equal(res.header['access-control-allow-headers'], undefined)
73+
assert.equal(res.header['access-control-allow-origin'], undefined)
74+
6975
it 'should shutdown successfully', ->
7076
server.shutdown() # returns a promise. Mocha should handle that properly
7177

0 commit comments

Comments
 (0)