GraphQL API와의 상호작용보안: 쿼리에서 사용하는 자격 증명 노출 방지
보안: 쿼리에서 사용하는 자격 증명 노출 방지
GraphQL API가 공개되어 있지 않은 경우(예: 정적 사이트를 구축하는 경우)를 제외하고, GraphQL 쿼리가 비공개 데이터를 노출하지 않도록 주의해야 합니다:
- 쿼리의 응답에 포함되는 경우
- 오류 발생 시 출력에 포함되는 경우
- 로그에 포함되는 경우
예를 들어, Environment Fields 모듈이 제공하는 필드 _env를 사용하는 다음 쿼리는:
{
githubAccessToken: _env(name: "GITHUB_ACCESS_TOKEN")
}...응답에 직접 자격 증명을 출력하게 됩니다:
{
"data": {
"githubAccessToken": "{some access token}"
}
}플러그인의 여러 기능을 활용하면 GraphQL 쿼리를 안전하게 만들 수 있습니다:
- Field to Input: 동적 변수를 통해 환경 변수 값을 다른 필드에 주입합니다
- @remove Directive: 환경 변수의 값이 출력에 표시되지 않도록 합니다
- Send HTTP Request Fields: GraphQL 쿼리 내에서 직접 외부 서비스에 연결합니다
예를 들어, 다음 쿼리는 비공개 액세스 토큰을 사용하여 GitHub REST API에 연결합니다:
{
githubAccessToken: _env(name: "GITHUB_ACCESS_TOKEN")
# This directive will remove this entry from the output
@remove
# Create the authorization header to send to GitHub
authorizationHeader: _sprintf(
string: "Bearer %s",
# "Field to Input" feature to access value from the field above
values: [$__githubAccessToken]
)
# Do not print in output
@remove
# Use the field from "Send HTTP Request Fields" to connect to GitHub
gitHubArtifactData: _sendJSONObjectCollectionHTTPRequest(
input: {
url: "https://api.github.com/repos/GatoGraphQL/GatoGraphQL/actions/artifacts",
options: {
headers: [
{
name: "Accept"
value: "application/vnd.github+json"
},
{
name: "Authorization"
# "Field to Input" feature to access value from the field above
value: $__authorizationHeader
},
]
}
}
)
}이 쿼리에서는 민감한 데이터를 포함하는 필드 githubAccessToken과 authorizationHeader가 모두 출력에서 제거되며, 필드 gitHubArtifactData는 API 호출 결과만 표시합니다. 입력값은 노출되지 않습니다(예: 오류 발생 시 변수의 실제 값 대신 문자열 "$__authorizationHeader"가 표시됩니다).