Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/jaspr/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
against, so `<circle>` came out as an unknown html element rather than an svg one — visible in server-rendered
output and gone as soon as the component hydrated. It is now parsed against the element the nodes are inserted
into, which also covers markup that is only valid inside a specific parent, such as a `<td>` in a `<tr>`.
- Fixed a request failing with a 500 when the client sends an `If-Modified-Since` header that is not a valid date.
`shelf_static` reads it through `Request.ifModifiedSince`, which throws on anything it cannot parse, so the
development server answered `GET /main.css` with an error dump instead of the file — reported with
`Thu,27 Aug 2026 20:33:22 GMT`, a date missing the space after the weekday. RFC 9110 says a recipient must ignore
an `If-Modified-Since` value that is not a valid date, so it is now dropped and the file is served as if it had not
been sent. Both the framework's static handler and the development proxy that serves the generated styles are
covered.

## 0.23.4

Expand Down
21 changes: 20 additions & 1 deletion packages/jaspr/lib/src/server/server_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Handler createHandler(

// We skip static file handling in generate mode to always generate fresh content on the server.
if (!kGenerateMode) {
cascade = cascade.add(gzipMiddleware(staticHandler));
cascade = cascade.add(gzipMiddleware(_dropUnparsableConditional(staticHandler)));
}

cascade = cascade.add((request) async {
Expand Down Expand Up @@ -101,6 +101,25 @@ Handler createHandler(
return cascade.handler;
}

/// Drops an `If-Modified-Since` header that is not a date.
///
/// `shelf_static` reads the header through `Request.ifModifiedSince`, which
/// throws a `FormatException` on anything it cannot parse, and the request
/// then fails with a 500 — over a header the client sent. RFC 9110 says a
/// recipient must ignore an `If-Modified-Since` field value that is not a
/// valid date, so it is dropped and the file is served as if it had not been
/// there.
Handler _dropUnparsableConditional(Handler handler) {
return (Request request) {
try {
request.ifModifiedSince;
} on FormatException {
return handler(request.change(headers: {'if-modified-since': null}));
}
return handler(request);
};
}

Future<String?> Function(String) proxyFileLoader(Request req, Handler proxyHandler) {
return (name) async {
final indexRequest = Request(
Expand Down
80 changes: 80 additions & 0 deletions packages/jaspr/test/server/conditional_headers_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
@TestOn('vm')
library;

import 'package:jaspr/src/server/server_handler.dart';
import 'package:shelf/shelf.dart';
import 'package:test/test.dart';

/// Stands in for `shelf_static`, which reads the header the same way and
/// throws the same `FormatException` on a value it cannot parse.
Handler staticHandlerLike(void Function(DateTime?) onDate) {
return (Request request) {
onDate(request.ifModifiedSince);
return Response.ok('the file');
};
}

Request get(String path, {String? ifModifiedSince}) => Request(
'GET',
Uri.parse('http://localhost$path'),
headers: {'if-modified-since': ?ifModifiedSince},
);

void main() {
group('an if-modified-since the file handler cannot parse', () {
test('is dropped rather than answered with a 500', () async {
DateTime? seen;
var called = false;
final handler = createHandler(
(request, render) => Response.ok('rendered page'),
fileHandler: staticHandlerLike((date) {
called = true;
seen = date;
}),
);

// The value a browser sends back when it is missing the space after the
// weekday, as reported in the wild.
final response = await handler(
get('/main.css', ifModifiedSince: 'Thu,27 Aug 2026 20:33:22 GMT'),
);

expect(called, isTrue, reason: 'the file handler never ran');
expect(seen, isNull, reason: 'the unparsable header was passed through');
expect(response.statusCode, equals(200));
expect(await response.readAsString(), equals('the file'));
});

test('a valid one is left alone', () async {
DateTime? seen;
final handler = createHandler(
(request, render) => Response.ok('rendered page'),
fileHandler: staticHandlerLike((date) => seen = date),
);

final response = await handler(
get('/main.css', ifModifiedSince: 'Thu, 27 Aug 2026 20:33:22 GMT'),
);

expect(seen, equals(DateTime.utc(2026, 8, 27, 20, 33, 22)));
expect(response.statusCode, equals(200));
});

test('no header at all is left alone', () async {
DateTime? seen;
var called = false;
final handler = createHandler(
(request, render) => Response.ok('rendered page'),
fileHandler: staticHandlerLike((date) {
called = true;
seen = date;
}),
);

await handler(get('/main.css'));

expect(called, isTrue);
expect(seen, isNull);
});
});
}
18 changes: 17 additions & 1 deletion packages/jaspr_cli/lib/src/helpers/proxy_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ mixin ProxyHelper on BaseCommand {
final client = devProxy?.client ?? http.Client();
final flutterHandler = flutterPort != null ? proxyHandler('http://localhost:$flutterPort/', client: client) : null;
Directory('.dart_tool/jaspr/generated').createSync(recursive: true);
final generatedHandler = createStaticHandler('.dart_tool/jaspr/generated');
// `createStaticHandler` reads `If-Modified-Since` through
// `Request.ifModifiedSince`, which throws on a value it cannot parse and
// takes the request down with a 500 — over a header the client sent. RFC
// 9110 says such a value must be ignored, so it is dropped here and the
// file is served as if it had not been there.
final generatedHandler = _dropUnparsableConditional(createStaticHandler('.dart_tool/jaspr/generated'));
final allowedFlutterPaths = RegExp(r'^assets|^canvaskit|^packages|.js$|.wasm$');
final webdevHandler = devProxy?.handler ?? (req) => Response.notFound(null);

Expand Down Expand Up @@ -108,3 +113,14 @@ mixin ProxyHelper on BaseCommand {
return server;
}
}

Handler _dropUnparsableConditional(Handler handler) {
return (Request request) {
try {
request.ifModifiedSince;
} on FormatException {
return handler(request.change(headers: {'if-modified-since': null}));
}
return handler(request);
};
}
Loading