Skip to content

Commit

Permalink
Merge pull request #768 from snyk/fix/pass-body-as-is-to-primary-for-…
Browse files Browse the repository at this point in the history
…older-clients

fix: pass body unmodified to primary from secondary srv [HYB-532]
  • Loading branch information
aarlaud authored May 13, 2024
2 parents 6a41d48 + 694eeb9 commit f001b36
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
14 changes: 11 additions & 3 deletions lib/server/routesHandlers/httpRequestHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,23 @@ export const overloadHttpRequestWithConnectionDetailsMiddleware = async (
const url = new URL(`http://${req.hostname}${req.url}`);
url.hostname = req.hostname.replace(/-[0-9]{1,2}\./, '.');
url.searchParams.append('connection_role', 'primary');
logger.debug({}, 'Making request to primary');

const postFilterPreparedRequest: PostFilterPreparedRequest = {
url: url.toString(),
headers: req.headers,
method: req.method,
};
if (req.body) {
postFilterPreparedRequest.body = JSON.stringify(req.body);
if (
req.method == 'POST' ||
req.method == 'PUT' ||
req.method == 'PATCH'
) {
postFilterPreparedRequest.body = req.body;
}
logger.debug(
{ url: req.url, method: req.method },
'Making request to primary',
);
try {
const httpResponse = await makeStreamingRequestToDownstream(
postFilterPreparedRequest,
Expand Down
27 changes: 24 additions & 3 deletions test/unit/old-client-redirect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ describe('Testing older clients specific logic', () => {
return [200, { test: 'value' }];
});
const app = express();
app.use(bodyParser.json());
app.use(
bodyParser.raw({
type: (req) =>
req.headers['content-type'] !==
'application/vnd.broker.stream+octet-stream',
limit: '10mb',
}),
);
app.all(
'/broker/:token/*',
overloadHttpRequestWithConnectionDetailsMiddleware,
Expand All @@ -71,7 +78,14 @@ describe('Testing older clients specific logic', () => {
return [200, requestBody];
});
const app = express();
app.use(bodyParser.json());
app.use(
bodyParser.raw({
type: (req) =>
req.headers['content-type'] !==
'application/vnd.broker.stream+octet-stream',
limit: '10mb',
}),
);
app.all(
'/broker/:token/*',
overloadHttpRequestWithConnectionDetailsMiddleware,
Expand All @@ -98,7 +112,14 @@ describe('Testing older clients specific logic', () => {
return [200, fileJson];
});
const app = express();
app.use(bodyParser.json());
app.use(
bodyParser.raw({
type: (req) =>
req.headers['content-type'] !==
'application/vnd.broker.stream+octet-stream',
limit: '10mb',
}),
);
app.all(
'/broker/:token/*',
overloadHttpRequestWithConnectionDetailsMiddleware,
Expand Down

0 comments on commit f001b36

Please sign in to comment.