| 1 |
describe('security options', function() { |
| 2 |
beforeEach(function() { |
| 3 |
this.server = makeServer() |
| 4 |
clearWorkArea() |
| 5 |
}) |
| 6 |
afterEach(function() { |
| 7 |
this.server.restore() |
| 8 |
clearWorkArea() |
| 9 |
}) |
| 10 |
|
| 11 |
it('can disable a single elt', function() { |
| 12 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 13 |
|
| 14 |
var btn = make('<button hx-disable hx-get="/test">Initial</button>') |
| 15 |
btn.click() |
| 16 |
this.server.respond() |
| 17 |
btn.innerHTML.should.equal('Initial') |
| 18 |
}) |
| 19 |
|
| 20 |
it('can disable a parent elt', function() { |
| 21 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 22 |
|
| 23 |
var div = make('<div hx-disable><button id="b1" hx-get="/test">Initial</button></div>') |
| 24 |
var btn = byId('b1') |
| 25 |
btn.click() |
| 26 |
this.server.respond() |
| 27 |
btn.innerHTML.should.equal('Initial') |
| 28 |
}) |
| 29 |
|
| 30 |
it('can disable a child elt', function() { |
| 31 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 32 |
|
| 33 |
var div = make('<div><button id="b1" hx-disable hx-get="/test">Initial</button></div>') |
| 34 |
var btn = byId('b1') |
| 35 |
btn.click() |
| 36 |
this.server.respond() |
| 37 |
btn.innerHTML.should.equal('Initial') |
| 38 |
}) |
| 39 |
|
| 40 |
it('can disable a single elt dynamically', function() { |
| 41 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 42 |
|
| 43 |
var btn = make('<button id="b1" hx-get="/test">Initial</button>') |
| 44 |
btn.click() |
| 45 |
this.server.respond() |
| 46 |
btn.innerHTML.should.equal('Clicked!') |
| 47 |
|
| 48 |
this.server.respondWith('GET', '/test', 'Clicked a second time') |
| 49 |
|
| 50 |
btn.setAttribute('hx-disable', '') |
| 51 |
btn.click() |
| 52 |
this.server.respond() |
| 53 |
btn.innerHTML.should.equal('Clicked!') |
| 54 |
}) |
| 55 |
|
| 56 |
it('can disable a single elt dynamically & enable it back', function() { |
| 57 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 58 |
|
| 59 |
var btn = make('<button id="b1" hx-get="/test">Initial</button>') |
| 60 |
btn.click() |
| 61 |
this.server.respond() |
| 62 |
btn.innerHTML.should.equal('Clicked!') |
| 63 |
|
| 64 |
this.server.respondWith('GET', '/test', 'Clicked a second time') |
| 65 |
|
| 66 |
btn.setAttribute('hx-disable', '') |
| 67 |
btn.click() |
| 68 |
this.server.respond() |
| 69 |
btn.innerHTML.should.equal('Clicked!') |
| 70 |
|
| 71 |
btn.removeAttribute('hx-disable') |
| 72 |
htmx.process(btn) |
| 73 |
btn.click() |
| 74 |
this.server.respond() |
| 75 |
btn.innerHTML.should.equal('Clicked a second time') |
| 76 |
}) |
| 77 |
|
| 78 |
it('can disable a single a tag dynamically & enable it back with boost', function() { |
| 79 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 80 |
|
| 81 |
var btn = make('<a id="b1" hx-boost="true" href="/test" hx-target="this">Initial</button>') |
| 82 |
btn.click() |
| 83 |
this.server.respond() |
| 84 |
btn.innerHTML.should.equal('Clicked!') |
| 85 |
|
| 86 |
this.server.respondWith('GET', '/test', 'Clicked a second time') |
| 87 |
|
| 88 |
btn.setAttribute('hx-disable', '') |
| 89 |
btn.click() |
| 90 |
this.server.respond() |
| 91 |
btn.innerHTML.should.equal('Clicked!') |
| 92 |
|
| 93 |
btn.removeAttribute('hx-disable') |
| 94 |
htmx.process(btn) |
| 95 |
btn.click() |
| 96 |
this.server.respond() |
| 97 |
btn.innerHTML.should.equal('Clicked a second time') |
| 98 |
}) |
| 99 |
|
| 100 |
it('can disable a single parent elt dynamically', function() { |
| 101 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 102 |
|
| 103 |
var div = make('<div><button id="b1" hx-get="/test">Initial</button></div>') |
| 104 |
var btn = byId('b1') |
| 105 |
btn.click() |
| 106 |
this.server.respond() |
| 107 |
btn.innerHTML.should.equal('Clicked!') |
| 108 |
|
| 109 |
this.server.respondWith('GET', '/test', 'Clicked a second time') |
| 110 |
|
| 111 |
div.setAttribute('hx-disable', '') |
| 112 |
btn.click() |
| 113 |
this.server.respond() |
| 114 |
btn.innerHTML.should.equal('Clicked!') |
| 115 |
}) |
| 116 |
|
| 117 |
it('can disable a single parent elt dynamically & enable it back', function() { |
| 118 |
this.server.respondWith('GET', '/test', 'Clicked!') |
| 119 |
|
| 120 |
var div = make('<div><button id="b1" hx-get="/test">Initial</button></div>') |
| 121 |
var btn = byId('b1') |
| 122 |
btn.click() |
| 123 |
this.server.respond() |
| 124 |
btn.innerHTML.should.equal('Clicked!') |
| 125 |
|
| 126 |
this.server.respondWith('GET', '/test', 'Clicked a second time') |
| 127 |
|
| 128 |
div.setAttribute('hx-disable', '') |
| 129 |
btn.click() |
| 130 |
this.server.respond() |
| 131 |
btn.innerHTML.should.equal('Clicked!') |
| 132 |
|
| 133 |
div.removeAttribute('hx-disable') |
| 134 |
htmx.process(div) |
| 135 |
btn.click() |
| 136 |
this.server.respond() |
| 137 |
btn.innerHTML.should.equal('Clicked a second time') |
| 138 |
}) |
| 139 |
|
| 140 |
it('can make egress cross site requests when htmx.config.selfRequestsOnly is disabled', function(done) { |
| 141 |
this.timeout(4000) |
| 142 |
htmx.config.selfRequestsOnly = false |
| 143 |
|
| 144 |
var listener = htmx.on('htmx:sendError', function() { |
| 145 |
htmx.config.selfRequestsOnly = true |
| 146 |
htmx.off('htmx:sendError', listener) |
| 147 |
done() |
| 148 |
}) |
| 149 |
this.server.restore() |
| 150 |
|
| 151 |
var btn = make('<button hx-get="https://hypermedia.systems/www/test">Initial</button>') |
| 152 |
btn.click() |
| 153 |
}) |
| 154 |
|
| 155 |
it('can make a real local data uri request when selfRequestOnly false', function(done) { |
| 156 |
htmx.config.selfRequestsOnly = false |
| 157 |
var pathVerifier = htmx.on('htmx:validateUrl', function(evt) { |
| 158 |
if (evt.detail.sameHost === false && evt.detail.url.protocol !== 'data:') { |
| 159 |
evt.preventDefault() |
| 160 |
} |
| 161 |
}) |
| 162 |
this.server.restore() |
| 163 |
var btn = make('<button hx-get="data:,foo">Initial</button>') |
| 164 |
btn.click() |
| 165 |
htmx.config.selfRequestsOnly = true |
| 166 |
setTimeout(function() { |
| 167 |
htmx.off('htmx:validateUrl', pathVerifier) |
| 168 |
btn.innerHTML.should.equal('foo') |
| 169 |
done() |
| 170 |
}, 30) |
| 171 |
}) |
| 172 |
|
| 173 |
it('can disable hx-on on a single elt', function() { |
| 174 |
var btn = make("<button hx-disable hx-on:click='window.foo = true'>Foo</button>") |
| 175 |
btn.click() |
| 176 |
should.equal(window.foo, undefined) |
| 177 |
delete window.foo |
| 178 |
}) |
| 179 |
|
| 180 |
it('can disable hx-on on a parent elt', function() { |
| 181 |
var div = make("<div hx-disable><button id='b1' hx-on:click='window.foo = true'>Foo</button></div>") |
| 182 |
var btn = byId('b1') |
| 183 |
btn.click() |
| 184 |
should.equal(window.foo, undefined) |
| 185 |
delete window.foo |
| 186 |
}) |
| 187 |
|
| 188 |
it('can disable hx-on on a single elt dynamically', function() { |
| 189 |
var btn = make("<button hx-on:click='window.foo = true'>Foo</button>") |
| 190 |
btn.click() |
| 191 |
should.equal(window.foo, true) |
| 192 |
delete window.foo |
| 193 |
|
| 194 |
btn.setAttribute('hx-disable', '') |
| 195 |
|
| 196 |
btn.click() |
| 197 |
should.equal(window.foo, undefined) |
| 198 |
delete window.foo |
| 199 |
}) |
| 200 |
|
| 201 |
it('can disable hx-on on a parent elt dynamically', function() { |
| 202 |
var div = make("<div><button id='b1' hx-on:click='window.foo = true'>Foo</button></div>") |
| 203 |
var btn = byId('b1') |
| 204 |
btn.click() |
| 205 |
should.equal(window.foo, true) |
| 206 |
delete window.foo |
| 207 |
|
| 208 |
div.setAttribute('hx-disable', '') |
| 209 |
|
| 210 |
btn.click() |
| 211 |
should.equal(window.foo, undefined) |
| 212 |
delete window.foo |
| 213 |
}) |
| 214 |
|
| 215 |
it("can't make egress cross site requests when htmx.config.selfRequestsOnly is true", function(done) { |
| 216 |
this.timeout(4000) |
| 217 |
|
| 218 |
var listener = htmx.on('htmx:invalidPath', function() { |
| 219 |
htmx.off('htmx:invalidPath', listener) |
| 220 |
done() |
| 221 |
}) |
| 222 |
this.server.restore() |
| 223 |
|
| 224 |
var btn = make('<button hx-get="https://hypermedia.systems/www/test">Initial</button>') |
| 225 |
btn.click() |
| 226 |
}) |
| 227 |
|
| 228 |
it('can cancel egress request based on htmx:validateUrl event', function(done) { |
| 229 |
this.timeout(4000) |
| 230 |
htmx.config.selfRequestsOnly = false |
| 231 |
|
| 232 |
var pathVerifier = htmx.on('htmx:validateUrl', function(evt) { |
| 233 |
evt.preventDefault() |
| 234 |
}) |
| 235 |
var listener = htmx.on('htmx:invalidPath', function() { |
| 236 |
htmx.off('htmx:invalidPath', listener) |
| 237 |
htmx.off('htmx:validateUrl', pathVerifier) |
| 238 |
done() |
| 239 |
}) |
| 240 |
this.server.restore() |
| 241 |
|
| 242 |
var btn = make('<button hx-get="https://hypermedia.systems/www/test">Initial</button>') |
| 243 |
btn.click() |
| 244 |
htmx.config.selfRequestsOnly = true |
| 245 |
}) |
| 246 |
|
| 247 |
it('can cancel egress request based on htmx:validateUrl event, sameHost is false', function(done) { |
| 248 |
this.timeout(4000) |
| 249 |
htmx.config.selfRequestsOnly = false |
| 250 |
|
| 251 |
var pathVerifier = htmx.on('htmx:validateUrl', function(evt) { |
| 252 |
if (evt.detail.sameHost === false) { |
| 253 |
evt.preventDefault() |
| 254 |
} |
| 255 |
}) |
| 256 |
var listener = htmx.on('htmx:invalidPath', function() { |
| 257 |
htmx.off('htmx:validateUrl', pathVerifier) |
| 258 |
htmx.off('htmx:invalidPath', listener) |
| 259 |
done() |
| 260 |
}) |
| 261 |
this.server.restore() |
| 262 |
|
| 263 |
var btn = make('<button hx-get="https://hypermedia.systems/www/test">Initial</button>') |
| 264 |
btn.click() |
| 265 |
htmx.config.selfRequestsOnly = true |
| 266 |
}) |
| 267 |
|
| 268 |
it('can cancel egress request based on htmx:validateUrl event and then allow a request', function(done) { |
| 269 |
htmx.config.selfRequestsOnly = false |
| 270 |
var requestCount = 0 |
| 271 |
var pathVerifier = htmx.on('htmx:validateUrl', function(evt) { |
| 272 |
requestCount = requestCount + 1 |
| 273 |
if (requestCount === 1) { |
| 274 |
evt.preventDefault() |
| 275 |
} |
| 276 |
}) |
| 277 |
this.server.restore() |
| 278 |
var btn = make('<button hx-get="data:,foo">Initial</button>') |
| 279 |
btn.click() |
| 280 |
setTimeout(function() { |
| 281 |
btn.innerHTML.should.not.equal('foo') |
| 282 |
btn.click() |
| 283 |
setTimeout(function() { |
| 284 |
htmx.off('htmx:validateUrl', pathVerifier) |
| 285 |
htmx.config.selfRequestsOnly = true |
| 286 |
btn.innerHTML.should.equal('foo') |
| 287 |
done() |
| 288 |
}, 30) |
| 289 |
}, 30) |
| 290 |
}) |
| 291 |
|
| 292 |
it('can disable script tag support with htmx.config.allowScriptTags', function() { |
| 293 |
var globalWasCalled = false |
| 294 |
window.callGlobal = function() { |
| 295 |
globalWasCalled = true |
| 296 |
} |
| 297 |
try { |
| 298 |
htmx.config.allowScriptTags = false |
| 299 |
this.server.respondWith('GET', '/test', '<div><script>callGlobal()</script></div>') |
| 300 |
var div = make("<div hx-get='/test'></div>") |
| 301 |
div.click() |
| 302 |
this.server.respond() |
| 303 |
globalWasCalled.should.equal(false) |
| 304 |
} finally { |
| 305 |
htmx.config.allowScriptTags = true |
| 306 |
delete window.callGlobal |
| 307 |
} |
| 308 |
}) |
| 309 |
}) |