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

feat: add known webui addresses to CORS conf #1956

Merged
merged 3 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 47 additions & 7 deletions src/daemon/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,29 @@ function applyDefaults (ipfsd) {
writeConfigFile(ipfsd, config)
}

function getGatewayPort (config) {
let gatewayUrl = null

if (Array.isArray(config.Addresses.Gateway)) {
gatewayUrl = config.Addresses.Gateway.find(v => v.includes('127.0.0.1'))
} else {
gatewayUrl = config.Addresses.Gateway
}

const gw = parseCfgMultiaddr(gatewayUrl)
return gw.nodeAddress().port
}

// Apply one-time updates to the config of IPFS node.
// This is the place where we execute fixes and performance tweaks for existing users.
function migrateConfig (ipfsd) {
// Bump revision number when new migration rule is added
const REVISION = 1
const REVISION = 2
const REVISION_KEY = 'daemonConfigRevision'
const CURRENT_REVISION = store.get(REVISION_KEY, 0)

// Migration is applied only once per revision
if (store.get(REVISION_KEY) >= REVISION) return
if (CURRENT_REVISION >= REVISION) return

// Read config
let config = null
Expand All @@ -78,11 +92,37 @@ function migrateConfig (ipfsd) {
return
}

// Cleanup https://github.com/ipfs-shipyard/ipfs-desktop/issues/1631
if (config.Discovery && config.Discovery.MDNS && config.Discovery.MDNS.enabled) {
config.Discovery.MDNS.Enabled = config.Discovery.MDNS.Enabled || true
delete config.Discovery.MDNS.enabled
changed = true
if (CURRENT_REVISION <= 0) {
// Cleanup https://github.com/ipfs-shipyard/ipfs-desktop/issues/1631
if (config.Discovery && config.Discovery.MDNS && config.Discovery.MDNS.enabled) {
config.Discovery.MDNS.Enabled = config.Discovery.MDNS.Enabled || true
delete config.Discovery.MDNS.enabled
changed = true
}
}

if (CURRENT_REVISION <= 1) {
const api = config.API || {}
const httpHeaders = api.HTTPHeaders || {}
const accessControlAllowOrigin = httpHeaders['Access-Control-Allow-Origin'] || []

const addURL = url => {
if (!accessControlAllowOrigin.includes(url)) {
accessControlAllowOrigin.push(url)
return true
}
return false
}

const addedWebUI = addURL('https://webui.ipfs.io')
const addedGw = addURL(`http://webui.ipfs.io.ipns.localhost:${getGatewayPort(config)}`)

if (addedWebUI || addedGw) {
httpHeaders['Access-Control-Allow-Origin'] = accessControlAllowOrigin
api.HTTPHeaders = httpHeaders
config.API = api
changed = true
}
}

// TODO: update config.Swarm.ConnMgr.*
Expand Down
63 changes: 62 additions & 1 deletion test/e2e/launch.e2e.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ test.describe.serial('Application launch', async () => {
expect(peerId).toBe(expectedId)
})

test('applies config migration to existing config', async () => {
test('applies config migration (MDNS.enabled)', async () => {
// create preexisting, initialized repo and config
const { repoPath, configPath, peerId: expectedId } = await makeRepository({ start: false })

Expand All @@ -106,6 +106,67 @@ test.describe.serial('Application launch', async () => {
expect(config.Discovery.MDNS.Enabled).toBeTruthy()
})

test('applies config migration (Web UI CORS 1)', async () => {
// create preexisting, initialized repo and config
const { repoPath, configPath, peerId: expectedId } = await makeRepository({ start: false })

const initConfig = fs.readJsonSync(configPath)
initConfig.API.HTTPHeaders['Access-Control-Allow-Origin'] = ['https://127.0.0.1:4040']
fs.writeJsonSync(configPath, initConfig, { spaces: 2 })

const { app } = await startApp({ repoPath })
const { peerId } = await daemonReady(app)
expect(peerId).toBe(expectedId)

const config = fs.readJsonSync(configPath)
// ensure app has migrated config
expect(config.API.HTTPHeaders['Access-Control-Allow-Origin']).toEqual([
'https://127.0.0.1:4040',
'https://webui.ipfs.io',
'http://webui.ipfs.io.ipns.localhost:0' // ipfsd 'test' profile uses '/ip4/127.0.0.1/tcp/0'
])
})

test('applies config migration (Web UI CORS 2)', async () => {
// create preexisting, initialized repo and config
const { repoPath, configPath, peerId: expectedId } = await makeRepository({ start: false })

const initConfig = fs.readJsonSync(configPath)
initConfig.API.HTTPHeaders['Access-Control-Allow-Origin'] = []
fs.writeJsonSync(configPath, initConfig, { spaces: 2 })

const { app } = await startApp({ repoPath })
const { peerId } = await daemonReady(app)
expect(peerId).toBe(expectedId)

const config = fs.readJsonSync(configPath)
// ensure app has migrated config
expect(config.API.HTTPHeaders['Access-Control-Allow-Origin']).toEqual([
'https://webui.ipfs.io',
'http://webui.ipfs.io.ipns.localhost:0' // ipfsd 'test' profile uses '/ip4/127.0.0.1/tcp/0'
])
})

test('applies config migration (Web UI CORS 3)', async () => {
// create preexisting, initialized repo and config
const { repoPath, configPath, peerId: expectedId } = await makeRepository({ start: false })

const initConfig = fs.readJsonSync(configPath)
delete initConfig.API.HTTPHeaders
fs.writeJsonSync(configPath, initConfig, { spaces: 2 })

const { app } = await startApp({ repoPath })
const { peerId } = await daemonReady(app)
expect(peerId).toBe(expectedId)

const config = fs.readJsonSync(configPath)
// ensure app has migrated config
expect(config.API.HTTPHeaders['Access-Control-Allow-Origin']).toEqual([
'https://webui.ipfs.io',
'http://webui.ipfs.io.ipns.localhost:0' // ipfsd 'test' profile uses '/ip4/127.0.0.1/tcp/0'
])
})

test('starts with repository with "IPFS_PATH/api" file and no daemon running', async () => {
// create "remote" repo
const { ipfsd } = await makeRepository({ start: true })
Expand Down