Skip to content
This repository was archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Fix magnet link handling
Browse files Browse the repository at this point in the history
Auditors: @darkdh

Fix #5981
  • Loading branch information
bbondy committed Jan 18, 2017
1 parent 0bd01d0 commit 4564618
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 32 deletions.
23 changes: 0 additions & 23 deletions app/browser/webtorrent.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const electron = require('electron')
const ipc = electron.ipcMain
const messages = require('../../js/constants/messages')
const Filtering = require('../filtering')
const { getTargetMagnetUrl } = require('../../js/lib/appUrlUtil')

// Set to see communication between WebTorrent and torrent viewer tabs
const DEBUG_IPC = false
Expand All @@ -13,29 +11,8 @@ if (DEBUG_IPC) console.log('WebTorrent IPC debugging enabled')
let server = null
let channels = {}

function handleMangetUrl (details, isPrivate) {
const result = {
resourceName: module.exports.resourceName,
redirectURL: null,
cancel: false
}

if (details.resourceType !== 'mainFrame') {
return result
}

const magnetUrl = getTargetMagnetUrl(details.url)
if (magnetUrl) {
result.redirectUrl = magnetUrl
}

return result
}

// Receive messages via the window process, ultimately from the UI in a <webview> process
function init (state, action) {
Filtering.registerBeforeRequestFilteringCB(handleMangetUrl)

if (DEBUG_IPC) console.log('WebTorrent IPC init')
ipc.on(messages.TORRENT_MESSAGE, function (e, msg) {
if (server === null) {
Expand Down
4 changes: 2 additions & 2 deletions app/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ let generateBraveManifest = () => {
let generateTorrentManifest = () => {
let cspDirectives = {
'default-src': '\'self\'',
'media-src': '\'self\'',
'media-src': '\'self\' http://localhost:*',
'form-action': '\'none\'',
'referrer': 'no-referrer',
'style-src': '\'self\' \'unsafe-inline\'',
Expand All @@ -175,7 +175,7 @@ let generateTorrentManifest = () => {
let devServer = 'localhost:' + process.env.npm_package_config_port
cspDirectives['default-src'] = '\'self\' http://' + devServer
cspDirectives['connect-src'] = '\'self\' http://' + devServer + ' ws://' + devServer
cspDirectives['media-src'] = '\'self\' http://' + devServer
cspDirectives['media-src'] = '\'self\' http://localhost:* http://' + devServer
cspDirectives['frame-src'] = '\'self\' http://' + devServer
cspDirectives['style-src'] = '\'self\' \'unsafe-inline\' http://' + devServer
}
Expand Down
16 changes: 15 additions & 1 deletion app/filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,19 @@ function registerForDownloadListener (session) {
})
}

function registerForMagnetHandler (session) {
const webtorrentUrl = appUrlUtil.getTorrentExtUrl('webtorrent.html')
try {
if (getSetting(settings.TORRENT_VIEWER_ENABLED)) {
session.protocol.registerNavigatorHandler('magnet', `${webtorrentUrl}#%s`)
} else {
session.protocol.unregisterNavigatorHandler('magnet', `${webtorrentUrl}#%s`)
}
} catch (e) {
console.warn('Could not register magnet URL handler, are oyu using the latest electron?')

This comment has been minimized.

Copy link
@darkdh

darkdh Jan 18, 2017

Member

s/oyu/you/

}
}

function initSession (ses, partition) {
initializedPartitions[partition] = true
registeredSessions[partition] = ses
Expand All @@ -531,7 +544,8 @@ function initForPartition (partition) {
registerForBeforeSendHeaders,
registerPermissionHandler,
registerForHeadersReceived,
registerForDownloadListener]
registerForDownloadListener,
registerForMagnetHandler]
let options = {}
if (isSessionPartition(partition)) {
options.parent_partition = ''
Expand Down
4 changes: 1 addition & 3 deletions js/components/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const debounce = require('../lib/debounce')
const getSetting = require('../settings').getSetting
const config = require('../constants/config')
const settings = require('../constants/settings')
const {aboutUrls, getTargetMagnetUrl, isSourceMagnetUrl, isSourceAboutUrl, isTargetAboutUrl, getTargetAboutUrl, getBaseUrl, isIntermediateAboutPage} = require('../lib/appUrlUtil')
const {aboutUrls, isSourceMagnetUrl, isSourceAboutUrl, isTargetAboutUrl, getTargetAboutUrl, getBaseUrl, isIntermediateAboutPage} = require('../lib/appUrlUtil')
const {isFrameError} = require('../../app/common/lib/httpUtil')
const locale = require('../l10n')
const appConfig = require('../constants/appConfig')
Expand Down Expand Up @@ -260,8 +260,6 @@ class Frame extends ImmutableComponent {

if (isSourceAboutUrl(newSrc)) {
newSrc = getTargetAboutUrl(newSrc)
} else if (isTorrentViewerURL(newSrc)) {
newSrc = getTargetMagnetUrl(newSrc)
}

let guestInstanceId = null
Expand Down
6 changes: 4 additions & 2 deletions js/lib/appUrlUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ module.exports.isTargetAboutUrl = function (input) {
* Example: getTargetMagnetUrl('magnet:...') -> 'chrome-extension://<...>.html#magnet:...'
*/
module.exports.getTargetMagnetUrl = function (input) {
if (!input.startsWith('magnet:')) return null
if (!input.startsWith('magnet:')) {
return null
}
const url = module.exports.getTorrentExtUrl('webtorrent.html')
return [url, input].join('#')
}
Expand All @@ -175,7 +177,7 @@ module.exports.getTargetMagnetUrl = function (input) {
*/
module.exports.getSourceMagnetUrl = function (input) {
if (getBaseUrl(input) !== module.exports.getTorrentExtUrl('webtorrent.html')) return null
const url = getHash(input)
const url = decodeURIComponent(getHash(input))
return url
}

Expand Down
2 changes: 1 addition & 1 deletion js/webtorrent/entry.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require('../../node_modules/font-awesome/css/font-awesome.css')

// UI state object. Pure function from `state` -> React element.
const state = {
torrentID: window.location.hash.substring(1),
torrentID: window.decodeURIComponent(window.location.hash.substring(1)),
parsedTorrent: null,
client: null,
torrent: null,
Expand Down

1 comment on commit 4564618

@darkdh
Copy link
Member

@darkdh darkdh commented on 4564618 Jan 19, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

Please sign in to comment.