@contrast/assess 1.27.1 → 1.27.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -102,6 +102,21 @@ module.exports = function (core) {
102
102
  sourceContext: store.assess
103
103
  };
104
104
 
105
+ // track the headers and the url.
106
+ //
107
+ // note that req.headers and req.headersDistinct are now (as of v15.1.0)
108
+ // lazily computed using an accessor property.
109
+ //
110
+ // there is no need to track headersDistinct because they are not
111
+ // referenced prior to this point. and, when they are referenced, node
112
+ // populates them with references to the (what will be after code below)
113
+ // already-tracked values in rawHeaders. But headers have already been
114
+ // referenced by node before the 'request' event is emitted by the server,
115
+ // so headers need to be tracked independently of rawHeaders. The way
116
+ // node handles the headers is convoluted; it's easier/safer to track the
117
+ // headers as they are. An attacker could use knowledge of node's handling
118
+ // to craft their attack.
119
+ //
105
120
  [
106
121
  {
107
122
  context: 'req.headers',
@@ -117,18 +132,57 @@ module.exports = function (core) {
117
132
  ...sourceInfo,
118
133
  }
119
134
  ].forEach((sourceData) => {
120
- const { inputType } = sourceData;
121
135
  try {
122
136
  dataflow.sources.handle(sourceData);
123
137
  } catch (err) {
138
+ const { inputType } = sourceData;
124
139
  logger.error({ err, inputType, sourceName }, 'unable to handle http source');
125
140
  }
126
141
  });
127
142
 
128
- for (let i = 0; i < req.rawHeaders.length; i += 2) {
129
- const header = toLowerCase(req.rawHeaders[i]);
130
- req.rawHeaders[i + 1] = req.headers[header];
143
+
144
+ //
145
+ // now track the rawHeaders. headers are complicated because they appear
146
+ // three times: headers, headersDistinct, and rawHeaders and we want to
147
+ // create only one event per header value. that turns out not to be as
148
+ // easy/possible as it sounds, due to the way node handles req.headers.
149
+ //
150
+ // see node's lib/_http_incoming.js for details. interesting optimizations
151
+ // and quirky handling per the RFC. some duplicate headers are joined by
152
+ // default, some are not.
153
+ //
154
+ // but we have to track rawHeaders. they are copied to a separate array
155
+ // because the dataflow.sources.handle() doesn't know about an array where
156
+ // only odd indexes are to be tracked.
157
+ //
158
+ // even though we could track the rawHeaders' keys, we don't because they
159
+ // are not used by any application that i'm aware of. it's easy enough to
160
+ // add here if we find there is an edge case where the application bypasses
161
+ // headers and headersDistinct and uses rawHeaders directly.
162
+ //
163
+ const headerValues = [];
164
+ for (let i = 1; i < req.rawHeaders.length; i += 2) {
165
+ headerValues.push(req.rawHeaders[i]);
166
+ }
167
+
168
+ try {
169
+ dataflow.sources.handle({
170
+ context: 'req.headers',
171
+ inputType: InputType.HEADER,
172
+ data: headerValues,
173
+ ...sourceInfo,
174
+ });
175
+ } catch (err) {
176
+ logger.error({ err, inputType: InputType.HEADER, sourceName }, 'unable to handle http source');
131
177
  }
178
+
179
+ //
180
+ // now that the raw headers are tracked, put each tracked value back
181
+ //
182
+ for (let i = 0; i < headerValues.length; i++) {
183
+ req.rawHeaders[(i << 1) + 1] = headerValues[i];
184
+ }
185
+
132
186
  } catch (err) {
133
187
  logger.error({ err, funcKey: data.funcKey }, 'Error during Assess request handling');
134
188
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/assess",
3
- "version": "1.27.1",
3
+ "version": "1.27.2",
4
4
  "description": "Contrast service providing framework-agnostic Assess support",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",