Repository navigation
Expand file tree
/
Copy pathManagedSettingsTest.java
More file actions
98 lines (84 loc) · 5.04 KB
/
Copy pathManagedSettingsTest.java
File metadata and controls
98 lines (84 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
package com.github.copilot;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.copilot.rpc.DisableBypassPermissionsModes;
import com.github.copilot.rpc.ManagedSettings;
import com.github.copilot.rpc.ManagedSettingsPermissions;
import com.github.copilot.rpc.PermissionRequestResult;
import com.github.copilot.rpc.ResumeSessionConfig;
import com.github.copilot.rpc.SessionConfig;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.jupiter.api.Test;
class ManagedSettingsTest {
@Test
void forwardsManagedSettingsOnCreateAndResume() throws Exception {
var permissions = new ManagedSettingsPermissions()
.setDisableBypassPermissionsMode(DisableBypassPermissionsModes.DISABLE).setDeny(List.of("Shell(rm *)"))
.setAsk(List.of("Domain(publish.example)")).setAllow(List.of("Read(**)"));
var managedSettings = new ManagedSettings().setPermissions(permissions);
var create = SessionRequestBuilder.buildCreateRequest(
new SessionConfig().setEnableManagedSettings(true).setManagedSettings(managedSettings),
"managed-create");
var resume = SessionRequestBuilder.buildResumeRequest("managed-resume",
new ResumeSessionConfig().setEnableManagedSettings(true).setManagedSettings(managedSettings));
assertEquals(managedSettings, create.getManagedSettings());
assertEquals(managedSettings, resume.getManagedSettings());
var json = new ObjectMapper().writeValueAsString(create);
assertTrue(json.contains("\"enableManagedSettings\":true"));
assertTrue(json.contains("\"managedSettings\":{\"permissions\""));
assertTrue(json.contains("\"disableBypassPermissionsMode\":\"disable\""));
}
@Test
void acceptsFutureBypassPermissionsModes() throws Exception {
var permissions = new ManagedSettingsPermissions().setDisableBypassPermissionsMode("future-fail-closed-mode");
var json = new ObjectMapper().writeValueAsString(permissions);
assertTrue(json.contains("\"disableBypassPermissionsMode\":\"future-fail-closed-mode\""));
}
@Test
void preservesExplicitEmptyPermissionArrays() throws Exception {
// Security-critical: a present empty allow list admits nothing, while an
// absent (null) list imposes no such restriction. Jackson NON_NULL must
// emit an explicit empty array as `[]` and omit null fields, so the two
// remain distinguishable on the wire.
var permissions = new ManagedSettingsPermissions().setDeny(List.of()).setAsk(List.of()).setAllow(List.of());
var managedSettings = new ManagedSettings().setPermissions(permissions);
var create = SessionRequestBuilder.buildCreateRequest(new SessionConfig().setManagedSettings(managedSettings),
"managed-empty");
var json = new ObjectMapper().writeValueAsString(create);
assertTrue(json.contains("\"deny\":[]"), json);
assertTrue(json.contains("\"ask\":[]"), json);
assertTrue(json.contains("\"allow\":[]"), json);
}
@Test
void distinguishesExplicitEmptyAllowFromAbsentAllow() throws Exception {
// Present empty allow admits nothing; the null deny/ask must be omitted.
var permissions = new ManagedSettingsPermissions().setAllow(List.of());
var managedSettings = new ManagedSettings().setPermissions(permissions);
var create = SessionRequestBuilder.buildCreateRequest(new SessionConfig().setManagedSettings(managedSettings),
"managed-mixed");
var json = new ObjectMapper().writeValueAsString(create);
assertTrue(json.contains("\"allow\":[]"), json);
assertFalse(json.contains("\"deny\""), json);
assertFalse(json.contains("\"ask\""), json);
}
@Test
void directInjectionEnablesManagedSafeguards() throws Exception {
var session = new CopilotSession("session-1", null);
var settings = new ManagedSettings().setPermissions(new ManagedSettingsPermissions());
var managedSettingsEnabled = new AtomicBoolean();
var config = new SessionConfig().setManagedSettings(settings).setOnPermissionRequest((request, invocation) -> {
managedSettingsEnabled.set(invocation.isManagedSettingsEnabled());
return CompletableFuture.completedFuture(PermissionRequestResult.noResult());
});
SessionRequestBuilder.configureSession(session, config);
session.handlePermissionRequest(new ObjectMapper().readTree("{\"kind\":\"read\"}")).get();
assertTrue(managedSettingsEnabled.get());
}
}