Skip to content

Commit cd8a852

Browse files
author
Christian Holm
authored
Escape username and password in wss (#1107)
1 parent e2b69b1 commit cd8a852

File tree

4 files changed

+24
-24
lines changed

4 files changed

+24
-24
lines changed

src/security/WSSecurity.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import * as crypto from 'crypto';
33
import { ISecurity } from '../types';
4-
import { passwordDigest } from '../utils';
4+
import { passwordDigest, xmlEscape } from '../utils';
55

66
const validPasswordTypes = ['PasswordDigest', 'PasswordText'];
77

@@ -87,7 +87,7 @@ export class WSSecurity implements ISecurity {
8787
nonce = nHash.digest('base64');
8888
}
8989
if (this._passwordType === 'PasswordText') {
90-
password = '<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' + this._password + '</wsse:Password>';
90+
password = '<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' + xmlEscape(this._password) + '</wsse:Password>';
9191
if (nonce) {
9292
password += '<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">' + nonce + '</wsse:Nonce>';
9393
}
@@ -103,7 +103,7 @@ export class WSSecurity implements ISecurity {
103103
'xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">' +
104104
timeStampXml +
105105
'<wsse:UsernameToken xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="SecurityToken-' + created + '">' +
106-
'<wsse:Username>' + this._username + '</wsse:Username>' +
106+
'<wsse:Username>' + xmlEscape(this._username) + '</wsse:Username>' +
107107
password +
108108
(this._hasTokenCreated ? '<wsu:Created>' + created + '</wsu:Created>' : '') +
109109
'</wsse:UsernameToken>' +

src/utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,19 @@ export function splitQName<T>(nsName: T) {
4848
name: topLevelName.substring(prefixOffset + 1),
4949
};
5050
}
51+
52+
export function xmlEscape(obj) {
53+
if (typeof (obj) === 'string') {
54+
if (obj.substr(0, 9) === '<![CDATA[' && obj.substr(-3) === ']]>') {
55+
return obj;
56+
}
57+
return obj
58+
.replace(/&/g, '&amp;')
59+
.replace(/</g, '&lt;')
60+
.replace(/>/g, '&gt;')
61+
.replace(/"/g, '&quot;')
62+
.replace(/'/g, '&apos;');
63+
}
64+
65+
return obj;
66+
}

src/wsdl/index.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,13 @@ import * as url from 'url';
1616
import { HttpClient } from '../http';
1717
import { NamespaceContext } from '../nscontext';
1818
import { IOptions } from '../types';
19-
import { findPrefix, splitQName, TNS_PREFIX } from '../utils';
19+
import { findPrefix, splitQName, TNS_PREFIX, xmlEscape } from '../utils';
2020
import * as elements from './elements';
2121

2222
const debug = debugBuilder('node-soap');
2323

2424
const XSI_URI = 'http://www.w3.org/2001/XMLSchema-instance';
2525

26-
function xmlEscape(obj) {
27-
if (typeof (obj) === 'string') {
28-
if (obj.substr(0, 9) === '<![CDATA[' && obj.substr(-3) === ']]>') {
29-
return obj;
30-
}
31-
return obj
32-
.replace(/&/g, '&amp;')
33-
.replace(/</g, '&lt;')
34-
.replace(/>/g, '&gt;')
35-
.replace(/"/g, '&quot;')
36-
.replace(/'/g, '&apos;');
37-
}
38-
39-
return obj;
40-
}
41-
4226
const trimLeft = /^[\s\xA0]+/;
4327
const trimRight = /[\s\xA0]+$/;
4428

test/security/WSSecurity.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ describe('WSSecurity', function() {
3939
});
4040

4141
it('should insert a WSSecurity when postProcess is called', function() {
42-
var username = 'myUser';
43-
var password = 'myPass';
42+
var username = 'my&User';
43+
var password = 'my&Pass';
4444
var options = {
4545
passwordType: 'PassWordText',
4646
hasNonce: true,
@@ -59,10 +59,10 @@ describe('WSSecurity', function() {
5959
xml.should.containEql('<wsse:UsernameToken ');
6060
xml.should.containEql('xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" ');
6161
xml.should.containEql('wsu:Id="SecurityToken-');
62-
xml.should.containEql('<wsse:Username>myUser</wsse:Username>');
62+
xml.should.containEql('<wsse:Username>my&amp;User</wsse:Username>');
6363
xml.should.containEql('<wsse:Password ');
6464
xml.should.containEql('Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">');
65-
xml.should.containEql('myPass</wsse:Password>');
65+
xml.should.containEql('my&amp;Pass</wsse:Password>');
6666
xml.should.containEql('<wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">');
6767
xml.should.containEql('</wsse:Nonce>');
6868
xml.should.containEql('<wsu:Created>');

0 commit comments

Comments
 (0)