Skip to content

Ca adjustment#119

Open
JessB2000 wants to merge 7 commits intomainfrom
CA_ADJUSTMENT
Open

Ca adjustment#119
JessB2000 wants to merge 7 commits intomainfrom
CA_ADJUSTMENT

Conversation

@JessB2000
Copy link
Copy Markdown
Contributor

No description provided.

* Se é só um filename legado, reconstrói com userId.
*/
private resolveObjectKey(key: string, userId?: string): string {
if (key.includes('/')) {

Check failure

Code scanning / CodeQL

Type confusion through parameter tampering Critical

Potential type confusion as
this HTTP request parameter
may be either an array or a string.

Copilot Autofix

AI about 1 month ago

In general, to fix this class of problem you must validate the runtime type of any user-controlled value before calling string methods or using it in string concatenation, and reject or sanitize unexpected types (like arrays or objects). For query parameters in Express/NestJS this usually means: if the value is not a string, return a 400 Bad Request.

Here, the best fix with minimal behavioral change is:

  1. In MinioClientController, ensure that objectKey from @Query('key') is a single string and not an array or other type. If it is not a string or is empty, throw BadRequestException. Because all existing service methods already assume a string, doing this at the controller boundary centralizes validation and avoids needing extra checks deeper in the service.
  2. Optionally, we can keep MinioClientService.resolveObjectKey as-is, because after the controller fixes, it will only receive strings. However, for extra safety, we can make resolveObjectKey robust by guarding its includes call with a simple string check and throwing a BadRequestException for unexpected types. This ensures future callers within the service cannot accidentally pass a non-string.

Concretely:

  • In src/minio/minio.controller.ts, update viewDocument to:
    • Check typeof objectKey !== 'string' and reject with BadRequestException.
    • Keep the existing “empty” check, but now it runs only for valid strings.
  • Add a similar runtime type check to getPresignedUrl.
  • In src/minio/minio.service.ts, update resolveObjectKey to:
    • Throw BadRequestException if typeof key !== 'string' or key is empty-ish.
    • Only then call key.includes('/') and build paths.

No new methods are strictly required beyond using the already imported BadRequestException. There are no new imports needed.

Suggested changeset 2
src/minio/minio.service.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/minio/minio.service.ts b/src/minio/minio.service.ts
--- a/src/minio/minio.service.ts
+++ b/src/minio/minio.service.ts
@@ -175,6 +175,9 @@
    * Se é só um filename legado, reconstrói com userId.
    */
   private resolveObjectKey(key: string, userId?: string): string {
+    if (typeof key !== 'string' || !key) {
+      throw new BadRequestException('Parâmetro "key" inválido');
+    }
     if (key.includes('/')) {
       return key;
     }
EOF
@@ -175,6 +175,9 @@
* Se é um filename legado, reconstrói com userId.
*/
private resolveObjectKey(key: string, userId?: string): string {
if (typeof key !== 'string' || !key) {
throw new BadRequestException('Parâmetro "key" inválido');
}
if (key.includes('/')) {
return key;
}
src/minio/minio.controller.ts
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/minio/minio.controller.ts b/src/minio/minio.controller.ts
--- a/src/minio/minio.controller.ts
+++ b/src/minio/minio.controller.ts
@@ -47,10 +47,15 @@
    */
   @Get('view')
   async viewDocument(
-    @Query('key') objectKey: string,
+    @Query('key') objectKey: string | string[],
     @Req() request: AuthenticatedRequest,
     @Res() res: Response,
   ) {
+    if (typeof objectKey !== 'string') {
+      throw new BadRequestException(
+        'Parâmetro "key" deve ser uma string única',
+      );
+    }
     if (!objectKey) {
       throw new BadRequestException('Parâmetro "key" é obrigatório');
     }
@@ -72,7 +74,12 @@
    * Ex: GET /documents/presigned?key=userId/documentos/1234_abc123.pdf
    */
   @Get('presigned')
-  async getPresignedUrl(@Query('key') objectKey: string) {
+  async getPresignedUrl(@Query('key') objectKey: string | string[]) {
+    if (typeof objectKey !== 'string') {
+      throw new BadRequestException(
+        'Parâmetro "key" deve ser uma string única',
+      );
+    }
     if (!objectKey) {
       throw new BadRequestException('Parâmetro "key" é obrigatório');
     }
EOF
@@ -47,10 +47,15 @@
*/
@Get('view')
async viewDocument(
@Query('key') objectKey: string,
@Query('key') objectKey: string | string[],
@Req() request: AuthenticatedRequest,
@Res() res: Response,
) {
if (typeof objectKey !== 'string') {
throw new BadRequestException(
'Parâmetro "key" deve ser uma string única',
);
}
if (!objectKey) {
throw new BadRequestException('Parâmetro "key" é obrigatório');
}
@@ -72,7 +74,12 @@
* Ex: GET /documents/presigned?key=userId/documentos/1234_abc123.pdf
*/
@Get('presigned')
async getPresignedUrl(@Query('key') objectKey: string) {
async getPresignedUrl(@Query('key') objectKey: string | string[]) {
if (typeof objectKey !== 'string') {
throw new BadRequestException(
'Parâmetro "key" deve ser uma string única',
);
}
if (!objectKey) {
throw new BadRequestException('Parâmetro "key" é obrigatório');
}
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants