From 08d6bc247709860a803f85a33d4a195b1f7fbc86 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 3 Mar 2022 15:37:31 +0100 Subject: [PATCH] Prevent exclusion of percent character from percent-encoding When a percent-replacement present in the exclude list would, normally, not have been encoded, it was encoded regardless, as otherwise round-tripping fails. However, when percent or its replacement would normally have been encoded, its presence in the exclude list lead to it not being encoded, which duly breaks round-tripping. It also violates https://datatracker.ietf.org/doc/html/rfc3986#section-2.4 [ChangeLog][Incompatible behavior change][QByteArray] When converting to percent-encoding, it was possible to exclude the percent character (or its replacement, unless this is one of the characters not normally encoded) from conversion. Doing so would violate RFC 3986 Section 2.4 and break round-tripping of any string that contains the percent character. The percent character, or its replacement, is now always encoded, even if mentioned in the exclude list, as the documentation has always claimed. Change-Id: I885ce33e05dc6877f1b3700fb0870fa30556a5aa Reviewed-by: Thiago Macieira --- src/corelib/text/qbytearray.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp index a3254c9a72..95d0ac5516 100644 --- a/src/corelib/text/qbytearray.cpp +++ b/src/corelib/text/qbytearray.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2021 The Qt Company Ltd. +** Copyright (C) 2022 The Qt Company Ltd. ** Copyright (C) 2016 Intel Corporation. ** Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo ** Contact: https://www.qt.io/licensing/ @@ -4653,15 +4653,16 @@ QByteArray QByteArray::toPercentEncoding(const QByteArray &exclude, const QByteA return QByteArray(data(), 0); QByteArray include2 = include; - if (percent != '%') // the default - if ((percent >= 0x61 && percent <= 0x7A) // ALPHA + if (exclude.contains(percent) + || ((percent >= 0x61 && percent <= 0x7A) // ALPHA || (percent >= 0x41 && percent <= 0x5A) // ALPHA || (percent >= 0x30 && percent <= 0x39) // DIGIT || percent == 0x2D // - || percent == 0x2E // . || percent == 0x5F // _ - || percent == 0x7E) // ~ + || percent == 0x7E)) { // ~ include2 += percent; + } QByteArray result = *this; q_toPercentEncoding(&result, exclude.nulTerminated().constData(), include2.nulTerminated().constData(), percent);