Local and remote file inclusion: when a request chooses the file the server runs

File inclusion is a flaw in which the name of a file that the application loads and runs as code comes from the request. Local file inclusion runs a file already on the server; remote file inclusion fetches one from another address first. Both are about code, which is what separates them from path traversal.

Section Web attacks Updated Weakness CWE-98

Quick facts

Also called
LFI and RFI; local and remote file include
Weakness
CWE-98: improper control of filename for include/require statement in a PHP program; the parent class is CWE-829, inclusion of functionality from an untrusted control sphere
OWASP category
A05:2025 Injection lists CWE-98; see injection on this site
Related, but not the same
Path traversal (CWE-22) reads or writes a file; inclusion loads it as code
Main fix
Choose the file in code from a fixed list; never let a request value name it
Does a WAF stop it?
Partly. It can flag file names and addresses in parameters; it cannot repair code that includes whatever it is handed

Definitions follow CWE-98. Details of PHP settings follow the PHP manual.

What local file inclusion and remote file inclusion mean

Many applications assemble a page from pieces: a header, a language file, a template chosen by a parameter. The code asks the runtime to load a file and run what is in it. File inclusion is the flaw where the name of that file comes from the request and nothing limits it.

The two names describe where the file lives. In local file inclusion (LFI) it is already on the server. In remote file inclusion (RFI) it sits at an address the attacker controls, and the server fetches it first. CWE-98 covers both under the PHP include and require statements, and notes that a remote address works only in certain PHP versions and configurations.

The LFI RFI pair gets mixed up with its neighbour, so the line is worth drawing. Reading /etc/passwd through a download link is path traversal: the file is returned as data. Pointing an include at that same file may show it too, but an include also executes whatever PHP it finds. That is the step from information disclosure to code execution.

Local inclusion, remote inclusion and path traversal compared
AttackWhat the application does with the fileNeeds
Path traversalReads or writes it, returns it as dataA file path built from input
Local file inclusionLoads it and runs it as codeAn include call that takes a name from input
Remote file inclusionFetches it from another address, then runs itThe same call, plus a runtime that allows loading code from a URL

How local file inclusion goes wrong

The typical shape is a parameter named page, lang, template or module that ends up inside an include statement. Developers write it for convenience: one script, many views. The flaw is that the set of acceptable values lives only in the developer's head.

What an attacker can do depends on what the server will load. Files with secrets are the first target, because an include of a configuration file may reveal values the page was never meant to print. Anything that holds attacker-chosen text is the second, and more serious: a file the attacker managed to put on disk, perhaps through an upload form, becomes code the moment it is included. That is how a web shell gets started without ever being served directly, and why file upload handling and inclusion are reviewed together.

Traversal sequences usually come along, because the include path starts inside a fixed folder. The encodings and spellings are the same ones catalogued on the path traversal page, so they are not repeated here.

A local file inclusion vulnerability also exists without any climbing out of folders: if the parameter can name any file inside the include folder, including internal-only files, the application is already giving more than it should.

Why remote file inclusion needs more than a bad parameter

RFI is rarer than it was, for a reason on the language side. In PHP, loading a URL through include or require is controlled by allow_url_include. According to the PHP manual, its default is off, it can be set only at the system level (INI_SYSTEM), it depends on allow_url_fopen being on, and it has been deprecated since PHP 7.4.0.

So a remote file inclusion example needs three things at once: an include that takes a name from input, a runtime configured to fetch code from URLs, and a server that may open outbound connections. Remove any one and the remote variant fails. The local variant does not care about the setting. CWE-98 says it directly: turning allow_url_include off does not protect against local file inclusion.

The outbound request an RFI triggers has a cousin that needs no include at all: server-side request forgery, where the server fetches an address from input and returns or acts on the result. Where inclusion ends in running attacker code, the outcome is described on remote code execution and, for shell calls, command injection.

Signs in your logs

Inclusion attempts leave a trail in ordinary access logs, because the control is a request parameter. Nothing below needs a payload; each row is a pattern to search for. Expect false positives: a legitimate integration or a URL-valued redirect parameter can look the same, which is the reason to read matches before refusing them.

What to look for in access logs
SignWhat it suggests
A parameter that normally holds a short word now holds a full addressAn RFI attempt, or a feature that legitimately takes URLs
Parent-folder sequences in a parameter that picks a template or languageAn LFI attempt, often in several encodings
Absolute system paths or well-known configuration file names as a valueProbing for what the include will load
The same parameter tried with many different values from one client in a short runAutomated scanning for an include point
New outbound connections from the web server to unfamiliar hosts, right after such a requestThe server fetching something; check egress logs, not only access logs
Server errors mentioning a failed include, or a page that renders blank or partlyThe include ran with a value the code did not expect

Request logs show attempts, not outcomes. A 200 on a suspicious request does not prove success and a 403 does not prove the code is safe, so compare against what the application did next. The activity log records what the filter matched, which is the right starting list for that review.

Fixing it in code

The strongest fix is to stop treating request values as file names. CWE-98 recommends two related approaches: map fixed input values, such as numeric identifiers, to the actual file names and reject everything else; or accept only inputs found on a list of known good values.

  1. Allow-list the choices. Keep an array of views the page may load, keyed by short names. Look the request value up as a key; if it is not there, serve the default or a 404. Never concatenate the value into a path.
  2. Keep includes out of the request. Where a framework router or template engine can pick the view, let it. Hand-written include calls with variable names are the pattern to remove.
  3. Turn off URL includes. In PHP, leave allow_url_include off, as it is by default, and set allow_url_fopen off too if nothing needs it. Both are system-level settings in php.ini, described in the PHP manual. This closes RFI only.
  4. Do not let uploads become code. Store uploaded files outside the folders that include or execute, with names you generate. A file that can never be included cannot be run through LFI.
  5. Narrow what the process can reach. Run the application as a user that cannot read secrets it does not need, and block outbound connections from the web tier unless the application has a reason to make them.
  6. Validate as a second layer. Strict character checks on the value, as the OWASP Input Validation Cheat Sheet describes, catch mistakes, but they sit behind the allow-list, not in place of it.

Where a WAF helps and where it does not

A filter sees the request, and inclusion starts there. Addresses where a short name belongs, parent-folder sequences, and sensitive file names in a parameter are recognisable shapes, and rules match them across common encodings. In monitor mode such matches build the same list you would read from the logs above, without any visitor being refused.

What a filter does not see is the code. It cannot know which parameters feed an include, so it cannot tell a harmless value from a dangerous one by meaning, only by shape. A name that is a legitimate file on your server looks identical to a legitimate request. Values the request does not carry, such as stored data read later or a path assembled from several fields, never appear as one suspicious string. Outbound fetches made by the server are invisible to a filter placed in front of it. And the traffic that proves the flaw, a file already on disk being included, arrives as an ordinary request.

Treat the filter as an early warning that buys time while the include is rewritten. Start in monitor, read the matches against real traffic, and move to block only for the parameters where matches are confirmed attacks. The page on WAF monitor mode describes the sequence and false positives covers what to do when a legitimate request is matched.

Questions

What is the difference between local file inclusion and remote file inclusion?

Local file inclusion makes the application load and run a file that is already on the server. Remote file inclusion makes it fetch a file from another address and run that. The remote form also needs a runtime that allows loading code from URLs.

Is file inclusion the same as path traversal?

No. Path traversal (CWE-22) uses parent-folder sequences to read or write a file outside the intended folder, and the file comes back as data. File inclusion (CWE-98 for PHP) loads the file as code. The two often appear together because include paths can be traversed.

Does turning off allow_url_include prevent file inclusion?

It prevents the remote form in PHP, where it is already off by default. It does not protect against local file inclusion, as CWE-98 states. The code still has to stop request values from naming the file.

Which CWE numbers apply to LFI and RFI?

CWE-98 covers improper control of the filename for include and require statements in PHP, including the remote variant. Its parent class is CWE-829, inclusion of functionality from an untrusted control sphere. Path traversal is CWE-22.

Can a WAF block LFI and RFI attempts?

It can flag and refuse requests whose parameters carry addresses, traversal sequences or sensitive file names. It cannot fix the include itself, and it does not see values that arrive by other routes. Use it alongside an allow-list in the code, starting in monitor mode.