Skip to content

Commit

Permalink
fix: censor sensitive http headers for logging (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovik authored Jul 23, 2024
1 parent ac86891 commit 414cee8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
6 changes: 5 additions & 1 deletion aidial_adapter_dial/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from openai.types.chat.chat_completion_chunk import ChatCompletionChunk

from aidial_adapter_dial.transformer import AttachmentTransformer
from aidial_adapter_dial.utils.dict import censor_ci_dict
from aidial_adapter_dial.utils.env import get_env
from aidial_adapter_dial.utils.exceptions import (
HTTPException,
Expand Down Expand Up @@ -68,7 +69,10 @@ async def parse(

if is_debug:
log.debug(f"request.body: {body}")
log.debug(f"request.headers: {headers}")
secret_headers = ["api-key", "authorization", UPSTREAM_KEY_HEADER]
log.debug(
f"request.headers: {censor_ci_dict(headers, secret_headers)}"
)
log.debug(f"request.params: {query_params}")

local_dial_api_key = headers.get("api-key", None)
Expand Down
8 changes: 8 additions & 0 deletions aidial_adapter_dial/utils/dict.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from typing import List, Mapping


def censor_ci_dict(d: Mapping[str, str], keys: List[str]) -> dict:
key_set = {k.lower() for k in keys}
return {
k: v if k.lower() not in key_set else "**********" for k, v in d.items()
}
4 changes: 3 additions & 1 deletion aidial_adapter_dial/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ async def wrapper(*args, **kwargs):
try:
return await func(*args, **kwargs)
except Exception as e:
log.exception(e)
log.exception(
f"caught exception: {type(e).__module__}.{type(e).__name__}"
)
dial_exception = to_dial_exception(e)
raise to_starlette_exception(dial_exception) from e

Expand Down
25 changes: 13 additions & 12 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ uvicorn = "0.23"
aiohttp = "3.9.5"
openai = "1.32.0" # NOTE: used solely for chat completion response types
pydantic = "^1.10.12"
aidial-sdk = {version = "^0.8.0", extras = ["telemetry"]}
aidial-sdk = {version = "^0.9.0", extras = ["telemetry"]}
respx = "^0.21.1"

[tool.poetry.group.test.dependencies]
Expand Down

0 comments on commit 414cee8

Please sign in to comment.