This repository has been archived by the owner on Sep 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mail: Add 'rel=noreferrer' to external links
Add 'rel=noreferrer' to every external link only if application.noreferrer is true. It requests clients not to add Referer header in the request to the link. We assume a link is a external if the hostname is not empty and does not equal to the system's hostname from Config.getHostname(). Note: It works only for clients which support HTML5.
- Loading branch information
Yi EungJun
committed
Aug 8, 2014
1 parent
53f9101
commit 4d4f0a9
Showing
2 changed files
with
126 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Yobi, Project Hosting SW | ||
* | ||
* Copyright 2014 NAVER Corp. | ||
* http://yobi.io | ||
* | ||
* @Author Yi EungJun | ||
* | ||
* 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 models; | ||
|
||
import java.util.Map; | ||
|
||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.select.Elements; | ||
|
||
import play.test.FakeApplication; | ||
import play.test.Helpers; | ||
|
||
import static org.fest.assertions.Assertions.assertThat; | ||
|
||
public class NotificationMailTest { | ||
protected static FakeApplication app; | ||
private static Map<String, String> additionalConfiguration; | ||
|
||
@BeforeClass | ||
public static void before() { | ||
additionalConfiguration = support.Helpers.makeTestConfig(); | ||
additionalConfiguration.put("application.noreferrer", "true"); | ||
additionalConfiguration.put("application.hostname", "yobi.io"); | ||
app = support.Helpers.makeTestApplication(additionalConfiguration); | ||
Helpers.start(app); | ||
} | ||
|
||
@AfterClass | ||
public static void after() { | ||
Helpers.stop(app); | ||
} | ||
|
||
@Test | ||
public void handleLinks() { | ||
// Given | ||
Document doc = Jsoup.parse( | ||
"<a href=\"http://y/foo/bar\">external link</a>" + | ||
"<a href=\"http://y/foo/bar\" rel=\"nofollow\">external link</a>" + | ||
"<a href=\"http://yobi.io/foo/bar\">internal link</a>" + | ||
"<a href=\"/foo/bar\">relative link</a>" + | ||
"<a href=\"http://yobi.io/%ag\">malformed link</a>"); | ||
|
||
// When | ||
NotificationMail.handleLinks(doc); | ||
|
||
// Then | ||
Elements links = doc.select("a"); | ||
|
||
assertThat(links.get(0).attr("rel")) | ||
.describedAs("rel attribute of external link") | ||
.isEqualTo(" noreferrer"); | ||
|
||
assertThat(links.get(1).attr("rel")) | ||
.describedAs("rel attribute of external link whose rel attribute was 'nofollow'") | ||
.isEqualTo("nofollow noreferrer"); | ||
|
||
assertThat(links.get(2).hasAttr("rel")) | ||
.describedAs("rel attribute of internal link contains 'noreferrer'.") | ||
.isFalse(); | ||
|
||
assertThat(links.get(3).hasAttr("rel")) | ||
.describedAs("rel attribute of relative link contains 'noreferrer'.") | ||
.isFalse(); | ||
|
||
assertThat(links.get(4).attr("rel")) | ||
.describedAs("rel attribute of malformed link") | ||
.isEqualTo(" noreferrer"); | ||
|
||
} | ||
} |