Skip to content

Commit

Permalink
Enable macro escaping in %files
Browse files Browse the repository at this point in the history
Avoid macro re-expansion of already pre-processed file paths so that the
literal percent sign can be escaped in them, just like it can in other
parts of the spec file.

The re-expansion happens in rpmGenPath() through rpmGetPath() called
within, so instead of calling the former, just concatenate the path
components and canonicalize the result, which essentially has the same
effect, with the exception that URLs are not handled, but we can
probably live without that as we're dealing with local files here.

We need to do this in all the places (i.e. two) where package file names
are used in path construction.  This shouldn't do any harm since the
base paths in both cases already have been expanded at that point,
anyway.  Note that we're not changing processMetadataFile() here since
that one doesn't support globs in the first place.

We may want to turn this ugly nested call into a neat function named
something like rpmCatPath() later, but let's keep it simple for now.
  • Loading branch information
dmnks committed Apr 22, 2022
1 parent 510c725 commit a23411d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
7 changes: 4 additions & 3 deletions build/files.c
Original file line number Diff line number Diff line change
Expand Up @@ -2199,13 +2199,14 @@ static rpmRC processBinaryFile(Package pkg, FileList fl, const char *fn)

/* Copy file name or glob pattern removing multiple "/" chars. */
/*
* Note: rpmGetPath should guarantee a "canonical" path. That means
* Note: rpmCleanPath should guarantee a "canonical" path. That means
* that the following pathologies should be weeded out:
* //bin//sh
* //usr//bin/
* /.././../usr/../bin//./sh
*/
diskPath = rpmGenPath(fl->buildRoot, NULL, fileName);
diskPath = rpmCleanPath(rstrscat(NULL, fl->buildRoot, "/", fileName, NULL));

/* Arrange trailing slash on directories */
if (fl->cur.isDir)
diskPath = rstrcat(&diskPath, "/");
Expand Down Expand Up @@ -2436,7 +2437,7 @@ static void processSpecialDir(rpmSpec spec, Package pkg, FileList fl,
files = sd->files;
fi = 0;
while (*files != NULL) {
char *origfile = rpmGenPath(basepath, *files, NULL);
char *origfile = rpmCleanPath(rstrscat(NULL, basepath, "/", *files, NULL));
char *eorigfile = NULL;
ARGV_t globFiles;
int globFilesCount, i;
Expand Down
13 changes: 11 additions & 2 deletions tests/data/SPECS/globesctest.spec
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# filenames in the resulting package (with "rpm -qpl") against a static list in
# tests/rpmbuild.at.
%global _prefix /opt
%global mymacro %%%name

Name: globesctest
Version: 1.0
Expand All @@ -16,7 +17,7 @@ BuildArch: noarch


%build
touch 'foo[bar]' bar baz 'foo bar' 'foo b' 'foo a' 'foo r'
touch 'foo[bar]' bar baz 'foo bar' 'foo b' 'foo a' 'foo r' doc%%name

%install
mkdir -p %{buildroot}/opt
Expand All @@ -39,6 +40,10 @@ touch '%{buildroot}/opt/foo b'
touch '%{buildroot}/opt/foo a'
touch '%{buildroot}/opt/foo r'

# Macro escaping
touch '%{buildroot}/opt/foo%%name'
touch '%{buildroot}/opt/foo%mymacro'

# Regression checks
touch '%{buildroot}/opt/foo-bar1'
touch '%{buildroot}/opt/foo-bar2'
Expand All @@ -57,7 +62,7 @@ touch '%{buildroot}/opt/foobawb'

%files

%doc foo\[bar\] ba* "foo bar" foo\ [bar]
%doc foo\[bar\] ba* "foo bar" foo\ [bar] doc%%name

# Glob escaping
/opt/foo\[bar\]
Expand All @@ -75,6 +80,10 @@ touch '%{buildroot}/opt/foobawb'
/opt/foo"\ bar"
/opt/foo\ [bar]

# Macro escaping
/opt/foo%%name
/opt/foo%mymacro

# Regression checks
/opt/foo-bar*
/opt/foo?bar?baz
Expand Down
3 changes: 3 additions & 0 deletions tests/rpmbuild.at
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ runroot rpm -qpl /build/RPMS/noarch/globesctest-1.0-1.noarch.rpm
/opt/foo bar
/opt/foo r
/opt/foo" bar"
/opt/foo%globesctest
/opt/foo%name
/opt/foo'baz
/opt/foo*
/opt/foo-bar1
Expand All @@ -473,6 +475,7 @@ runroot rpm -qpl /build/RPMS/noarch/globesctest-1.0-1.noarch.rpm
/opt/share/doc/globesctest-1.0
/opt/share/doc/globesctest-1.0/bar
/opt/share/doc/globesctest-1.0/baz
/opt/share/doc/globesctest-1.0/doc%name
/opt/share/doc/globesctest-1.0/foo a
/opt/share/doc/globesctest-1.0/foo b
/opt/share/doc/globesctest-1.0/foo bar
Expand Down

0 comments on commit a23411d

Please sign in to comment.