Which fields can the access log record, and how to write the content received from the client in a secure way?
Which fields can the server's access log record depends on how the format instruction is拼,not a fixed table structure. nginx's log module useslog_formatDefine naming format, useaccess_logQuotation format, fields are composed of variables. To judge security, first divide the fields into two categories according to「Who provides this value」The source address, request line, status code, and processing time are generated by the server side, and the source path and client identifier are completely controlled by the requesting party.The latter can contain any characters or be entirely forged.
The log format is拼凑出来的指令,not fixed.
log_formatThe value is a template with variables, named after it can be referenced by multipleaccess_logreferences. The same log can be distinguished by format for different purposes: a set for troubleshooting performance, a set for counting sources, a set for auditing, and fields do not have to overlap.
There are two details that will affect the subsequent parsing. First, when a variable cannot be retrieved, it is recorded as a hyphen, so the parsing rules must accept the empty value form; second, the escaping method,log_formatThe escape parameter can be default, , or none, the default is default-default will escape double quotes, backslashes, and characters with values less than 32 or greater than 126 will escape according to the rules of JSON strings, none will close the escape
Which fields come from the request itself and which come from the server
| field | Source | Client可控 | Suggestion |
|---|---|---|---|
| Source address | Connection layer | No (can be rewritten by proxy) | Reserved, as the minimum available set |
| Request line and method | a request | Is | 保留,用于定位异常路径 |
| Status code | Server Response | No | retains |
| Processing Time | Server | No | Reserved for performance troubleshooting |
| Source and client identifier | Request Headers | Is | Reserved but depends on escaping |
| Complete request header | a request | Is | Caution, large volume and may contain identity credentials |
The source address, request, status code, source, and client identifier are the minimum set of data used for troubleshooting. Whether to write the entire request header into the log should be judged separately: it may contain identity information. Once this value is in the log, the file itself becomes sensitive data.
Why escape the client可控 field
The route and client identifier are emitted by the visitor's own program, and the content is not constrained. Inserting newline characters, quotes, or control characters can cause two types of consequences: field misalignment during parsing, a single request being split into multiple records, or vice versa; or forging a line that appears normal, making it impossible to tell who wrote it after the fact.
The deeper risk is at the log consumption end. When logs are read by the background interface, search panel, or report, unescaped content may be executed on the display side, which is a common entry point for stored cross-site scripting. AnQi CMS has protected against XSS and SQL injection within the site, but logs from writing to analysis often cross multiple tools, and the other end still depends on escaping and format constraints, and cannot assume that the downstream will handle it.
Escape as default when writing, and use escaping when connecting to a JSON analysis pipeline; none is only suitable for completely trusted scenarios that will not be parsed again.
Log is configured for the entire site or by path
access_logIt can appear at different levels of server configuration, covering the three levels of entire site, site, and path; where a request is eventually logged at a certain level depends on the context at the end of processing ——Requests that have undergone internal redirection may have different recording locations from the originally matched path.
When running multiple sites on a single host, this directly affects whether fault troubleshooting can clarify responsibility: specifying log files separately by site is easier to locate than using a single large file for the entire site. For those deployed with Docker images, pay attention to whether the logs are output to a volume outside the container; otherwise, they will be lost after a restart; for those deployed with Baota panel or aaPanel, the log path and splitting strategy are usually in two places, the panel and server configuration, and it is necessary to confirm which one is actually effective after modification.
The fields to keep and the handling to block
In addition to retaining the minimum available set, there are two things to be implemented. First is archiving and retention: logs should be cut according to the cycle and included in the backup range, only keeping the daily files will cause a gap in post-event traceability; whether the log file of the next day is included in the backup record should be written. Second is desensitization on the collection side: excluding headers with credentials from the format is much more reliable than post-cleaning.
Common questions
Question: Should the size of the response body be recorded in the log?Answer: Yes. It is a commonly used basis for judging whether compression and caching are effective, and does not involve client controllable content.
Question: Isn't it safer to turn on JSON escaping?Answer: Escaping solves field misalignment and control characters, changing the format to JSON does not change the fact that 'Content is provided by the requestor', still need to limit which request headers to record.
Question: If multiple websites share one host, should the logs be separated into different files?Answer: It is recommended to separate by site. When sharing files, troubleshooting should first be done by domain name, which is costly and easy to miss.