# Security

## Security

이 장에서는 응용 프로그램의 보안을 강화할 수 있는 몇 가지 기술을 배웁니다.

## Helmet

[Helmet](https://github.com/helmetjs/helmet)은 HTTP 헤더를 적절하게 설정하여 잘 알려진 일부 웹 취약점으로부터 앱을 보호 할 수 있습니다. 일반적으로 Helmet은 보안 관련 HTTP 헤더를 설정하는 12 개의 작은 미들웨어 함수 모음입니다 ([더보기](https://github.com/helmetjs/helmet#how-it-works)). 먼저 필요한 패키지를 설치하십시오.

```bash
$ npm i --save helmet
```

설치가 완료되면 글로벌 미들웨어로 적용하십시오.

```typescript
import * as helmet from 'helmet';
// somewhere in your initialization file
app.use(helmet());
```

## CORS

CORS (Cross-Origin Resource Sharing)는 다른 도메인에서 리소스를 요청할 수 있는 메커니즘입니다. 후드 아래에서 Nest는 [cors](https://github.com/expressjs/cors) 패키지를 사용합니다. 이 패키지는 요구 사항에 따라 사용자 지정할 수 있는 옵션을 제공합니다. CORS를 활성화하려면 `enableCors()`메소드를 호출해야 합니다.

```typescript
const app = await NestFactory.create(ApplicationModule);
app.enableCors();
await app.listen(3000);
```

또한 구성 객체를 이 함수의 매개 변수로 전달할 수 있습니다. 사용 가능한 속성은 공식 [cors](https://github.com/expressjs/cors) 저장소에 철저하게 설명되어 있습니다. 다른 방법은 Nest 옵션 객체를 사용하는 것입니다.

```typescript
const app = await NestFactory.create(ApplicationModule, { cors: true });
await app.listen(3000);
```

조건 값을 전달하는 대신 cors 구성 객체를 사용할 수도 있습니다 ([여기](https://github.com/expressjs/cors#configuration-options) 참조).

## CSRF

사이트 간 요청 위조 (CSRF 또는 XSRF라고 함)는 웹 응용 프로그램이 신뢰하는 사용자로부터 **무단** 명령이 전송되는 웹 사이트의 악의적인 악용 유형입니다. 이러한 종류의 공격을 완화하기 위해 [csurf](https://github.com/expressjs/csurf) 패키지를 사용할 수 있습니다. 먼저 필요한 패키지를 설치하십시오.

```bash
$ npm i --save csurf
```

설치가 완료되면 글로벌 미들웨어로 적용하십시오.

```typescript
import * as csurf from 'csurf';
// somewhere in your initialization file
app.use(csurf());
```

## Rate limiting

무차별 대입 공격으로부터 애플리케이션을 보호하려면 일종의 속도 제한을 구현해야 합니다. 운 좋게도 NPM에는 이미 다양한 미들웨어가 있습니다. 그중 하나가 [express-rate-limit](https://github.com/nfriedly/express-rate-limit)입니다.

```bash
$ npm i --save express-rate-limit
```

설치가 완료되면 글로벌 미들웨어로 적용하십시오.

```typescript
import * as rateLimit from 'express-rate-limit';
// somewhere in your initialization file
app.use(
  rateLimit({
    windowMs: 15 * 60 * 1000, // 15 minutes
    max: 100, // limit each IP to 100 requests per windowMs
  }),
);
```

> info **힌트** `FastifyAdapter`로 작업하는 경우 [fastify-rate-limit](https://github.com/fastify/fastify-rate-limit)를 대신 사용해보십시오.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jakekwak.gitbook.io/nestjs/techniques/security.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
