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 <thiago.macieira@intel.com>
bb10
Edward Welbourne 2022-03-03 15:37:31 +01:00
parent 84984cfce2
commit 08d6bc2477
1 changed files with 5 additions and 4 deletions

View File

@ -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 <giuseppe.dangelo@kdab.com>
** 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);