Skip to content

Commit 284f6e7

Browse files
committed
Safeguard against multiple header values.
1 parent be04094 commit 284f6e7

File tree

3 files changed

+42
-37
lines changed

3 files changed

+42
-37
lines changed
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
const _envIdHeader = 'x-ld-envid';
22

3+
final _splitRegex = RegExp(r'\s*,\s*');
4+
35
/// Get the environment ID from headers.
46
String? getEnvironmentId(Map<String, String>? headers) {
5-
return headers?[_envIdHeader];
7+
// Headers will always be in lower case from the http response.
8+
// If multiple headers are associated with a single key, then they will be
9+
// in a comma separated list with potential whitespace.
10+
final headerValue = headers?[_envIdHeader];
11+
if (headerValue == null) {
12+
return null;
13+
}
14+
return headerValue.split(_splitRegex).first;
615
}

packages/common_client/test/data_sources/data_source_event_handler_test.dart

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ void main() {
4646
'"variation":0,"trackEvents":false}'
4747
'}');
4848

49-
final bob = flagManager!.get('HasBob')!.flag!;
50-
expect(bob.version, 11);
51-
expect(bob.detail.value, LDValue.ofBool(false));
52-
expect(bob.detail.variationIndex, 1);
53-
expect(bob.detail.reason, isNull);
54-
5549
final killSwitch = flagManager!.get('killswitch')!.flag!;
5650
expect(killSwitch.version, 10);
5751
expect(killSwitch.detail.value, LDValue.ofBool(true));

packages/common_client/test/data_sources/get_environment_id_test.dart

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,42 @@ import 'package:test/test.dart';
22
import 'package:launchdarkly_common_client/src/data_sources/get_environment_id.dart';
33

44
void main() {
5-
group('getEnvironmentId', () {
6-
test('returns environment ID from headers', () {
7-
final headers = {'x-ld-envid': 'test-env-123'};
8-
final result = getEnvironmentId(headers);
9-
expect(result, 'test-env-123');
10-
});
5+
test('returns environment ID from headers', () {
6+
final headers = {'x-ld-envid': 'test-env-123'};
7+
final result = getEnvironmentId(headers);
8+
expect(result, 'test-env-123');
9+
});
1110

12-
test('returns null when header is missing', () {
13-
final headers = <String, String>{'other-header': 'value'};
14-
final result = getEnvironmentId(headers);
15-
expect(result, null);
16-
});
11+
test('returns null when header is missing', () {
12+
final headers = <String, String>{'other-header': 'value'};
13+
final result = getEnvironmentId(headers);
14+
expect(result, null);
15+
});
1716

18-
test('returns null when headers are null', () {
19-
final result = getEnvironmentId(null);
20-
expect(result, null);
21-
});
17+
test('returns null when headers are null', () {
18+
final result = getEnvironmentId(null);
19+
expect(result, null);
20+
});
2221

23-
test('returns null when headers are empty', () {
24-
final headers = <String, String>{};
25-
final result = getEnvironmentId(headers);
26-
expect(result, null);
27-
});
22+
test('returns null when headers are empty', () {
23+
final headers = <String, String>{};
24+
final result = getEnvironmentId(headers);
25+
expect(result, null);
26+
});
2827

29-
test('handles case-sensitive header name correctly', () {
30-
final headers = {'X-LD-ENVID': 'test-env-123'};
31-
final result = getEnvironmentId(headers);
32-
expect(result, null);
33-
});
28+
test('handles multiple values for environment id', () {
29+
// Services should only send a single environment ID, but if we did get
30+
// multiple we want it to be handled safely.
31+
final headers = <String, String>{'x-ld-envid': 'envid-a, envid-b'};
32+
final result = getEnvironmentId(headers);
33+
expect(result, 'envid-a');
34+
});
3435

35-
test('handles environment ID with special characters', () {
36-
final headers = {'x-ld-envid': 'env-123-abc-456'};
37-
final result = getEnvironmentId(headers);
38-
expect(result, 'env-123-abc-456');
39-
});
36+
test('handles envid is empty string', () {
37+
// Services shouldn't send an empty string, but we want to ensure it
38+
// doesn't cause any runtime issue.
39+
final headers = <String, String>{'x-ld-envid': ''};
40+
final result = getEnvironmentId(headers);
41+
expect(result, '');
4042
});
4143
}

0 commit comments

Comments
 (0)