Skip to content

Commit 8d776f4

Browse files
committed
Merge pull request #167 from eve-bright/allow-hostname-only-config-for-whitelist
Allow whitelist config to contain hostnames without protocols
2 parents f75be3e + 97e620f commit 8d776f4

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

lib/helpers.coffee

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class neo.helpers
5252

5353
@mergeDocumentArrays = (arr1, arr2) ->
5454
[].concat(arr1, arr2)
55-
.reduce((tot, curr) ->
55+
.reduce((tot, curr) ->
5656
return tot if tot.done.indexOf(curr.content) > -1
5757
tot.done.push(curr.content)
5858
tot.out.push(curr)
@@ -142,10 +142,15 @@ class neo.helpers
142142
@stripNGAttributes = (string = '') ->
143143
string.replace(/(\s+(ng|data|x)[^\s=]*\s*=\s*("[^"]*"|'[^']*'|[\w\-.:]+\s*))/ig, '')
144144

145-
@hostIsAllowed = (hostname, whitelist) ->
145+
@hostIsAllowed = (uri, whitelist) ->
146146
return true if whitelist is '*'
147-
whitelisted_hosts = if whitelist? and whitelist isnt '' then whitelist.split(",") else ['http://guides.neo4j.com', 'https://guides.neo4j.com', 'http://localhost', 'https://localhost']
148-
hostname in whitelisted_hosts
147+
host_without_port = document.createElement('a')
148+
host_without_port.setAttribute('href', uri)
149+
hostnamePlusProtocol = host_without_port.protocol + '//' + host_without_port.hostname
150+
hostname = host_without_port.hostname
151+
152+
whitelisted_hosts = if whitelist? and whitelist isnt '' then whitelist.split(",") else ['guides.neo4j.com', 'localhost']
153+
hostname in whitelisted_hosts || hostnamePlusProtocol in whitelisted_hosts
149154

150155
@getBrowserName = ->
151156
return 'Opera' if !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0
@@ -155,7 +160,7 @@ class neo.helpers
155160
return 'Internet Explorer' if !!document.documentMode
156161
return 'Edge' if !!window.StyleMedia
157162
'Unknown'
158-
163+
159164
@getServerHostname = (Settings) ->
160165
if Settings.host then Settings.host else location.href
161166

@@ -167,7 +172,7 @@ class neo.helpers
167172
flat = [].concat.apply(flat, [].concat.apply(that.flattenArray(item))) if Array.isArray item
168173
flat
169174
, [])
170-
175+
171176
@getUrlParam = (name, theLocation) ->
172177
return no unless theLocation
173178
out = []

test/spec/other/utils.coffee

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe 'Utils', () ->
4747
text = """
4848
cypher queries
4949
will often be more
50-
legible on multiple lines
50+
legible on multiple lines
5151
than squashed onto a single line
5252
"""
5353
expect(Utils.firstWord text).toBe 'cypher'
@@ -119,22 +119,30 @@ describe 'Utils', () ->
119119
expect(Utils.cleanHTML text).toBe 'hello <p>xxx</p>'
120120

121121
it 'should respect whitelist from server', ->
122-
host = 'http://first.com'
123-
whitelist = 'http://second.com,http://third.com'
124-
expect(Utils.hostIsAllowed host, '*').toBe yes
125-
expect(Utils.hostIsAllowed host, null).toBe no
126-
expect(Utils.hostIsAllowed host, '').toBe no
127-
expect(Utils.hostIsAllowed host, host).toBe yes
128-
expect(Utils.hostIsAllowed host, whitelist).toBe no
129-
expect(Utils.hostIsAllowed 'http://guides.neo4j.com', null).toBe yes
130-
expect(Utils.hostIsAllowed 'http://guides.neo4j.com', '').toBe yes
122+
whitelist = 'https://second.com,fourth.com'
123+
expect(Utils.hostIsAllowed 'http://first.com', whitelist).toBe no
124+
expect(Utils.hostIsAllowed 'http://second.com', whitelist).toBe no
125+
expect(Utils.hostIsAllowed 'https://second.com', whitelist).toBe yes
126+
expect(Utils.hostIsAllowed 'http://fourth.com', whitelist).toBe yes
127+
expect(Utils.hostIsAllowed 'https://fourth.com', whitelist).toBe yes
128+
129+
it 'should treat * from server as all hosts allowed', ->
130+
expect(Utils.hostIsAllowed 'anything', '*').toBe yes
131+
132+
it 'should use defaults if no whitelist specified', ->
133+
expect(Utils.hostIsAllowed 'http://anything.com', null).toBe no
134+
expect(Utils.hostIsAllowed 'http://anything.com', '').toBe no
135+
expect(Utils.hostIsAllowed 'guides.neo4j.com', null).toBe yes
136+
expect(Utils.hostIsAllowed 'guides.neo4j.com', '').toBe yes
137+
expect(Utils.hostIsAllowed 'localhost', '').toBe yes
138+
expect(Utils.hostIsAllowed 'localhost', '').toBe yes
131139

132140
it 'should merge two arrays with documents without duplicates', ->
133141
arr1 = [getDocument('MATCH (n) RETURN n'), getDocument('//My script\nRETURN "me"')]
134142
arr2 = [getDocument('MATCH (n)-(m) RETURN n'), getDocument('//My script\nRETURN "me"'), getDocument('RETURN 1')]
135143
expect(JSON.stringify(Utils.mergeDocumentArrays(arr1, arr2)))
136-
.toBe(JSON.stringify([getDocument('MATCH (n) RETURN n'),
137-
getDocument('//My script\nRETURN "me"'),
144+
.toBe(JSON.stringify([getDocument('MATCH (n) RETURN n'),
145+
getDocument('//My script\nRETURN "me"'),
138146
getDocument('MATCH (n)-(m) RETURN n'),
139147
getDocument('RETURN 1')]))
140148

@@ -155,7 +163,7 @@ describe 'Utils', () ->
155163
{location: 'http://neo4j.com/?param=', paramName: 'param', expect: undefined},
156164
{location: 'http://neo4j.com/', paramName: 'param', expect: undefined}
157165
]
158-
urls.forEach((tCase) ->
166+
urls.forEach((tCase) ->
159167
res = Utils.getUrlParam tCase.paramName, tCase.location
160168
val = if res then res[0] else res
161169
expect(val).toBe(tCase.expect)

0 commit comments

Comments
 (0)