Skip to content

Commit

Permalink
Fix profile links and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grishka committed Jun 7, 2024
1 parent 7fb1c65 commit ce957b5
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/main/java/smithereen/routes/ProfileRoutes.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public static Object profile(Request req, Response resp){
contactsFields.add(Map.of("name", l.get("profile_city"), "value", user.location));
if(StringUtils.isNotEmpty(user.website)){
String url=TextProcessor.escapeHTML(user.website);
contactsFields.add(Map.of("name", l.get("profile_website"), "value", "<a href=\""+url+"\" target=\"_blank\">"+url+"</a>", "html", "true"));
contactsFields.add(Map.of("name", l.get("profile_website"), "value", "<a href=\""+url+"\" rel=\"me noopener ugc\" target=\"_blank\">"+url+"</a>", "html", "true"));
}
for(User.ContactInfoKey key:User.ContactInfoKey.values()){
if(user.contacts.containsKey(key)){
Expand Down Expand Up @@ -169,7 +169,7 @@ public static Object profile(Request req, Response resp){
url=TextProcessor.escapeHTML(url);
String v="<a href=\""+url+"\"";
if(url.startsWith("http"))
v+=" target=\"_blank\"";
v+=" target=\"_blank\" rel=\"me noopener ugc\"";
v+=">"+TextProcessor.escapeHTML(value)+"</a>";
field.put("value", v);
field.put("html", "true");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/smithereen/routes/SettingsAdminRoutes.java
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ public static Object viewReport(Request req, Response resp, Account self, Applic
};
return new ViolationReportActionViewModel(a, TextProcessor.substituteLinks(mainText, links), switch(a.actionType()){
default -> null;
case COMMENT -> TextProcessor.postprocessPostHTMLForDisplay(a.text(), false);
case COMMENT -> TextProcessor.postprocessPostHTMLForDisplay(a.text(), false, false);
case RESOLVE_WITH_ACTION -> {
User targetUser=users.get(report.targetID);
String statusStr=switch(UserBanStatus.valueOf(a.extra().get("status").getAsString())){
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/smithereen/templates/PostprocessHTMLFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@
public class PostprocessHTMLFilter implements Filter{
@Override
public Object apply(Object input, Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) throws PebbleException{
return new SafeString(TextProcessor.postprocessPostHTMLForDisplay((String)input, (boolean)args.getOrDefault("forceTargetBlank", Boolean.FALSE)));
return new SafeString(TextProcessor.postprocessPostHTMLForDisplay((String)input,
(boolean)args.getOrDefault("forceTargetBlank", Boolean.FALSE),
(boolean)args.getOrDefault("keepLinksRel", Boolean.FALSE)));
}

@Override
public List<String> getArgumentNames(){
return List.of("forceTargetBlank");
return List.of("forceTargetBlank", "keepLinksRel");
}
}
4 changes: 2 additions & 2 deletions src/main/java/smithereen/text/TextProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public void tail(@NotNull Node node, int depth){
return newBody.html();
}

public static String postprocessPostHTMLForDisplay(String text, boolean forceTargetBlank){
public static String postprocessPostHTMLForDisplay(String text, boolean forceTargetBlank, boolean keepLinksRel){
if(text==null)
return "";
Document doc=Jsoup.parseBodyFragment(text);
Expand Down Expand Up @@ -469,7 +469,7 @@ public static String postprocessPostHTMLForDisplay(String text, boolean forceTar
URI uri=new URI(href);
if(forceTargetBlank || (uri.isAbsolute() && (uri.getHost()==null || !Config.isLocal(uri)))){
el.attr("target", "_blank");
if(!el.hasAttr("rel"))
if(!keepLinksRel || !el.hasAttr("rel"))
el.attr("rel", "noopener ugc");
}
}catch(URISyntaxException x){}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/desktop/profile.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
{% endif %}
{% for fld in fields %}
<div class="label">{{ fld.name }}:</div>
<div>{% if fld.html %}{{ fld.value | postprocessHTML }}{% else %}{{ fld.value | nl2br }}{% endif %}</div>
<div>{% if fld.html %}{{ fld.value | postprocessHTML(keepLinksRel=true) }}{% else %}{{ fld.value | nl2br }}{% endif %}</div>
{% else %}
<div class="empty">{{ L('profile_no_info') }}</div>
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/mobile/profile.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{% endif %}
{% for fld in fields %}
<div class="profileFieldName">{{ fld.name }}:</div>
<div class="profileFieldValue">{% if fld.html %}{{ fld.value | postprocessHTML }}{% else %}{{ fld.value | nl2br }}{% endif %}</div>
<div class="profileFieldValue">{% if fld.html %}{{ fld.value | postprocessHTML(keepLinksRel=true) }}{% else %}{{ fld.value | nl2br }}{% endif %}</div>
{% else %}
<div class="emptyState">{{ L('profile_no_info') }}</div>
{% endfor %}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/smithereen/HTMLProcessingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ public void testMastodonTootMicroformatIsKept(){
"<p><a href=\"https://mastodon.host/tags/photo\" class=\"mention hashtag\" rel=\"noopener ugc\" target=\"_blank\">#photo</a> <a href=\"https://mastodon.host/tags/birds\" class=\"mention hashtag\" rel=\"noopener ugc\" target=\"_blank\">#birds</a> <a href=\"https://mastodon.host/tags/sparrow\" class=\"mention hashtag\" rel=\"noopener ugc\" target=\"_blank\">#sparrow</a> <a href=\"https://mastodon.host/tags/%D1%84%D0%BE%D1%82%D0%BE\" class=\"mention hashtag\" rel=\"noopener ugc\" target=\"_blank\">#фото</a> <a href=\"https://mastodon.host/tags/%D0%BF%D1%82%D0%B8%D1%86%D1%8B\" class=\"mention hashtag\" rel=\"noopener ugc\" target=\"_blank\">#птицы</a> <a href=\"https://mastodon.host/tags/%D0%B2%D0%BE%D1%80%D0%BE%D0%B1%D0%B5%D0%B9\" class=\"mention hashtag\" rel=\"noopener ugc\" target=\"_blank\">#воробей</a></p>\n"+
"<p><span class=\"h-card\"><a href=\"https://mastodonsocial.ru/@rf\" class=\"u-url mention\" target=\"_blank\" rel=\"noopener ugc\">@rf</a></span> <span class=\"h-card\"><a href=\"https://mastodon.social/@russian_mastodon\" class=\"u-url mention\" target=\"_blank\" rel=\"noopener ugc\">@russian_mastodon</a></span></p>";

String out=TextProcessor.postprocessPostHTMLForDisplay(TextProcessor.sanitizeHTML(in), false);
String out=TextProcessor.postprocessPostHTMLForDisplay(TextProcessor.sanitizeHTML(in), false, false);
assertEquals(expected.replaceAll("\\s*\n", ""), out.replaceAll("\\s*\n", ""));
}

@Test
public void testTargetBlankIsAddedToExternalLinks(){
String in="<a href=\"https://example.com\">External link</a>";
String expected="<a href=\"https://example.com\" target=\"_blank\" rel=\"noopener ugc\">External link</a>";
String out=TextProcessor.postprocessPostHTMLForDisplay(TextProcessor.sanitizeHTML(in), false);
String out=TextProcessor.postprocessPostHTMLForDisplay(TextProcessor.sanitizeHTML(in), false, false);
assertEquals(expected, out);
}

Expand Down Expand Up @@ -79,7 +79,7 @@ public void testUnsupportedUrlSchemesAreRemoved(){
public void testRelativeUrlsAreExpended(){
String in="<a href=\"/test.html\">Relative link</a>";
String expected="<a href=\"https://example.com/test.html\" target=\"_blank\" rel=\"noopener ugc\">Relative link</a>";
assertEquals(expected, TextProcessor.postprocessPostHTMLForDisplay(TextProcessor.sanitizeHTML(in, URI.create("https://example.com/")), false));
assertEquals(expected, TextProcessor.postprocessPostHTMLForDisplay(TextProcessor.sanitizeHTML(in, URI.create("https://example.com/")), false, false));
}

@Test
Expand Down

0 comments on commit ce957b5

Please sign in to comment.