Skip to content

Commit 3c8567b

Browse files
committed
Bug 1354989 - Avoid pivoting via UTF-16 when loading CSS in the Stylo mode. r=jdm,SimonSapin
MozReview-Commit-ID: Llt29dvB4Io --HG-- extra : rebase_source : 3ae51dc8beff3fb19e9318a6c7c30c9ab08a5b57
1 parent 7d07e19 commit 3c8567b

23 files changed

+804
-456
lines changed

dom/base/nsDocument.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9997,7 +9997,8 @@ NS_IMPL_ISUPPORTS(StubCSSLoaderObserver, nsICSSLoaderObserver)
99979997
} // namespace
99989998

99999999
void
10000-
nsDocument::PreloadStyle(nsIURI* uri, const nsAString& charset,
10000+
nsDocument::PreloadStyle(nsIURI* uri,
10001+
const Encoding* aEncoding,
1000110002
const nsAString& aCrossOriginAttr,
1000210003
const ReferrerPolicy aReferrerPolicy,
1000310004
const nsAString& aIntegrity)
@@ -10006,11 +10007,14 @@ nsDocument::PreloadStyle(nsIURI* uri, const nsAString& charset,
1000610007
nsCOMPtr<nsICSSLoaderObserver> obs = new StubCSSLoaderObserver();
1000710008

1000810009
// Charset names are always ASCII.
10009-
CSSLoader()->LoadSheet(uri, true, NodePrincipal(),
10010-
NS_LossyConvertUTF16toASCII(charset),
10010+
CSSLoader()->LoadSheet(uri,
10011+
true,
10012+
NodePrincipal(),
10013+
aEncoding,
1001110014
obs,
1001210015
Element::StringToCORSMode(aCrossOriginAttr),
10013-
aReferrerPolicy, aIntegrity);
10016+
aReferrerPolicy,
10017+
aIntegrity);
1001410018
}
1001510019

1001610020
nsresult

dom/base/nsDocument.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,8 @@ class nsDocument : public nsIDocument,
836836
virtual void MaybePreconnect(nsIURI* uri,
837837
mozilla::CORSMode aCORSMode) override;
838838

839-
virtual void PreloadStyle(nsIURI* uri, const nsAString& charset,
839+
virtual void PreloadStyle(nsIURI* uri,
840+
const mozilla::Encoding* aEncoding,
840841
const nsAString& aCrossOriginAttr,
841842
ReferrerPolicy aReferrerPolicy,
842843
const nsAString& aIntegrity) override;

dom/base/nsIDocument.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2464,7 +2464,8 @@ class nsIDocument : public nsINode,
24642464
* parser if and when the parser is merged with libgklayout. aCrossOriginAttr
24652465
* should be a void string if the attr is not present.
24662466
*/
2467-
virtual void PreloadStyle(nsIURI* aURI, const nsAString& aCharset,
2467+
virtual void PreloadStyle(nsIURI* aURI,
2468+
const mozilla::Encoding* aEncoding,
24682469
const nsAString& aCrossOriginAttr,
24692470
ReferrerPolicyEnum aReferrerPolicy,
24702471
const nsAString& aIntegrity) = 0;

dom/security/SRICheck.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,37 +182,30 @@ SRICheck::IntegrityMetadata(const nsAString& aMetadataList,
182182

183183
/* static */ nsresult
184184
SRICheck::VerifyIntegrity(const SRIMetadata& aMetadata,
185-
nsIUnicharStreamLoader* aLoader,
186-
const nsAString& aString,
185+
nsIChannel* aChannel,
186+
const nsACString& aBytes,
187187
const nsACString& aSourceFileURI,
188188
nsIConsoleReportCollector* aReporter)
189189
{
190-
NS_ENSURE_ARG_POINTER(aLoader);
191190
NS_ENSURE_ARG_POINTER(aReporter);
192191

193-
nsCOMPtr<nsIChannel> channel;
194-
aLoader->GetChannel(getter_AddRefs(channel));
195-
196192
if (MOZ_LOG_TEST(SRILogHelper::GetSriLog(), mozilla::LogLevel::Debug)) {
197193
nsAutoCString requestURL;
198194
nsCOMPtr<nsIURI> originalURI;
199-
if (channel &&
200-
NS_SUCCEEDED(channel->GetOriginalURI(getter_AddRefs(originalURI))) &&
195+
if (aChannel &&
196+
NS_SUCCEEDED(aChannel->GetOriginalURI(getter_AddRefs(originalURI))) &&
201197
originalURI) {
202198
originalURI->GetAsciiSpec(requestURL);
203199
}
204200
SRILOG(("SRICheck::VerifyIntegrity (unichar stream)"));
205201
}
206202

207203
SRICheckDataVerifier verifier(aMetadata, aSourceFileURI, aReporter);
208-
nsresult rv;
209-
nsDependentCString rawBuffer;
210-
rv = aLoader->GetRawBuffer(rawBuffer);
211-
NS_ENSURE_SUCCESS(rv, rv);
212-
rv = verifier.Update(rawBuffer.Length(), (const uint8_t*)rawBuffer.get());
204+
nsresult rv =
205+
verifier.Update(aBytes.Length(), (const uint8_t*)aBytes.BeginReading());
213206
NS_ENSURE_SUCCESS(rv, rv);
214207

215-
return verifier.Verify(aMetadata, channel, aSourceFileURI, aReporter);
208+
return verifier.Verify(aMetadata, aChannel, aSourceFileURI, aReporter);
216209
}
217210

218211
//////////////////////////////////////////////////////////////

dom/security/SRICheck.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "nsICryptoHash.h"
1212

1313
class nsIChannel;
14-
class nsIUnicharStreamLoader;
1514
class nsIConsoleReportCollector;
1615

1716
namespace mozilla {
@@ -39,8 +38,8 @@ class SRICheck final
3938
* must prevent the resource from loading.
4039
*/
4140
static nsresult VerifyIntegrity(const SRIMetadata& aMetadata,
42-
nsIUnicharStreamLoader* aLoader,
43-
const nsAString& aString,
41+
nsIChannel* aChannel,
42+
const nsACString& aBytes,
4443
const nsACString& aSourceFileURI,
4544
nsIConsoleReportCollector* aReporter);
4645
};

dom/xbl/nsXBLResourceLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ nsXBLResourceLoader::LoadResources(nsIContent* aBoundElement)
151151
}
152152
else
153153
{
154-
rv = cssLoader->LoadSheet(url, false, docPrincipal, EmptyCString(), this);
154+
rv = cssLoader->LoadSheet(url, false, docPrincipal, nullptr, this);
155155
if (NS_SUCCEEDED(rv))
156156
++mPendingSheets;
157157
}

dom/xul/XULDocument.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3738,10 +3738,8 @@ XULDocument::AddPrototypeSheets()
37383738
nsCOMPtr<nsIURI> uri = sheets[i];
37393739

37403740
RefPtr<StyleSheet> incompleteSheet;
3741-
rv = CSSLoader()->LoadSheet(uri,
3742-
mCurrentPrototype->DocumentPrincipal(),
3743-
EmptyCString(), this,
3744-
&incompleteSheet);
3741+
rv = CSSLoader()->LoadSheet(
3742+
uri, mCurrentPrototype->DocumentPrincipal(), this, &incompleteSheet);
37453743

37463744
// XXXldb We need to prevent bogus sheets from being held in the
37473745
// prototype's list, but until then, don't propagate the failure

editor/libeditor/HTMLEditor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,8 +2839,8 @@ HTMLEditor::ReplaceStyleSheet(const nsAString& aURL)
28392839
nsresult rv = NS_NewURI(getter_AddRefs(uaURI), aURL);
28402840
NS_ENSURE_SUCCESS(rv, rv);
28412841

2842-
return ps->GetDocument()->CSSLoader()->
2843-
LoadSheet(uaURI, false, nullptr, EmptyCString(), this);
2842+
return ps->GetDocument()->CSSLoader()->LoadSheet(
2843+
uaURI, false, nullptr, nullptr, this);
28442844
}
28452845

28462846
NS_IMETHODIMP

intl/Encoding.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ mozilla_encoding_decode_to_nscstring_without_bom_handling(
8888
nsACString const* src,
8989
nsACString* dst);
9090

91+
nsresult
92+
mozilla_encoding_decode_from_slice_to_nscstring_without_bom_handling(
93+
mozilla::Encoding const* encoding,
94+
uint8_t const* src,
95+
size_t src_len,
96+
nsACString* dst,
97+
size_t already_validated);
98+
9199
nsresult
92100
mozilla_encoding_decode_to_nscstring_without_bom_handling_and_without_replacement(
93101
mozilla::Encoding const* encoding,
@@ -552,6 +560,41 @@ class Encoding final
552560
this, bytes, out);
553561
}
554562

563+
/**
564+
* Decode complete input to `nsACString` _without BOM handling_ and
565+
* with malformed sequences replaced with the REPLACEMENT CHARACTER when
566+
* the entire input is available as a single buffer (i.e. the end of the
567+
* buffer marks the end of the stream) _asserting that a number of bytes
568+
* from the start are already known to be valid UTF-8_.
569+
*
570+
* The use case for this method is avoiding copying when dealing with
571+
* input that has a UTF-8 BOM. _When in doubt, do not use this method._
572+
*
573+
* When invoked on `UTF_8`, this method implements the (non-streaming
574+
* version of) the _UTF-8 decode without BOM_
575+
* (https://encoding.spec.whatwg.org/#utf-8-decode-without-bom) spec concept.
576+
*
577+
* Returns `NS_ERROR_OUT_OF_MEMORY` upon OOM, `NS_OK_HAD_REPLACEMENTS`
578+
* if there were malformed sequences (that were replaced with the
579+
* REPLACEMENT CHARACTER) and `NS_OK` otherwise.
580+
*
581+
* _Note:_ It is wrong to use this when the input buffer represents only
582+
* a segment of the input instead of the whole input. Use
583+
* `NewDecoderWithoutBOMHandling()` when decoding segmented input.
584+
*
585+
* # Safety
586+
*
587+
* The first `aAlreadyValidated` bytes of `aBytes` _must_ be valid UTF-8.
588+
* `aBytes` _must not_ alias the buffer (if any) of `aOut`.
589+
*/
590+
inline nsresult DecodeWithoutBOMHandling(Span<const uint8_t> aBytes,
591+
nsACString& aOut,
592+
size_t aAlreadyValidated) const
593+
{
594+
return mozilla_encoding_decode_from_slice_to_nscstring_without_bom_handling(
595+
this, aBytes.Elements(), aBytes.Length(), &aOut, aAlreadyValidated);
596+
}
597+
555598
/**
556599
* Decode complete input to `nsAString` _without BOM handling_ and
557600
* _with malformed sequences treated as fatal_ when the entire input is

intl/encoding_glue/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,11 @@ pub fn decode_to_nscstring_without_bom_handling(encoding: &'static Encoding,
421421
decode_from_slice_to_nscstring_without_bom_handling(encoding, src, dst, valid_up_to)
422422
}
423423

424+
#[no_mangle]
425+
pub unsafe extern "C" fn mozilla_encoding_decode_from_slice_to_nscstring_without_bom_handling(encoding: *const Encoding, src: *const u8, src_len: usize, dst: *mut nsACString, already_validated: usize) -> nsresult {
426+
decode_from_slice_to_nscstring_without_bom_handling(&*encoding, slice::from_raw_parts(src, src_len), &mut *dst, already_validated)
427+
}
428+
424429
fn decode_from_slice_to_nscstring_without_bom_handling(encoding: &'static Encoding,
425430
src: &[u8],
426431
dst: &mut nsACString,

0 commit comments

Comments
 (0)