ABAP Keyword Documentation → ABAP − Reference → Processing External Data → ABAP File Interface → Authorization for File Access
Validation of File Names
Alongside the automatic authorization checks, it may be necessary to validate file names before they are used to prevent directory traversals. This is particularly important if
- the automatic authorization checks are not enough, for example because the database table SPTH or the authorizations for the authorization object S_DATASET have not been defined in full.
- programs with physical file names are used, and these file names are provided using external interfaces such as APIs or UIs.
However if logical file names are consistently used, there is no need for validation.
Other versions: 7.31 | 7.40 | 7.54
Using Logical File Names
File names do not usually need to be validated if a program is consistent in using only logical file names created by the system administrator in the transactions FILE or SF01. Next, the set of logical file names available to an application defines the set of possible physical file names. The associated physical file names are not edited explicitly in the program. Instead, the function module FILE_GET_NAME is used to create the physical file name from the logical file name directly before it is used in a statement of the file interface and used for file access.
Example
The following program works with a logical file name in field log_name
. The
function module FILE_GET_NAME uses this file name to create a platform-specific physical file name in
phys_name
(for use in the statement OPEN DATASET
).
As the value abap_true
is passed to parameter INCLUDING_DIR, the physical file name is absolute; in other words, it contains an absolute path.
DATA: log_name TYPE filename-fileintern,
phys_name TYPE string.
...
CALL FUNCTION 'FILE_GET_NAME'
EXPORTING
logical_filename = log_name
including_dir = abap_true
IMPORTING
file_name = phys_name
EXCEPTIONS
file_not_found = 2
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
DISPLAY LIKE sy-msgty.
RETURN.
ENDIF.
OPEN DATASET phys_name FOR OUTPUT IN TEXT MODE ENCODING UTF-8 .
...
Using Physical File Names
If a program uses physical file names, the name almost always needs to be validated.
Self-Programmed Validation
If valid directories and file names are defined precisely (as is often the case in programs from the technical infrastructure), this type of validation can be skipped. The following can be used, for example:
- Methods from character string processing,
- Methods from class CL_ABAP_DYN_PRG for checking whitelists,
- Methods in the class CL_FS_PATH
However, self-programmed validations (especially when using character string processing) is suitable only for simple cases. For all other cases, validation with logical file names is usually recommended.
Validation with logical file names
In many cases, directories and file names are generic, and are predefined by the system administrator when configuring the system. They can be modified or enhanced while the system is running. In these cases, the concept of logical file names should be employed when handling physical file names explicitly.
In addition to the case above, where a program uses only logical file names, the associations between logical and physical file names can also be useful when handling physical file names for validation purposes. As long as the list of logical file names is complete, the function module FILE_VALIDATE_NAME can be called before a file is accessed. This module checks whether the physical file name is associated with a logical file name or whether the directory is valid. In this way, the function module checks whether the physical file exists in the set defined by the logical file names.
Note
The function module FILE_VALIDATE_NAME always checks absolute file names with specified paths. If a relative file name is passed for checking, the default path is implicitly added as a prefix to DIR_HOME in accordance with the profile parameter.
Example
Validation of a directory. For a directory, the logical file name contained in log_name
must have been created in the format DIR using transaction FILE.
The function module FILE_GET_NAME provides the platform-specific path for this directory in path
.
For a directory, the value abap_true
must be passed to parameter INCLUDING_DIR,
otherwise the function module is terminated with an exception. The method IS_RELATIVE of class
CL_FS_PATH is used to check whether a file name phys_name
entered
by a user is relative or contains an absolute path. An existing absolute file name is applied without
being modified. Relative file names are concatenated with the path. This is done using the method APPEND_PATH_NAME
of a path object from the class CL_FS_PATH created from path
. This object
is platform-independent and works regardless of whether path
contains a closing
separator like \. Finally, phys_name
with FILE_VALIDATE_NAME
is validated by checking the directory of log_name
. This check is also necessary
when creating a chain from the path and relative file name. This is because the specified relative file
name can contains parts such as ..\, which can point to path locations outside of the permitted directory.
DATA: phys_name TYPE string,
log_name TYPE filename-fileintern,
path TYPE string.
...
CALL FUNCTION 'FILE_GET_NAME'
EXPORTING
logical_filename = log_name
including_dir = abap_true
IMPORTING
file_name = path
EXCEPTIONS
file_not_found = 2
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno DISPLAY LIKE sy-msgty.
RETURN.
ENDIF.
cl_demo_input=>request( CHANGING field = phys_name ).
IF cl_fs_path=>create( phys_name )->is_relative( ) = abap_true.
DATA(pref) = cl_fs_path=>create( path ).
pref->append_path_name( phys_name ).
phys_name = pref->get_path_name( ).
ENDIF.
CALL FUNCTION 'FILE_VALIDATE_NAME'
EXPORTING
logical_filename = log_name
CHANGING
physical_filename = phys_name
EXCEPTIONS
logical_filename_not_found = 1
validation_failed = 2
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4
DISPLAY LIKE sy-msgty.
RETURN.
ENDIF.
OPEN DATASET phys_name FOR OUTPUT IN TEXT MODE ENCODING UTF-8 .