Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

136/sanitize returned api data #373

Merged
merged 2 commits into from
Sep 16, 2023
Merged

Conversation

esteban-gs
Copy link
Contributor

@esteban-gs esteban-gs commented Sep 12, 2023

Dev Summary

Changes:

  • add class-transform serialization interceptor. This will use the class-transformer annotations to explicitly expose what we want to expose on data going out.
  • sanitize return user-dto vals. Apply the transform interceptor to the account controller register endpoint.

Test Plan

repro steps:

See the the test that verifies the password isn't returned:

Resources

@esteban-gs esteban-gs self-assigned this Sep 12, 2023
@esteban-gs esteban-gs linked an issue Sep 12, 2023 that may be closed by this pull request
3 tasks
@esteban-gs esteban-gs marked this pull request as ready for review September 12, 2023 02:48
@esteban-gs esteban-gs requested a review from a team September 12, 2023 02:49
Copy link
Contributor

@marvinsjsu marvinsjsu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in a previous comment, I've verified that the password data is not in the response and sanitizing looks to be working. I just have a few questions, but this LGTM.

firstName: string;
last_name: string;
@Expose()
last_name?: string;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's worth to add a migration to update field names to follow a naming convention with camel-case instead of snake-case, or vice-versa? I think my preference is camelCase as it aligns with the front-end, but I'm not sure what is more common with Node/Nest.js?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. This was done, I believe to make table column names conform to the Postgres standard, but there should be a way to tell Typeorm to use camel case in JS.

return UseInterceptors(new SerializeInterceptor(dto));
};

export class SerializeInterceptor implements NestInterceptor {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is an interceptor similar to a middleware? (I should go to the link and read up on class-transformer :D)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In class-transformer documentation, it looks like we may also need reflect-metadata and don't see it as a dependency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's satisfied by the nestjs/common peer dependencies, which uses it as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is an interceptor similar to a middleware?

In NestJs an interceptor is middle-ware. In this specific case, we're chaining a class-transformer operation on the outgoing handler response. It's applied to any handler (controller method) using the UseInterceptors(new SerializeInterceptor(dto)) decorator, but in our case, I just wrapped it in a shorter decorator MapTo.

try {
const hashedPw = await bcrypt.hash(createUserDto.password, parseInt(BCRYPT_WORK_FACTOR));
createUserDto.password = hashedPw;
const user = await this.usersRepository.save(createUserDto);
delete user.password;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've verified locally that password is not included in the returned payload even with this line removed. I'm not 100% clear on how this is happening, so describing here what I think is going on. In UsersService.create method, we've replaced ReturnUserDto as the output type with User, but in the controller, we use annotation @MapTo(ReturnUserDto), which I'm assuming is doing the sanitizing. Since password is not one of the properties of ReturnUserDto, what does the @Expose() annotation do and why do we use it?

Copy link
Contributor Author

@esteban-gs esteban-gs Sep 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, in a way. changing the return type actually has no bearing on the returned data (thanks to Javascript). Thankfully, class-transformer is very good at this. The class-transformer interceptor is getting set to

 return plainToInstance(this.dto, data, {
          excludeExtraneousValues: true,
          strategy: 'excludeAll',
        });

The excludeAll strategy means that any ReturnDto property not decorated with Expose will be removed from the response.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went with excludeAll so that we have to explicitly list all the data we want to expose, but we can change it if it becomes to cumbersome.

@esteban-gs esteban-gs merged commit d1c68cd into main Sep 16, 2023
3 checks passed
@esteban-gs esteban-gs deleted the 136/sanitize-returned-api-data branch September 16, 2023 04:09
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.

discovery: sanitize dtos to entities before repository write
2 participants