262 lines
10 KiB
Java
262 lines
10 KiB
Java
package run.webup.platform;
|
|
|
|
import static org.hamcrest.Matchers.containsString;
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import java.io.ByteArrayOutputStream;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.time.OffsetDateTime;
|
|
import java.util.*;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipOutputStream;
|
|
import org.junit.jupiter.api.*;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.mock.web.MockMultipartFile;
|
|
import org.springframework.test.context.ActiveProfiles;
|
|
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
|
import org.springframework.test.web.servlet.MockMvc;
|
|
import org.springframework.test.web.servlet.ResultActions;
|
|
|
|
@SpringBootTest
|
|
@ActiveProfiles("test")
|
|
@AutoConfigureMockMvc
|
|
class ExtensionIntegrationTest {
|
|
@Autowired MockMvc mvc;
|
|
@Autowired JdbcTemplate db;
|
|
@Autowired ObjectMapper json;
|
|
@MockitoBean EphemeralStore ephemeral;
|
|
String owner, admin, user;
|
|
|
|
@BeforeEach
|
|
void setup() {
|
|
TestDatabase.clear(db);
|
|
owner = account("SUPER_ADMIN");
|
|
admin = account("ADMIN");
|
|
user = account("USER");
|
|
}
|
|
|
|
@Test
|
|
void uploadRequiresOwnerPrivilegesAndValidatesBoundedPackages() throws Exception {
|
|
String manifest = manifest("plugin", "studio.tools", "1.0.0");
|
|
byte[] valid = zip(Map.of("manifest.json", manifest, "index.html", "<p>Tool</p>"));
|
|
upload(user, valid).andExpect(status().isForbidden());
|
|
upload(admin, valid).andExpect(status().isForbidden());
|
|
mvc.perform(get("/api/extensions/plugins")).andExpect(status().isUnauthorized());
|
|
upload(owner, zip(Map.of("manifest.json", manifest, "../index.html", "<p>Tool</p>")))
|
|
.andExpect(status().isBadRequest());
|
|
upload(owner, zip(Map.of("manifest.json", manifest, "index.html", "a".repeat(262145))))
|
|
.andExpect(status().isBadRequest());
|
|
upload(owner, new byte[524289]).andExpect(status().isPayloadTooLarge());
|
|
upload(
|
|
owner,
|
|
zip(
|
|
Map.of(
|
|
"manifest.json",
|
|
manifest.replace("\"apiVersion\":1", "\"apiVersion\":2"),
|
|
"index.html",
|
|
"<p>Tool</p>")))
|
|
.andExpect(status().isBadRequest());
|
|
upload(
|
|
owner,
|
|
zip(
|
|
Map.of(
|
|
"manifest.json",
|
|
manifest.replace("\"apiVersion\":1", "\"apiVersion\":2,\"apiVersion\":1"),
|
|
"index.html",
|
|
"<p>Tool</p>")))
|
|
.andExpect(status().isBadRequest());
|
|
assertEquals(0, db.queryForObject("SELECT COUNT(*) FROM extension_packages", Integer.class));
|
|
}
|
|
|
|
@Test
|
|
void activationMakesOnlyCurrentPluginVersionFrameableAndCanBeRolledBack() throws Exception {
|
|
byte[] archive = plugin("1.0.0", "<button>V1</button>");
|
|
String first = installed(archive);
|
|
assertEquals(first, installed(archive));
|
|
upload(owner, plugin("1.0.0", "<button>Changed</button>")).andExpect(status().isConflict());
|
|
String next = installed(plugin("1.0.1", "<button>V2</button>"));
|
|
mvc.perform(get("/api/extensions/plugins").header("Authorization", user))
|
|
.andExpect(content().json("[]"));
|
|
frame(first).andExpect(status().isNotFound());
|
|
activation(first, true, admin).andExpect(status().isForbidden());
|
|
activation(first, true, owner).andExpect(status().isOk());
|
|
mvc.perform(get("/api/extensions/plugins").header("Authorization", user))
|
|
.andExpect(jsonPath("$[0].version").value("1.0.0"))
|
|
.andExpect(header().string("X-Frame-Options", "DENY"))
|
|
.andExpect(jsonPath("$[0].payload").doesNotExist());
|
|
mvc.perform(get("/api/public/extensions/plugin-frame/" + first))
|
|
.andExpect(status().isNotFound());
|
|
mvc.perform(
|
|
get("/api/public/extensions/plugin-frame/" + first)
|
|
.header("Sec-Fetch-Dest", "iframe")
|
|
.header("Sec-Fetch-Site", "cross-site"))
|
|
.andExpect(status().isNotFound());
|
|
frame(first)
|
|
.andExpect(status().isOk())
|
|
.andExpect(header().string("X-Frame-Options", "SAMEORIGIN"))
|
|
.andExpect(header().string("Cache-Control", "no-store"))
|
|
.andExpect(
|
|
header()
|
|
.string(
|
|
"Content-Security-Policy",
|
|
containsString("sandbox allow-scripts; default-src 'none'")))
|
|
.andExpect(content().string("<button>V1</button>"));
|
|
activation(next, true, owner).andExpect(status().isOk());
|
|
frame(first).andExpect(status().isNotFound());
|
|
frame(next).andExpect(content().string("<button>V2</button>"));
|
|
mvc.perform(delete("/api/admin/extensions/" + next).header("Authorization", owner))
|
|
.andExpect(status().isConflict());
|
|
activation(first, true, owner).andExpect(status().isOk());
|
|
frame(first).andExpect(status().isOk());
|
|
mvc.perform(delete("/api/admin/extensions/" + next).header("Authorization", owner))
|
|
.andExpect(status().isOk());
|
|
assertEquals(
|
|
6,
|
|
db.queryForObject(
|
|
"SELECT COUNT(*) FROM admin_operation_logs WHERE action LIKE 'EXTENSION_%'",
|
|
Integer.class));
|
|
}
|
|
|
|
@Test
|
|
void themeSupportsSafeColorsAndOneGlobalSelectionAndReset() throws Exception {
|
|
String one = manifest("theme", "studio.one", "1.0.0");
|
|
upload(
|
|
owner,
|
|
zip(
|
|
Map.of(
|
|
"manifest.json",
|
|
one,
|
|
"theme.json",
|
|
"""
|
|
{"light":{"accent":"url(https://example.test/pixel)"}}\
|
|
""")))
|
|
.andExpect(status().isBadRequest());
|
|
upload(
|
|
owner,
|
|
zip(
|
|
Map.of(
|
|
"manifest.json",
|
|
one,
|
|
"theme.json",
|
|
"""
|
|
{"light":{"unknown":"#ffffff"}}\
|
|
""")))
|
|
.andExpect(status().isBadRequest());
|
|
String first =
|
|
installed(
|
|
zip(
|
|
Map.of(
|
|
"manifest.json",
|
|
one,
|
|
"theme.json",
|
|
"""
|
|
{"light":{"accent":"#297763"},"dark":{"text":"#ffffff"}}\
|
|
""")));
|
|
String next =
|
|
installed(
|
|
zip(
|
|
Map.of(
|
|
"manifest.json",
|
|
manifest("theme", "studio.two", "1.0.0"),
|
|
"theme.json",
|
|
"""
|
|
{"light":{"accent":"#ab2817"}}\
|
|
""")));
|
|
mvc.perform(get("/api/public/extensions/theme")).andExpect(content().json("{}"));
|
|
activation(first, true, owner).andExpect(status().isOk());
|
|
mvc.perform(get("/api/public/extensions/theme"))
|
|
.andExpect(jsonPath("$.palettes.light.accent").value("#297763"))
|
|
.andExpect(jsonPath("$.palettes.dark.text").value("#ffffff"));
|
|
activation(next, true, owner).andExpect(status().isOk());
|
|
mvc.perform(get("/api/public/extensions/theme")).andExpect(jsonPath("$.id").value(next));
|
|
assertEquals(
|
|
1,
|
|
db.queryForObject(
|
|
"SELECT COUNT(*) FROM extension_packages WHERE kind='theme' AND enabled=TRUE",
|
|
Integer.class));
|
|
activation(next, false, owner).andExpect(status().isOk());
|
|
mvc.perform(get("/api/public/extensions/theme")).andExpect(content().json("{}"));
|
|
}
|
|
|
|
private String account(String role) {
|
|
UUID id = UUID.randomUUID();
|
|
String token = UUID.randomUUID().toString();
|
|
db.update(
|
|
"INSERT INTO users(id,email,display_name,password_hash,registration_ip) VALUES (?,?,?,?,?)",
|
|
id,
|
|
role + "@example.test",
|
|
role,
|
|
"fixture",
|
|
"test");
|
|
db.update("INSERT INTO user_roles VALUES (?,?)", id, role);
|
|
db.update(
|
|
"INSERT INTO user_sessions(id,user_id,token_hash,ip,user_agent,expires_at) VALUES"
|
|
+ " (?,?,?,?,?,?)",
|
|
UUID.randomUUID(),
|
|
id,
|
|
SecurityConfig.hash(token),
|
|
"test",
|
|
"test",
|
|
OffsetDateTime.now().plusHours(1));
|
|
return "Bearer " + token;
|
|
}
|
|
|
|
private ResultActions upload(String token, byte[] bytes) throws Exception {
|
|
return mvc.perform(
|
|
multipart("/api/admin/extensions")
|
|
.file(new MockMultipartFile("file", "extension.zip", "application/zip", bytes))
|
|
.header("Authorization", token));
|
|
}
|
|
|
|
private String installed(byte[] bytes) throws Exception {
|
|
var response = upload(owner, bytes).andExpect(status().isOk()).andReturn().getResponse();
|
|
return json.readTree(response.getContentAsByteArray()).path("id").asText();
|
|
}
|
|
|
|
private ResultActions activation(String id, boolean enabled, String token) throws Exception {
|
|
return mvc.perform(
|
|
put("/api/admin/extensions/" + id + "/activation")
|
|
.header("Authorization", token)
|
|
.contentType(MediaType.APPLICATION_JSON)
|
|
.content("{\"enabled\":" + enabled + "}"));
|
|
}
|
|
|
|
private ResultActions frame(String id) throws Exception {
|
|
return mvc.perform(
|
|
get("/api/public/extensions/plugin-frame/" + id)
|
|
.header("Sec-Fetch-Dest", "iframe")
|
|
.header("Sec-Fetch-Site", "same-origin"));
|
|
}
|
|
|
|
private static String manifest(String kind, String slug, String version) {
|
|
return """
|
|
{"apiVersion":1,"type":"%s","id":"%s","name":"Sample","version":"%s"}\
|
|
"""
|
|
.formatted(kind, slug, version);
|
|
}
|
|
|
|
private static byte[] plugin(String version, String html) throws Exception {
|
|
return zip(
|
|
Map.of("manifest.json", manifest("plugin", "studio.tools", version), "index.html", html));
|
|
}
|
|
|
|
private static byte[] zip(Map<String, String> files) throws Exception {
|
|
var output = new ByteArrayOutputStream();
|
|
try (var zip = new ZipOutputStream(output)) {
|
|
for (var file : files.entrySet()) {
|
|
zip.putNextEntry(new ZipEntry(file.getKey()));
|
|
zip.write(file.getValue().getBytes(StandardCharsets.UTF_8));
|
|
zip.closeEntry();
|
|
}
|
|
}
|
|
return output.toByteArray();
|
|
}
|
|
}
|