diff --git a/lib/models/solid-host.js b/lib/models/solid-host.js index 1a926e97e..663546f62 100644 --- a/lib/models/solid-host.js +++ b/lib/models/solid-host.js @@ -72,12 +72,14 @@ class SolidHost { allowsSessionFor (userId, origin) { // Allow no user or an empty origin if (!userId || !origin) return true - // Allow the server's main domain - if (origin === this.serverUri) return true - // Allow the user's subdomain - const userIdHost = userId.replace(/([^:/])\/.*/, '$1') - if (origin === userIdHost) return true - // Disallow everything else + // Allow the server and subdomains + const originHost = getHostName(origin) + const serverHost = getHostName(this.serverUri) + if (originHost === serverHost) return true + if (originHost.endsWith('.' + serverHost)) return true + // Allow the user's own domain + const userHost = getHostName(userId) + if (originHost === userHost) return true return false } @@ -109,4 +111,9 @@ class SolidHost { } } +function getHostName (url) { + const match = url.match(/^\w+:\/*([^/]+)/) + return match ? match[1] : '' +} + module.exports = SolidHost diff --git a/test/unit/solid-host-test.js b/test/unit/solid-host-test.js index 04cdad308..19b7e95d7 100644 --- a/test/unit/solid-host-test.js +++ b/test/unit/solid-host-test.js @@ -55,23 +55,23 @@ describe('SolidHost', () => { }) it('should allow a userId with empty origin', () => { - expect(host.allowsSessionFor('https://user.test.local/profile/card#me', '')).to.be.true + expect(host.allowsSessionFor('https://user.own/profile/card#me', '')).to.be.true }) it('should allow a userId with the user subdomain as origin', () => { - expect(host.allowsSessionFor('https://user.test.local/profile/card#me', 'https://user.test.local')).to.be.true + expect(host.allowsSessionFor('https://user.own/profile/card#me', 'https://user.own')).to.be.true }) - it('should disallow a userId with another subdomain as origin', () => { - expect(host.allowsSessionFor('https://user.test.local/profile/card#me', 'https://other.test.local')).to.be.false + it('should allow a userId with the server domain as origin', () => { + expect(host.allowsSessionFor('https://user.own/profile/card#me', 'https://test.local')).to.be.true }) - it('should allow a userId with the server domain as origin', () => { - expect(host.allowsSessionFor('https://user.test.local/profile/card#me', 'https://test.local')).to.be.true + it('should allow a userId with a server subdomain as origin', () => { + expect(host.allowsSessionFor('https://user.own/profile/card#me', 'https://other.test.local')).to.be.true }) it('should disallow a userId from a different domain', () => { - expect(host.allowsSessionFor('https://user.test.local/profile/card#me', 'https://other.remote')).to.be.false + expect(host.allowsSessionFor('https://user.own/profile/card#me', 'https://other.remote')).to.be.false }) })