fix!: remove image element proxy check, and allow proxyRouteIsAvoided to inspect the body
MODIFY
lib/basedriver/driver.js
lib/basedriver/driver.js
@@ -470,10 +470,11 @@ class BaseDriver extends Protocol {
* should be made available to overridden methods.
* @param {string} method - HTTP method of the route
* @param {string} url - url of the route
+ * @param {?*} body - webdriver request body
*
* @returns {boolean} - whether the route should be avoided
*/
- proxyRouteIsAvoided (sessionId, method, url) {
+ proxyRouteIsAvoided (sessionId, method, url/*, body*/) {
for (let avoidSchema of this.getProxyAvoidList(sessionId)) {
if (!_.isArray(avoidSchema) || avoidSchema.length !== 2) {
throw new Error('Proxy avoidance must be a list of pairs');

MODIFY
lib/protocol/protocol.js
lib/protocol/protocol.js
@@ -10,25 +10,13 @@ import B from 'bluebird';
import {
formatResponseValue, formatStatus,
} from './helpers';
-import {
- MJSONWP_ELEMENT_KEY, W3C_ELEMENT_KEY, MAX_LOG_BODY_LENGTH, PROTOCOLS,
- DEFAULT_BASE_PATH, IMAGE_ELEMENT_PREFIX,
-} from '../constants';
+import { MAX_LOG_BODY_LENGTH, PROTOCOLS, DEFAULT_BASE_PATH } from '../constants';
import SESSIONS_CACHE from './sessions-cache';
const CREATE_SESSION_COMMAND = 'createSession';
const DELETE_SESSION_COMMAND = 'deleteSession';
-const IMG_EL_BODY_RE = new RegExp(
- `"(${W3C_ELEMENT_KEY}|${MJSONWP_ELEMENT_KEY})":\s*` + // eslint-disable-line no-useless-escape
- `"${IMAGE_ELEMENT_PREFIX}[^"]+"`
-);
-const IMG_EL_URL_RE = new RegExp(
- `/(element|screenshot)` +
- `/${IMAGE_ELEMENT_PREFIX}[^/]+`
-);
-
class Protocol {}
function determineProtocol (desiredCapabilities, requiredCapabilities, capabilities) {
@@ -438,25 +426,7 @@ function driverShouldDoJwpProxy (driver, req, command) {
// validate avoidance schema, and say we shouldn't proxy if anything in the
// avoid list matches our req
- if (driver.proxyRouteIsAvoided(req.params.sessionId, req.method, req.originalUrl)) {
- return false;
- }
-
- // if it looks like we have an image element in the url (as a route
- // parameter), never proxy. Just look for our image element prefix in allowed
- // positions (either after an 'element' or 'screenshot' path segment), and
- // ensure the prefix is followed by something
- if (IMG_EL_URL_RE.test(req.originalUrl)) {
- return false;
- }
-
-
- // also if it looks like we have an image element in the request body (as
- // a JSON parameter), never proxy. Basically check against a regexp of the
- // json string of the body, where we know what the form of an image element
- // must be
- const stringBody = JSON.stringify(req.body);
- if (stringBody && IMG_EL_BODY_RE.test(stringBody)) {
+ if (driver.proxyRouteIsAvoided(req.params.sessionId, req.method, req.originalUrl, req.body)) {
return false;
}

DELETE
test/protocol/protocol-specs.js
-// transpile:mocha
-
-import '../..'; // NOTE: For some reason this file needs to be imported to prevent a babel error
-import chai from 'chai';
-import sinon from 'sinon';
-import chaiAsPromised from 'chai-as-promised';
-import { driverShouldDoJwpProxy } from '../../lib/protocol/protocol';
-import {
- MJSONWP_ELEMENT_KEY, W3C_ELEMENT_KEY, IMAGE_ELEMENT_PREFIX
-} from '../../lib/constants';
-import BaseDriver from '../../lib/basedriver/driver';
-
-chai.should();
-chai.use(chaiAsPromised);
-
-describe('Protocol', function () {
-
- describe('#driverShouldDoJwpProxy', function () {
- it('should not proxy if an image element is found in request url', function () {
- const d = new BaseDriver();
- sinon.stub(d, 'proxyActive').returns(true);
- sinon.stub(d, 'proxyRouteIsAvoided').returns(false);
- const hasImageElements = [
- `/session/:sessionId/element/${IMAGE_ELEMENT_PREFIX}bar`,
- `/session/:sessionId/element/${IMAGE_ELEMENT_PREFIX}bar/click`,
- `/session/:sessionId/element/${IMAGE_ELEMENT_PREFIX}bar/submit`,
- `/session/:sessionId/screenshot/${IMAGE_ELEMENT_PREFIX}bar`,
- ];
- const noImageElements = [
- `/session/:sessionId/element/${IMAGE_ELEMENT_PREFIX}`,
- `/session/:sessionId/screenshot/${IMAGE_ELEMENT_PREFIX}`,
- `/session/:sessionId/element/bar${IMAGE_ELEMENT_PREFIX}`,
- '/session/:sessionId/element/element123',
- '/session/:sessionId/title',
- `/session/:sessionId/notelement/${IMAGE_ELEMENT_PREFIX}bar`,
- ];
- for (let testCase of hasImageElements) {
- const req = {body: {}, params: {}, originalUrl: testCase};
- driverShouldDoJwpProxy(d, req, null).should.be.false;
- }
- for (let testCase of noImageElements) {
- const req = {body: {}, params: {}, originalUrl: testCase};
- driverShouldDoJwpProxy(d, req, null).should.be.true;
- }
- });
- it('should not proxy if an image element is found in request body', function () {
- const d = new BaseDriver();
- sinon.stub(d, 'proxyActive').returns(true);
- sinon.stub(d, 'proxyRouteIsAvoided').returns(false);
- const hasImageElements = [{
- [W3C_ELEMENT_KEY]: `${IMAGE_ELEMENT_PREFIX}bar`,
- }, {
- [W3C_ELEMENT_KEY]: `${IMAGE_ELEMENT_PREFIX}foo`,
- }, {
- [MJSONWP_ELEMENT_KEY]: `${IMAGE_ELEMENT_PREFIX}bar`,
- }];
- const noImageElements = [{
- [IMAGE_ELEMENT_PREFIX]: 'foo',
- }, {
- [W3C_ELEMENT_KEY]: `${IMAGE_ELEMENT_PREFIX}`,
- }, {
- [MJSONWP_ELEMENT_KEY]: `${IMAGE_ELEMENT_PREFIX}`,
- }, {
- foo: 'bar',
- }, {
- [W3C_ELEMENT_KEY]: 'bar',
- }, {
- [MJSONWP_ELEMENT_KEY]: 'bar',
- }, {
- foo: `${IMAGE_ELEMENT_PREFIX}bar`,
- }, {
- foo: `bar${IMAGE_ELEMENT_PREFIX}`
- }, {
- [W3C_ELEMENT_KEY]: `bar${IMAGE_ELEMENT_PREFIX}`
- }, {
- [MJSONWP_ELEMENT_KEY]: `bar${IMAGE_ELEMENT_PREFIX}`
- }];
- for (let testCase of hasImageElements) {
- const req = {body: testCase, params: {}};
- driverShouldDoJwpProxy(d, req, null).should.be.false;
- }
- for (let testCase of noImageElements) {
- const req = {body: testCase, params: {}};
- driverShouldDoJwpProxy(d, req, null).should.be.true;
- }
-
- });
- });
-});

fix!: remove image element proxy check, and allow proxyRouteIsAvoided to inspect the body