Skip to content

Commit

Permalink
Fix vsiSuffix extraction for GDAL provider
Browse files Browse the repository at this point in the history
1. Extract VSI suffixes only with slashes which prevents mishandling of
domains like foo.ziplock.localdomain. The .zip TLD is still affected and
requires major refactoring.

2. Do not extract suffixes from *.gz files as they aren't archives.

(cherry picked from commit 24712fc)
  • Loading branch information
r.abdullaev authored and nyalldawson committed Oct 17, 2024
1 parent 50404e1 commit 313020a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/core/providers/gdal/qgsgdalproviderbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ QVariantMap QgsGdalProviderBase::decodeGdalUri( const QString &uri )
{
path = path.mid( vsiPrefix.count() );

const thread_local QRegularExpression vsiRegex( QStringLiteral( "(?:\\.zip|\\.tar|\\.gz|\\.tar\\.gz|\\.tgz)([^|]+)" ) );
const thread_local QRegularExpression vsiRegex( QStringLiteral( "(?:\\.zip|\\.tar|\\.tar\\.gz|\\.tgz)([\\\\/][^|]+)" ) );
const QRegularExpressionMatch match = vsiRegex.match( path );
if ( match.hasMatch() )
{
Expand Down
43 changes: 43 additions & 0 deletions tests/src/core/testqgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,49 @@ void TestQgsGdalProvider::decodeUri()
components = QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "gdal" ), uri );
QCOMPARE( components.value( QStringLiteral( "path" ) ).toString(), QString( "/vsicurl/https://www.qgis.org/dataset.tif" ) );
QCOMPARE( components.value( QStringLiteral( "authcfg" ) ).toString(), QString( "1234567" ) );

//test .[extension] in domain-part of http[s]
uri = QStringLiteral( "/vsicurl/https://www.qgis.zip.org/dataset.tif" );
components = QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "gdal" ), uri );
QCOMPARE( components.value( QStringLiteral( "path" ) ).toString(), QString( "https://www.qgis.zip.org/dataset.tif" ) );
QCOMPARE( components.value( QStringLiteral( "vsiSuffix" ) ).toString(), QString( "" ) );
QCOMPARE( components.value( QStringLiteral( "vsiPrefix" ) ).toString(), QString( "/vsicurl/" ) );

//test .zip archive with non-latin symbol in the path
uri = QStringLiteral( "/vsizip/α.zip/img.tif" );
components = QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "gdal" ), uri );
QCOMPARE( components.value( QStringLiteral( "path" ) ).toString(), QString( "α.zip" ) );
QCOMPARE( components.value( QStringLiteral( "vsiPrefix" ) ).toString(), QString( "/vsizip/" ) );
QCOMPARE( components.value( QStringLiteral( "vsiSuffix" ) ).toString(), QString( "/img.tif" ) );

// test .tar archive in local path
uri = QStringLiteral( "/vsitar/path/arc.tar/img.tif" );
components = QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "gdal" ), uri );
QCOMPARE( components.value( QStringLiteral( "path" ) ).toString(), QString( "path/arc.tar" ) );
QCOMPARE( components.value( QStringLiteral( "vsiPrefix" ) ).toString(), QString( "/vsitar/" ) );
QCOMPARE( components.value( QStringLiteral( "vsiSuffix" ) ).toString(), QString( "/img.tif" ) );

// test windows path
uri = QStringLiteral( "/vsizip/C:\\arc.zip/img.tif" );
components = QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "gdal" ), uri );
QCOMPARE( components.value( QStringLiteral( "path" ) ).toString(), QString( "C:\\arc.zip" ) );
QCOMPARE( components.value( QStringLiteral( "vsiPrefix" ) ).toString(), QString( "/vsizip/" ) );
QCOMPARE( components.value( QStringLiteral( "vsiSuffix" ) ).toString(), QString( "/img.tif" ) );

// test backslash after .[extension] in the path
uri = QStringLiteral( "/vsizip/C:\\arc.zip\\img.tif" );
components = QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "gdal" ), uri );
QCOMPARE( components.value( QStringLiteral( "path" ) ).toString(), QString( "C:\\arc.zip" ) );
QCOMPARE( components.value( QStringLiteral( "vsiPrefix" ) ).toString(), QString( "/vsizip/" ) );
QCOMPARE( components.value( QStringLiteral( "vsiSuffix" ) ).toString(), QString( "\\img.tif" ) );

// TODO: [zip] in TLD domain - this test will fail.
// Due to recent updates of web-standards allowing .zip in domain - it has to be protected from the zip-extension check
//uri = QStringLiteral( "/vsizip/vsicurl/https://tld.zip/img.tif" );
//components = QgsProviderRegistry::instance()->decodeUri( QStringLiteral( "gdal" ), uri );
//QCOMPARE( components.value( QStringLiteral( "path" ) ).toString(), QString( "/vsizip/vsicurl/https://tld.zip/img.tif" ) );
//QCOMPARE( components.value( QStringLiteral( "vsiPrefix" ) ).toString(), QString( "" ) );
//QCOMPARE( components.value( QStringLiteral( "vsiSuffix" ) ).toString(), QString( "" ) );
}

void TestQgsGdalProvider::encodeUri()
Expand Down

0 comments on commit 313020a

Please sign in to comment.