summaryrefslogtreecommitdiffstats
path: root/src/main/java/com/gitblit/wicket/SafeTextModel.java
blob: aef7e97a6789ae4698306793e3d04ab8ce567ffa (plain)
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
package com.gitblit.wicket;

import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.util.lang.Objects;
import org.parboiled.common.StringUtils;
import org.slf4j.LoggerFactory;

public class SafeTextModel implements IModel<String> {

	private static final long serialVersionUID = 1L;

	public enum Mode {
		relaxed, none
	}

	private final Mode mode;

	private String value;

	public static SafeTextModel none() {
		return new SafeTextModel(Mode.none);
	}

	public static SafeTextModel none(String value) {
		return new SafeTextModel(Mode.none);
	}

	public static SafeTextModel relaxed() {
		return new SafeTextModel(Mode.relaxed);
	}

	public static SafeTextModel relaxed(String value) {
		return new SafeTextModel(Mode.relaxed);
	}

	public SafeTextModel(Mode mode) {
		this.mode = mode;
	}

	public SafeTextModel(String value, Mode mode) {
		this.value = value;
		this.mode = mode;
	}

	@Override
	public void detach() {
	}

	@Override
	public String getObject() {
		if (StringUtils.isEmpty(value)) {
			return value;
		}
		String safeValue;
		switch (mode) {
		case none:
			safeValue = GitBlitWebApp.get().xssFilter().none(value);
			break;
		default:
			safeValue = GitBlitWebApp.get().xssFilter().relaxed(value);
			break;
		}
		if (!value.equals(safeValue)) {
			LoggerFactory.getLogger(getClass()).warn("XSS filter trigggered on suspicious form field value {}",
					value);
		}
		return safeValue;
	}

	@Override
	public void setObject(String input) {
		this.value = input;
	}

	@Override
	public int hashCode()
	{
		return Objects.hashCode(value);
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
		{
			return true;
		}
		if (!(obj instanceof Model<?>))
		{
			return false;
		}
		Model<?> that = (Model<?>)obj;
		return Objects.equal(value, that.getObject());
	}
}