Skip to content

Commit 9d6cf9a

Browse files
authored
[flutter_svg] Throw on non-2xx responses in SvgNetworkLoader (#13403)
`SvgNetworkLoader.prepareMessage` returned `response.bodyBytes` without checking the status code, so a 404 or 503 HTML error page was handed to the SVG parser and failed with a misleading `StateError: Bad state: Invalid SVG data`. This PR makes it throw an `http.ClientException` naming the status code and URL for any status outside 200–299, so the real failure is visible. It also moves closing an internally created client into a `finally`, so that client is closed on the error path too (and if `get` itself throws). Tests cover a 404 (throws, client still closed) and a 200 (returns the body). Fixes flutter/flutter#195162 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 0f3656e commit 9d6cf9a

4 files changed

Lines changed: 80 additions & 6 deletions

File tree

‎third_party/packages/flutter_svg/CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.3.2
2+
3+
* Fixes `SvgNetworkLoader` parsing the body of a non-2xx HTTP response as SVG.
4+
It now throws an `http.ClientException` naming the status code and URL.
5+
16
## 2.3.1
27

38
* Adds support for SVG alpha masks (`mask-type="alpha"`).

‎third_party/packages/flutter_svg/lib/src/loaders.dart‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,9 @@ class SvgAssetLoader extends SvgLoader<ByteData> {
385385

386386
/// A [BytesLoader] that decodes and parses a UTF-8 encoded SVG string the
387387
/// network in an isolate and creates a vector_graphics binary representation.
388+
///
389+
/// Loading throws an [http.ClientException] if the server responds with a
390+
/// status code outside the 2xx range.
388391
class SvgNetworkLoader extends SvgLoader<Uint8List> {
389392
/// See class doc.
390393
const SvgNetworkLoader(
@@ -405,12 +408,20 @@ class SvgNetworkLoader extends SvgLoader<Uint8List> {
405408

406409
@override
407410
Future<Uint8List?> prepareMessage(BuildContext? context) async {
411+
// Parse first: a malformed URL then throws before a client exists to leak.
412+
final Uri uri = Uri.parse(url);
408413
final http.Client client = _httpClient ?? http.Client();
409-
final http.Response response = await client.get(Uri.parse(url), headers: headers);
410-
if (_httpClient == null) {
411-
client.close();
414+
try {
415+
final http.Response response = await client.get(uri, headers: headers);
416+
if (response.statusCode < 200 || response.statusCode > 299) {
417+
throw http.ClientException('HTTP request failed, statusCode: ${response.statusCode}', uri);
418+
}
419+
return response.bodyBytes;
420+
} finally {
421+
if (_httpClient == null) {
422+
client.close();
423+
}
412424
}
413-
return response.bodyBytes;
414425
}
415426

416427
@override

‎third_party/packages/flutter_svg/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: flutter_svg
22
description: An SVG rendering and widget library for Flutter, which allows painting and displaying Scalable Vector Graphics 1.1 files.
33
repository: https://github.com/flutter/packages/tree/main/third_party/packages/flutter_svg
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_svg%22
5-
version: 2.3.1
5+
version: 2.3.2
66

77
environment:
88
sdk: ^3.11.0

‎third_party/packages/flutter_svg/test/loaders_test.dart‎

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,60 @@ void main() {
111111
await loader.prepareMessage(null);
112112
expect(client.closeCalled, isFalse);
113113
});
114+
115+
test('SvgNetworkLoader throws on a non-2xx response and still closes its client', () async {
116+
final createdClients = <VerifyCloseClient>[];
117+
118+
await http.runWithClient(
119+
() async {
120+
const loader = SvgNetworkLoader('https://example.com/a.svg');
121+
122+
await expectLater(
123+
loader.prepareMessage(null),
124+
throwsA(
125+
isA<http.ClientException>()
126+
.having((http.ClientException e) => e.message, 'message', contains('404'))
127+
.having(
128+
(http.ClientException e) => e.uri,
129+
'uri',
130+
Uri.parse('https://example.com/a.svg'),
131+
),
132+
),
133+
);
134+
expect(createdClients[0].closeCalled, isTrue);
135+
},
136+
() {
137+
final client = VerifyCloseClient(statusCode: 404);
138+
createdClients.add(client);
139+
return client;
140+
},
141+
);
142+
});
143+
144+
test('SvgNetworkLoader returns the body of a 200 response', () async {
145+
final client = VerifyCloseClient(body: '<svg/>');
146+
final loader = SvgNetworkLoader('', httpClient: client as http.Client);
147+
148+
expect(await loader.prepareMessage(null), utf8.encode('<svg/>'));
149+
});
150+
151+
test('SvgNetworkLoader throws on a malformed URL without creating a client', () async {
152+
final createdClients = <VerifyCloseClient>[];
153+
154+
await http.runWithClient(
155+
() async {
156+
const loader = SvgNetworkLoader('http://[invalid');
157+
158+
await expectLater(loader.prepareMessage(null), throwsFormatException);
159+
expect(createdClients, isEmpty);
160+
},
161+
() {
162+
final client = VerifyCloseClient();
163+
createdClients.add(client);
164+
return client;
165+
},
166+
);
167+
});
114168
}
115169

116170
class TestBundle extends Fake implements AssetBundle {
@@ -150,11 +204,15 @@ class _TestColorMapper extends ColorMapper {
150204
}
151205

152206
class VerifyCloseClient extends Fake implements http.Client {
207+
VerifyCloseClient({this.statusCode = 200, this.body = ''});
208+
209+
final int statusCode;
210+
final String body;
153211
bool closeCalled = false;
154212

155213
@override
156214
Future<http.Response> get(Uri url, {Map<String, String>? headers}) async {
157-
return http.Response('', 200);
215+
return http.Response(body, statusCode);
158216
}
159217

160218
@override

0 commit comments

Comments
 (0)