Skip to content

Commit

Permalink
[Bug] changed path for error handling for apis (#459)
Browse files Browse the repository at this point in the history
* [Bug] changed path for error handling for apis

Signed-off-by: sumukhswamy <sumukhhs@amazon.com>

* added input validation for pipeline and index

Signed-off-by: sumukhswamy <sumukhhs@amazon.com>

---------

Signed-off-by: sumukhswamy <sumukhhs@amazon.com>
  • Loading branch information
sumukhswamy authored Oct 22, 2024
1 parent d01d2ff commit 18b87d9
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion server/routes/dsl_route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ export function registerDslRoute(router: IRouter, openSearchServiceSetup: OpenS
const start = performance.now();
try {
let resp;
const invalidCharactersPattern = /[\s,:\"*+\/\\|?#><]/;
if (index !== index.toLowerCase() || index.startsWith('_') || index.startsWith('-') || invalidCharactersPattern.test(index)) {
resBody.errorMessage1 = {
statusCode: 400,
body: 'Invalid Index or missing',
};
}
if (pipeline !== '*' && pipeline !== '_none' && pipeline !== '' && !(/^[a-zA-Z0-9_\-*]+(,[a-zA-Z0-9_\-*]+)*$/.test(pipeline))){
resBody.errorMessage1 = {
statusCode: 400,
body: 'Invalid Pipepline',
};
}
if(dataSourceEnabled && dataSourceId1){
const client = context.dataSource.opensearch.legacy.getClient(dataSourceId1);
resp = await client.callAPI('search', params);
Expand Down Expand Up @@ -107,6 +120,16 @@ export function registerDslRoute(router: IRouter, openSearchServiceSetup: OpenS
const start = performance.now();
try {
let resp;
const invalidCharactersPattern = /[\s,:\"*+\/\\|?#><]/;
if (index !== index.toLowerCase() || index.startsWith('_') || index.startsWith('-') || invalidCharactersPattern.test(index)) {
throw new Error("Index invalid or missing.");
}
if (pipeline !== '*' && pipeline !== '_none' && pipeline !== '' && !(/^[a-zA-Z0-9_\-*]+(,[a-zA-Z0-9_\-*]+)*$/.test(pipeline))){
resBody.errorMessage1 = {
statusCode: 400,
body: 'Invalid Pipepline',
};
}
if(dataSourceEnabled && dataSourceId2){
const client = context.dataSource.opensearch.legacy.getClient(dataSourceId2);
resp = await client.callAPI('search', params);
Expand Down Expand Up @@ -195,7 +218,7 @@ export function registerDslRoute(router: IRouter, openSearchServiceSetup: OpenS
);
if (error.statusCode !== 404) console.error(error);
return response.custom({
statusCode: error.statusCode || 500,
statusCode: error.statusCode || 400,
body: error.message,
});
}
Expand Down

0 comments on commit 18b87d9

Please sign in to comment.