Skip to content

Commit

Permalink
Merge pull request #646 from snyk/fix/artifactory-validation-text-not…
Browse files Browse the repository at this point in the history
…-json

fix:validate urls returning non json response
  • Loading branch information
aarlaud authored Oct 17, 2023
2 parents 31b2e8a + 1a5e113 commit 81d863d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/client/utils/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { log as logger } from '../../logs/logger';
import version from '../../common/utils/version';
import { sanitise } from '../../logs/logger';
import { makeRequestToDownstream } from '../../common/http/request';
import { isJson } from '../../common/utils/json';

const credsFromHeader = (s) => {
if (s.indexOf(' ') >= 0) {
Expand Down Expand Up @@ -101,7 +102,10 @@ export const checkCredentials = async (

logger.error(data, response && response.body, 'Systemcheck failed');
} else {
const parsedBodyResponse = JSON.parse(response.body || {});
const parsedBodyResponse = isJson(response.headers)
? JSON.parse(response.body)
: response.body;

// const responseToReturn = response
response.body = parsedBodyResponse;
if (process.env.JEST_WORKER_ID) {
Expand All @@ -117,7 +121,6 @@ export const checkCredentials = async (
if (process.env.JEST_WORKER_ID) {
data['testError'] = error;
}

data['ok'] = false;
data['error'] = error;
}
Expand Down
28 changes: 28 additions & 0 deletions test/functional/systemcheck.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,34 @@ describe('broker client systemcheck endpoint', () => {
).not.toBeTruthy();
});

it('good validation url, custom endpoint, no authorization, no json response', async () => {
bc = await createBrokerClient({
brokerServerUrl: `http://localhost:${bs.port}`,
brokerToken: 'broker-token-12345',
type: 'client',
brokerClientValidationUrl: `http://localhost:${tws.port}/echo/textresponse`,
brokerSystemcheckPath: '/custom-systemcheck',
});
await waitForBrokerClientConnection(bs);

const response = await axiosClient.get(
`http://localhost:${bc.port}/custom-systemcheck`,
);
expect(response.data).toBeInstanceOf(Array);
const systemCheckBody = response.data[0];
expect(response.status).toEqual(200);
expect(systemCheckBody).toStrictEqual({
brokerClientValidationMethod: 'GET',
brokerClientValidationTimeoutMs: expect.any(Number),
brokerClientValidationUrl: `http://localhost:${tws.port}/echo/textresponse`,
brokerClientValidationUrlStatusCode: 200,
maskedCredentials: null,
ok: true,
testResponse: expect.any(Object),
});
expect(systemCheckBody.testResponse.body).toEqual('OK');
});

it('good validation url, authorization header', async () => {
bc = await createBrokerClient({
brokerServerUrl: `http://localhost:${bs.port}`,
Expand Down
9 changes: 9 additions & 0 deletions test/setup/test-web-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ const applyEchoRoutes = (app: Express) => {
},
);

// mimics functionality of https://httpbin.org/headers
echoRouter.get(
'/echo/textresponse',
(req: express.Request, resp: express.Response) => {
resp.status(200);
resp.send('OK');
},
);

echoRouter.post(
'/echo-headers/:param?',
(req: express.Request, resp: express.Response) => {
Expand Down

0 comments on commit 81d863d

Please sign in to comment.