Skip to content

Commit

Permalink
BACKLOG-22005 Migrate and improve html filtering api (#4)
Browse files Browse the repository at this point in the history
* BACKLOG-22005 Add graphql API

* BACKLOG-22005 Update api to show current config

* BACKLOG-22005 Update jahia-depends
  • Loading branch information
AKarmanov authored Jan 12, 2024
1 parent 96770bb commit 0a342f9
Showing 15 changed files with 613 additions and 5 deletions.
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -68,6 +68,7 @@
</scm>

<properties>
<jahia-depends>graphql-dxm-provider</jahia-depends>
<jahia-module-signature>MC0CFHH5gM/Zpl8kOBTjCGqH1xA5pqdaAhUAlUgJeofDw/DfOp6piIOe9St+y+U=</jahia-module-signature>
<export-package>
org.jahia.modules.richtext
@@ -90,6 +91,18 @@
</repositories>

<dependencies>
<dependency>
<groupId>org.jahia.modules</groupId>
<artifactId>graphql-dxm-provider</artifactId>
<version>2.20.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.github.graphql-java</groupId>
<artifactId>graphql-java-annotations</artifactId>
<version>${graphql-java-annotations.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.googlecode.owasp-java-html-sanitizer</groupId>
<artifactId>owasp-java-html-sanitizer</artifactId>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jahia.modules.richtext;

import org.json.JSONObject;
import org.owasp.html.PolicyFactory;

public interface RichTextConfigurationInterface {
@@ -16,5 +17,7 @@ public interface RichTextConfigurationInterface {

public PolicyFactory getMergedOwaspPolicyFactory(String... siteKeys);

public JSONObject getMergedJSONPolicy(String... siteKeys);

public boolean configExists(String siteKey);
}
Original file line number Diff line number Diff line change
@@ -106,6 +106,17 @@ public PolicyFactory getDefaultOwaspPolicyFactory() {

@Override
public PolicyFactory getMergedOwaspPolicyFactory(String... siteKeys) {
JSONObject mergedPolicy = getMergedJSONPolicy(siteKeys);

if (!mergedPolicy.isEmpty()) {
return Parser.parseToPolicy(mergedPolicy);
}

return null;
}

@Override
public JSONObject getMergedJSONPolicy(String... siteKeys) {
JSONObject mergedPolicy = new JSONObject();

for (String key : siteKeys) {
@@ -114,11 +125,7 @@ public PolicyFactory getMergedOwaspPolicyFactory(String... siteKeys) {
}
}

if (!mergedPolicy.isEmpty()) {
return Parser.parseToPolicy(mergedPolicy);
}

return null;
return mergedPolicy;
}

@Override
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2002-2020 Jahia Solutions Group SA. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jahia.modules.richtext.graphql;

import org.jahia.modules.graphql.provider.dxm.DXGraphQLExtensionsProvider;
import org.osgi.service.component.annotations.Component;

/**
* Uses DXGraphQLExtensionsProvider class scanning to implement graphql API support
*/
@Component(immediate = true)
public class GraphQLSiteSettingsExtensionsProvider implements DXGraphQLExtensionsProvider {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.jahia.modules.richtext.graphql.models;

import graphql.annotations.annotationTypes.GraphQLDescription;
import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;

@GraphQLDescription("Model for HTML filter settings of a site")
public class GqlHTMLFiltering {

private String siteKey;
private boolean filteringEnabled;

public GqlHTMLFiltering(String siteKey, boolean filteringEnabled) {
this.siteKey = siteKey;
this.filteringEnabled = filteringEnabled;
}

@GraphQLField
@GraphQLName("siteKey")
@GraphQLDescription("Site key")
public String getSiteKey() {
return siteKey;
}


@GraphQLField
@GraphQLName("filteringEnabled")
@GraphQLDescription("Indicates if html filtering is enabled or not")
public Boolean getFilteringEnabled() {
return filteringEnabled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package org.jahia.modules.richtext.graphql.models;

import graphql.annotations.annotationTypes.GraphQLDescription;
import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;

import java.util.HashSet;
import java.util.Set;

@GraphQLDescription("Model for HTML filtering remove attributes")
public class GqlHTMLFilteringRemovedAttributes {

private String element;
private Set<String> attributes = new HashSet<>();

@GraphQLField
@GraphQLName("element")
@GraphQLDescription("Element for which attributes were removed")
public String getElement() {
return element;
}


@GraphQLField
@GraphQLName("attributes")
@GraphQLDescription("Removed attributes")
public Set<String> getAttributes() {
return attributes;
}

public void setElement(String element) {
this.element = element;
}

public void setAttributes(Set<String> attributes) {
this.attributes = attributes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.jahia.modules.richtext.graphql.models;

import graphql.annotations.annotationTypes.GraphQLDescription;
import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@GraphQLDescription("Model for HTML filter test")
public class GqlHTMLFilteringTest {

private String html;
private List<GqlHTMLFilteringRemovedAttributes> removeAttributes = new ArrayList<>();
private Set<String> removeElements = new HashSet<>();


@GraphQLField
@GraphQLName("html")
@GraphQLDescription("Html after filtering")
public String getHtml() {
return html;
}

@GraphQLField
@GraphQLName("removedElements")
@GraphQLDescription("List of removed elements. Any attributes removed with the tag are not reported")
public Set<String> getRemovedElements() {
return removeElements;
}


@GraphQLField
@GraphQLName("removedAttributes")
@GraphQLDescription("Removed attributes")
public List<GqlHTMLFilteringRemovedAttributes> getRemovedAttributes() {
return removeAttributes;
}

public void setHtml(String html) {
this.html = html;
}

public void setRemoveAttributes(List<GqlHTMLFilteringRemovedAttributes> removeAttributes) {
this.removeAttributes = removeAttributes;
}

public void setRemoveElements(Set<String> removeElements) {
this.removeElements = removeElements;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.jahia.modules.richtext.graphql.models;

import graphql.annotations.annotationTypes.GraphQLDescription;
import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@GraphQLDescription("Model for richtext configuration")
public class GqlRichTextConfig {

private Set<String> protocols = new HashSet<>();
private Set<String> elements = new HashSet<>();
private List<GqlRichTextConfigAttribute> attributes = new ArrayList<>();

@GraphQLField
@GraphQLName("protocols")
@GraphQLDescription("Protocols")
public Set<String> getProtocols() {
return protocols;
}

@GraphQLField
@GraphQLName("elements")
@GraphQLDescription("HTML elements")
public Set<String> getElements() {
return elements;
}

@GraphQLField
@GraphQLName("attributes")
@GraphQLDescription("HTML attributes")
public List<GqlRichTextConfigAttribute> getAttributes() {
return attributes;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.jahia.modules.richtext.graphql.models;

import graphql.annotations.annotationTypes.GraphQLDescription;
import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;

import java.util.HashSet;
import java.util.Set;

@GraphQLDescription("Model for richtext configuration attribute")
public class GqlRichTextConfigAttribute {

private String attribute;
private Set<String> elements = new HashSet<>();
private String pattern;

@GraphQLField
@GraphQLName("attribute")
@GraphQLDescription("Html attribute")
public String getAttribute() {
return attribute;
}

@GraphQLField
@GraphQLName("elements")
@GraphQLDescription("Elements for which attribute is applied")
public Set<String> getElements() {
return elements;
}

@GraphQLField
@GraphQLName("pattern")
@GraphQLDescription("Pattern used to validate attribute value")
public String getPattern() {
return pattern;
}

@GraphQLField
@GraphQLName("isGlobal")
@GraphQLDescription("Indicates if attribute is configured globally or for specific elements")
public Boolean isGlobal() {
return elements.isEmpty();
}

public void setAttribute(String attribute) {
this.attribute = attribute;
}

public void setPattern(String pattern) {
this.pattern = pattern;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.jahia.modules.richtext.graphql.mutation;

import graphql.annotations.annotationTypes.GraphQLDescription;
import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;
import graphql.annotations.annotationTypes.GraphQLTypeExtension;
import org.jahia.modules.graphql.provider.dxm.DXGraphQLProvider;
import org.jahia.modules.richtext.graphql.mutation.impl.GqlRichTextConfigurationMutation;

@GraphQLTypeExtension(DXGraphQLProvider.Mutation.class)
public class RichTextConfigurationMutationExtension {

@GraphQLField
@GraphQLName("richtextConfiguration")
@GraphQLDescription("Entry point for richtext configuration mutations")
public static GqlRichTextConfigurationMutation getRichtextConfiguration() {
return new GqlRichTextConfigurationMutation();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jahia.modules.richtext.graphql.mutation.impl;

import graphql.annotations.annotationTypes.GraphQLDescription;
import graphql.annotations.annotationTypes.GraphQLField;
import graphql.annotations.annotationTypes.GraphQLName;
import org.jahia.modules.richtext.graphql.mutation.impl.htmlFiltering.GqlHtmlFilteringMutation;

@GraphQLName("RichTextConfigurationMutation")
@GraphQLDescription("RichText configuration mutations entry point")
public class GqlRichTextConfigurationMutation {

@GraphQLField
@GraphQLName("htmlFiltering")
@GraphQLDescription("HTML filtering mutation")
public GqlHtmlFilteringMutation getHtmlFiltering() {
return new GqlHtmlFilteringMutation();
}
}
Loading

0 comments on commit 0a342f9

Please sign in to comment.