Skip to content

Commit

Permalink
Some refactors.
Browse files Browse the repository at this point in the history
  • Loading branch information
garg3133 committed Mar 7, 2024
1 parent 9ad5d68 commit f965321
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 57 deletions.
9 changes: 4 additions & 5 deletions lib/http/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const http = require('http');
const https = require('https');
const dns = require('dns');
const path = require('path');
const {Key} = require('selenium-webdriver');

const HttpUtil = require('./http.js');
const HttpOptions = require('./options.js');
const Auth = require('./auth.js');
const Formatter = require('./formatter.js');
const HttpResponse = require('./response.js');
const Utils = require('./../utils');
const {Key} = require('selenium-webdriver');
const {Logger, isString} = Utils;
const {DEFAULT_RUNNER_EVENTS: {LogCreated}, NightwatchEventHub} = require('../runner/eventHub.js');

Expand Down Expand Up @@ -253,10 +253,9 @@ class HttpRequest extends EventEmitter {

return arg;
});
} else if (this.reqOptions.method === 'POST' &&
this.reqOptions.path.includes('/value') && params.text.startsWith(Key.NULL)) {
params.text = '****';
params.value = '****'.split('');
} else if (this.reqOptions.method === 'POST' && this.reqOptions.path.endsWith('/value') && params.text.startsWith(Key.NULL)) {
params.text = '*******';
params.value = '*******'.split('');
}

const content = ` Request ${[this.reqOptions.method, this.hostname + this.reqOptions.path, retryStr + ' '].join(' ')}`;
Expand Down
7 changes: 0 additions & 7 deletions test/sampletests/checkValueRedacted/passwordValueRedacted.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe('value redaction in setPassword', function() {
test('test setPassword', async browser => {
browser
.setPassword('#weblogin', 'password')
.setValue('#weblogin', 'simpletext');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const {settings} = common;
const NightwatchClient = common.require('index.js');
const MockServer = require('../../../../lib/mockserver.js');

describe('setPassword check', function() {

describe('setPassword report check', function() {
before(function(done) {
this.server = MockServer.init();
this.server.on('listening', () => done());
Expand All @@ -21,8 +20,8 @@ describe('setPassword check', function() {
});

it('client.setPassword() value redacted in rawHttpOutput', async function() {

let sendKeysRedactedMockCalled = false;
let sendKeysPasswordMockCalled = false;
let sendKeysNormalMockCalled = false;
let globalReporterCalled = false;

Mocks.createNewW3CSession({
Expand All @@ -35,7 +34,8 @@ describe('setPassword check', function() {
statusCode: 200,
response: {
value: null
}
},
times: 2
});

MockServer.addMock({
Expand All @@ -46,18 +46,9 @@ describe('setPassword check', function() {
value: null
},
onRequest: () => {
sendKeysRedactedMockCalled = true;
}
});

MockServer.addMock({
url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/clear',
method: 'POST',
statusCode: 200,
response: {
value: null
sendKeysPasswordMockCalled = true;
}
});
}, true);

MockServer.addMock({
url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/value',
Expand All @@ -67,47 +58,38 @@ describe('setPassword check', function() {
value: null
},
onRequest: () => {
sendKeysRedactedMockCalled = true;
sendKeysNormalMockCalled = true;
}
});
}, true);

const testsPath = [
path.join(__dirname, '../../../../sampletests/checkValueRedacted/passwordValueRedacted.js')
path.join(__dirname, '../../../../sampletests/passwordvalueRedacted/passwordValueRedacted.js')
];

const globals = {
calls: 0,
waitForConditionTimeout: 10,
waitForConditionPollInterval: 10,
reporter(results) {
globalReporterCalled = true;

assert.strictEqual(sendKeysRedactedMockCalled, true);
assert.strictEqual(sendKeysPasswordMockCalled, true);
assert.strictEqual(sendKeysNormalMockCalled, true);
assert.strictEqual(results.errmessages.length, 0);

const rawHttpOutput = results.modules.passwordValueRedacted.rawHttpOutput;
const requests = rawHttpOutput.filter((req) => req[1].includes('element/5cc459b8-36a8-3042-8b4a-258883ea642b/value') && req[1].includes('Request POST'));

// There are two calls in passwordValueRedacted.js test, setPassword('password') & setValue('simpletext')
// We first filter out the https requests that can contain these values as logs. Then we check these logs for our values.
// There are two flags:
// `foundRedactedText` which tracks if any redacted text is present in the log. So the value should be false only.
// `foundNonRedactedText` tracks that the non redacted values SHOULD be present in the logs. So the value should become true.

let foundRedactedText = false;
let foundNonRedactedText = false;

for (var request of requests) {
if (request[2].includes('password')) {
foundRedactedText = true;
}
if (request[2].includes('simpletext')) {
foundNonRedactedText = true;
}
}
assert.strictEqual(foundRedactedText, false);
assert.strictEqual(foundNonRedactedText, true);

const requests = rawHttpOutput
.filter((req) => {
return req[1].includes('element/5cc459b8-36a8-3042-8b4a-258883ea642b/value') &&
req[1].includes('Request POST');
});

assert.strictEqual(requests.length, 2);

// First request (setPassword) should contain redacted value
assert.strictEqual(requests[0][2].includes('password'), false);
assert.strictEqual(requests[0][2].includes('*******'), true);

// Second request (setValue) should NOT contain redacted value
assert.strictEqual(requests[1][2].includes('simpletext'), true);
assert.strictEqual(requests[1][2].includes('*******'), false);
}
};

Expand Down

0 comments on commit f965321

Please sign in to comment.