Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
aa0039cb
Commit
aa0039cb
authored
Oct 03, 2017
by
Sheetal Umesh Kumar
Committed by
GitHub
Oct 03, 2017
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #550 from hypothesis/add-search-uris-postmessage
Send search URIs to other frames by postMessage()
parents
23163ab2
9ea6b7d4
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
212 additions
and
1 deletion
+212
-1
app.js
src/sidebar/app.js
+3
-1
cross-origin-rpc.js
src/sidebar/cross-origin-rpc.js
+70
-0
cross-origin-rpc-test.js
src/sidebar/test/cross-origin-rpc-test.js
+139
-0
No files found.
src/sidebar/app.js
View file @
aa0039cb
...
@@ -4,6 +4,7 @@ var addAnalytics = require('./ga');
...
@@ -4,6 +4,7 @@ var addAnalytics = require('./ga');
var
disableOpenerForExternalLinks
=
require
(
'./util/disable-opener-for-external-links'
);
var
disableOpenerForExternalLinks
=
require
(
'./util/disable-opener-for-external-links'
);
var
getApiUrl
=
require
(
'./get-api-url'
);
var
getApiUrl
=
require
(
'./get-api-url'
);
var
serviceConfig
=
require
(
'./service-config'
);
var
serviceConfig
=
require
(
'./service-config'
);
var
crossOriginRPC
=
require
(
'./cross-origin-rpc.js'
);
require
(
'../shared/polyfills'
);
require
(
'../shared/polyfills'
);
var
raven
;
var
raven
;
...
@@ -248,7 +249,8 @@ module.exports = angular.module('h', [
...
@@ -248,7 +249,8 @@ module.exports = angular.module('h', [
.
config
(
configureRoutes
)
.
config
(
configureRoutes
)
.
config
(
configureToastr
)
.
config
(
configureToastr
)
.
run
(
setupHttp
);
.
run
(
setupHttp
)
.
run
(
crossOriginRPC
.
server
.
start
);
processAppOpts
();
processAppOpts
();
...
...
src/sidebar/cross-origin-rpc.js
0 → 100644
View file @
aa0039cb
'use strict'
;
/**
* Begin responding to JSON-RPC requests from frames on other origins.
*
* Register a window.postMessage() event listener that receives and responds to
* JSON-RPC requests sent by frames on other origins using postMessage() as the
* transport layer.
*
* Only frames whose origin is in the rpcAllowedOrigins config setting will be
* responded to.
*
* This is a very partial implementation of a JSON-RPC 2.0 server:
*
* http://www.jsonrpc.org/specification
*
* The only part that we support so far is receiving JSON-RPC 2.0 requests (not
* notifications) without any parameters and sending back a successful
* response. Notifications (JSON-RPC calls that don't require a response),
* method parameters, and error responses are not yet supported.
*
*/
// @ngInject
function
start
(
annotationUI
,
settings
,
$window
)
{
$window
.
addEventListener
(
'message'
,
function
receiveMessage
(
event
)
{
let
allowedOrigins
=
settings
.
rpcAllowedOrigins
||
[];
if
(
!
allowedOrigins
.
includes
(
event
.
origin
))
{
return
;
}
// The entire JSON-RPC request object is contained in the postMessage()
// data param.
let
jsonRpcRequest
=
event
.
data
;
event
.
source
.
postMessage
(
jsonRpcResponse
(
jsonRpcRequest
),
event
.
origin
);
});
/** Return a JSON-RPC response to the given JSON-RPC request object. */
function
jsonRpcResponse
(
request
)
{
// The set of methods that clients can call.
let
methods
=
{
'searchUris'
:
annotationUI
.
searchUris
,
};
let
method
=
methods
[
request
.
method
];
let
response
=
{
'jsonrpc'
:
'2.0'
,
'id'
:
request
.
id
,
};
if
(
method
)
{
response
.
result
=
method
();
}
else
{
response
.
error
=
{
'code'
:
-
32601
,
'message'
:
'Method not found'
,
};
}
return
response
;
}
}
module
.
exports
=
{
server
:
{
start
:
start
,
},
};
src/sidebar/test/cross-origin-rpc-test.js
0 → 100644
View file @
aa0039cb
'use strict'
;
var
crossOriginRPC
=
require
(
'../cross-origin-rpc'
);
describe
(
'crossOriginRPC'
,
function
()
{
describe
(
'server'
,
function
()
{
let
addedListener
;
// The postMessage() listener that the server adds.
let
fakeAnnotationUI
;
let
fakeWindow
;
let
settings
;
let
source
;
beforeEach
(
function
()
{
fakeAnnotationUI
=
{
searchUris
:
sinon
.
stub
().
returns
(
'THE_SEARCH_URIS'
),
};
fakeWindow
=
{
addEventListener
:
sinon
.
stub
().
callsFake
(
function
(
type
,
listener
)
{
// Save the registered listener function in a variable so test code
// can access it later.
addedListener
=
listener
;
}),
};
settings
=
{
rpcAllowedOrigins
:
[
'https://allowed1.com'
,
'https://allowed2.com'
],
};
source
=
{
postMessage
:
sinon
.
stub
()
};
});
/**
* Directly call the postMessage() listener func that the server
* registered. This simulates what would happen if window.postMessage()
* were called.
*/
function
postMessage
(
event
)
{
addedListener
(
event
);
}
it
(
'adds a postMessage() event listener function'
,
function
()
{
crossOriginRPC
.
server
.
start
(
fakeAnnotationUI
,
{},
fakeWindow
);
assert
.
isTrue
(
fakeWindow
.
addEventListener
.
calledOnce
);
assert
.
isTrue
(
fakeWindow
.
addEventListener
.
calledWith
(
'message'
));
});
it
(
'sends a response with the result from the called method'
,
function
()
{
crossOriginRPC
.
server
.
start
(
fakeAnnotationUI
,
settings
,
fakeWindow
);
postMessage
({
data
:
{
method
:
'searchUris'
,
id
:
42
},
origin
:
'https://allowed1.com'
,
source
:
source
,
});
assert
.
isTrue
(
source
.
postMessage
.
calledOnce
);
assert
.
isTrue
(
source
.
postMessage
.
calledWithExactly
(
{
jsonrpc
:
'2.0'
,
id
:
42
,
result
:
'THE_SEARCH_URIS'
,
},
'https://allowed1.com'
));
});
[
{},
{
rpcAllowedOrigins
:
[]
},
{
rpcAllowedOrigins
:
[
'https://allowed1.com'
,
'https://allowed2.com'
]
},
].
forEach
(
function
(
settings
)
{
it
(
"doesn't respond if the origin isn't allowed"
,
function
()
{
crossOriginRPC
.
server
.
start
(
fakeAnnotationUI
,
settings
,
fakeWindow
);
postMessage
({
origin
:
'https://notallowed.com'
,
data
:
{
method
:
'searchUris'
,
id
:
42
},
source
:
source
,
});
assert
.
isTrue
(
source
.
postMessage
.
notCalled
);
});
});
it
(
"responds with an error if there's no method"
,
function
()
{
crossOriginRPC
.
server
.
start
(
fakeAnnotationUI
,
settings
,
fakeWindow
);
let
jsonRpcRequest
=
{
id
:
42
};
// No "method" member.
postMessage
({
origin
:
'https://allowed1.com'
,
data
:
jsonRpcRequest
,
source
:
source
,
});
assert
.
isTrue
(
source
.
postMessage
.
calledOnce
);
assert
.
isTrue
(
source
.
postMessage
.
calledWithExactly
(
{
jsonrpc
:
'2.0'
,
id
:
42
,
error
:
{
code
:
-
32601
,
message
:
'Method not found'
,
},
},
'https://allowed1.com'
));
});
[
'unknownMethod'
,
null
,
].
forEach
(
function
(
method
)
{
it
(
'responds with an error if the method is unknown'
,
function
()
{
crossOriginRPC
.
server
.
start
(
fakeAnnotationUI
,
settings
,
fakeWindow
);
postMessage
({
origin
:
'https://allowed1.com'
,
data
:
{
method
:
method
,
id
:
42
},
source
:
source
,
});
assert
.
isTrue
(
source
.
postMessage
.
calledOnce
);
assert
.
isTrue
(
source
.
postMessage
.
calledWithExactly
(
{
jsonrpc
:
'2.0'
,
id
:
42
,
error
:
{
code
:
-
32601
,
message
:
'Method not found'
,
},
},
'https://allowed1.com'
));
});
});
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment