001/* 002 Licensed to the Apache Software Foundation (ASF) under one 003 or more contributor license agreements. See the NOTICE file 004 distributed with this work for additional information 005 regarding copyright ownership. The ASF licenses this file 006 to you under the Apache License, Version 2.0 (the 007 "License"); you may not use this file except in compliance 008 with the License. You may obtain a copy of the License at 009 010 http://www.apache.org/licenses/LICENSE-2.0 011 012 Unless required by applicable law or agreed to in writing, 013 software distributed under the License is distributed on an 014 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 KIND, either express or implied. See the License for the 016 specific language governing permissions and limitations 017 under the License. 018 */ 019package org.apache.wiki.plugin; 020 021import java.util.Locale; 022import org.apache.wiki.api.core.Attachment; 023import org.apache.wiki.api.core.Context; 024import org.apache.wiki.api.core.ContextEnum; 025import org.apache.wiki.api.core.Engine; 026import org.apache.wiki.api.exceptions.PluginException; 027import org.apache.wiki.api.exceptions.ProviderException; 028import org.apache.wiki.api.plugin.Plugin; 029import org.apache.wiki.attachment.AttachmentManager; 030import org.apache.wiki.parser.LinkParsingOperations; 031import org.apache.wiki.parser.MarkupParser; 032import org.apache.wiki.util.TextUtil; 033 034import java.util.Map; 035import java.util.ResourceBundle; 036import java.util.Set; 037 038 039/** 040 * Provides an image plugin for better control than is possible with a simple image inclusion. 041 * <br> Most parameters are equivalents of the html image attributes. 042 * 043 * <p>Parameters : </p> 044 * <ul> 045 * <li><b>src</b> - the source (a URL) of the image (required parameter)</li> 046 * <li><b>align</b> - the alignment of the image</li> 047 * <li><b>height</b> - the height of the image</li> 048 * <li><b>width</b> - the width of the image</li> 049 * <li><b>alt</b> - alternate text</li> 050 * <li><b>caption</b> - the caption for the image</li> 051 * <li><b>link</b> - the hyperlink for the image</li> 052 * <li><b>target</b> - the target (frame) to be used for opening the image</li> 053 * <li><b>style</b> - the style attribute of the image</li> 054 * <li><b>class</b> - the associated class for the image</li> 055 * <li><b>border</b> - the border for the image</li> 056 * <li><b>title</b> - the title for the image, can be presented as a tooltip to the user</li> 057 * </ul> 058 * 059 * @since 2.1.4. 060 */ 061// FIXME: It is not yet possible to do wiki internal links. In order to do this cleanly, a TranslatorReader revamp is needed. 062public class Image implements Plugin { 063 064 /** The parameter name for setting the src. Value is <tt>{@value}</tt>. */ 065 public static final String PARAM_SRC = "src"; 066 /** The parameter name for setting the align parameter. Value is <tt>{@value}</tt>. */ 067 public static final String PARAM_ALIGN = "align"; 068 /** The parameter name for setting the height. Value is <tt>{@value}</tt>. */ 069 public static final String PARAM_HEIGHT = "height"; 070 /** The parameter name for setting the width. Value is <tt>{@value}</tt>. */ 071 public static final String PARAM_WIDTH = "width"; 072 /** The parameter name for setting the alt. Value is <tt>{@value}</tt>. */ 073 public static final String PARAM_ALT = "alt"; 074 /** The parameter name for setting the caption. Value is <tt>{@value}</tt>. */ 075 public static final String PARAM_CAPTION = "caption"; 076 /** The parameter name for setting the link. Value is <tt>{@value}</tt>. */ 077 public static final String PARAM_LINK = "link"; 078 /** The parameter name for setting the target. Value is <tt>{@value}</tt>. */ 079 public static final String PARAM_TARGET = "target"; 080 /** The parameter name for setting the style. Value is <tt>{@value}</tt>. */ 081 public static final String PARAM_STYLE = "style"; 082 /** The parameter name for setting the class. Value is <tt>{@value}</tt>. */ 083 public static final String PARAM_CLASS = "class"; 084 /** The parameter name for setting the border. Value is <tt>{@value}</tt>. */ 085 public static final String PARAM_BORDER = "border"; 086 /** The parameter name for setting the title. Value is <tt>{@value}</tt>. */ 087 public static final String PARAM_TITLE = "title"; 088 089 /** Purely presentational CSS properties the <tt>style</tt> parameter may set. Repositioning and stacking 090 properties (position, z-index, transform, inset, ...) are deliberately absent, so an author cannot lift 091 the rendered box out of the page flow and overlay other content with it (in-wiki phishing overlays). */ 092 private static final Set< String > SAFE_STYLE_PROPERTIES = Set.of( 093 "background", "background-color", "border", "border-color", "border-radius", "border-style", 094 "border-width", "color", "display", "float", "font-family", "font-size", "font-style", "font-weight", 095 "height", "margin", "margin-bottom", "margin-left", "margin-right", "margin-top", "max-height", 096 "max-width", "min-height", "min-width", "opacity", "padding", "padding-bottom", "padding-left", 097 "padding-right", "padding-top", "text-align", "vertical-align", "white-space", "width" ); 098 099 /** 100 * This method is used to clean away things like quotation marks which 101 * a malicious user could use to stop processing and insert javascript. 102 */ 103 private static String getCleanParameter( final Map< String, String > params, final String paramId ) { 104 return TextUtil.replaceEntities( params.get( paramId ) ); 105 } 106 107 @Override 108 public String getDisplayName(Locale locale) { 109 final ResourceBundle rb = ResourceBundle.getBundle(PluginManager.PLUGIN_I18N_RESOURCE, locale); 110 return rb.getString(this.getClass().getSimpleName()); 111 } 112 113 @Override 114 public String getSnipExample() { 115 return "Image src='{image.jpg}'"; 116 } 117 118 /** 119 * Accepts an author-supplied style only if every declaration sets an allow-listed presentational property. 120 * CSS escapes, functions, and negative lengths are rejected outright. 121 * 122 * @param style the author-supplied style parameter, already entity-encoded 123 * @return true if every declaration is allow-listed 124 */ 125 static boolean isSafeStyle( final String style ) { 126 if( style.indexOf( '\\' ) >= 0 || style.indexOf( '(' ) >= 0 ) { 127 return false; 128 } 129 for( final String declaration : style.split( ";" ) ) { 130 if( declaration.trim().isEmpty() ) { 131 continue; 132 } 133 final int colon = declaration.indexOf( ':' ); 134 if( colon < 0 ) { 135 return false; 136 } 137 final String property = declaration.substring( 0, colon ).trim().toLowerCase( Locale.ENGLISH ); 138 final String value = declaration.substring( colon + 1 ); 139 if( !SAFE_STYLE_PROPERTIES.contains( property ) || containsNegativeNumber( value ) ) { 140 return false; 141 } 142 } 143 return true; 144 } 145 146 /** 147 * Procedurally detects negative numbers or offsets (e.g., -5px, - 0.5em) in CSS values 148 * in O(N) time without regular expressions, preventing ReDoS vulnerabilities. 149 */ 150 private static boolean containsNegativeNumber( final String val ) { 151 final int len = val.length(); 152 for( int i = 0; i < len; i++ ) { 153 if( val.charAt( i ) == '-' ) { 154 int j = i + 1; 155 while( j < len && Character.isWhitespace( val.charAt( j ) ) ) { 156 j++; 157 } 158 if( j < len ) { 159 final char c = val.charAt( j ); 160 if( Character.isDigit( c ) ) { 161 return true; 162 } 163 if( c == '.' && j + 1 < len && Character.isDigit( val.charAt( j + 1 ) ) ) { 164 return true; 165 } 166 } 167 } 168 } 169 return false; 170 } 171 172 /** 173 * Restricts the <tt>class</tt> parameter to a whitespace-separated list of CSS identifiers, 174 * refactored to avoid nested quantifiers and prevent ReDoS. 175 * 176 * @param cssclass the author-supplied class parameter 177 * @return true if it is a plain list of identifiers 178 */ 179 static boolean isSafeCssClass( final String cssclass ) { 180 if( cssclass == null || cssclass.isEmpty() ) { 181 return false; 182 } 183 final String[] tokens = cssclass.trim().split( "\\s+" ); 184 for( final String token : tokens ) { 185 if( !token.matches( "[\\w][\\w-]*" ) ) { 186 return false; 187 } 188 } 189 return true; 190 } 191 192 193 private boolean needsSanitization(String link) { 194 String testVal = link.toLowerCase().replaceAll("\\s+", "").trim(); 195 if (testVal.startsWith("data") 196 || testVal.startsWith("javascript") 197 || testVal.startsWith("vbscript")) { 198 return true; 199 } 200 return false; 201 } 202 203 /** 204 * {@inheritDoc} 205 */ 206 @Override 207 public String execute( final Context context, final Map<String, String> params ) throws PluginException { 208 final Engine engine = context.getEngine(); 209 String src = getCleanParameter( params, PARAM_SRC ); 210 String align = getCleanParameter( params, PARAM_ALIGN ); 211 final String ht = getCleanParameter( params, PARAM_HEIGHT ); 212 final String wt = getCleanParameter( params, PARAM_WIDTH ); 213 final String alt = getCleanParameter( params, PARAM_ALT ); 214 final String caption = getCleanParameter( params, PARAM_CAPTION ); 215 String link = getCleanParameter( params, PARAM_LINK ); 216 String target = getCleanParameter( params, PARAM_TARGET ); 217 String style = getCleanParameter( params, PARAM_STYLE ); 218 String cssclass = getCleanParameter( params, PARAM_CLASS ); 219 final String border = getCleanParameter( params, PARAM_BORDER ); 220 final String title = getCleanParameter( params, PARAM_TITLE ); 221 222 if( src == null ) { 223 throw new PluginException("Parameter 'src' is required for Image plugin"); 224 } 225 226 //if( cssclass == null ) cssclass = "imageplugin"; 227 228 if( target != null && !validTargetValue(target) ) { 229 target = null; // not a valid value so ignore 230 } 231 232 if( align != null && !align.equals( "left" ) && !align.equals( "right" ) && !align.equals( "center" ) ) { 233 align = null; // not a valid value so ignore; it is emitted into a CSS float declaration 234 } 235 236 if( style != null && !isSafeStyle( style ) ) { 237 style = null; // only allow-listed presentational CSS may pass, see isSafeStyle() 238 } 239 240 if( cssclass != null && !isSafeCssClass( cssclass ) ) { 241 cssclass = null; // not a plain list of CSS identifiers so ignore 242 } 243 244 try { 245 final AttachmentManager mgr = engine.getManager( AttachmentManager.class ); 246 final Attachment att = mgr.getAttachmentInfo( context, src ); 247 248 if( att != null ) { 249 src = context.getURL( ContextEnum.PAGE_ATTACH.getRequestContext(), att.getName() ); 250 } 251 } catch( final ProviderException e ) { 252 throw new PluginException( "Attachment info failed: " + e.getMessage() ); 253 } 254 255 final StringBuilder result = new StringBuilder(); 256 257 result.append( "<table border=\"0\" class=\"imageplugin\"" ); 258 259 if( title != null ) { 260 result.append( " title=\"" ).append( title ).append( "\"" ); 261 } 262 263 if( align != null ) { 264 if( align.equals( "center" ) ) { 265 result.append( " style=\"margin-left: auto; margin-right: auto; text-align:center; vertical-align:middle;\"" ); 266 } else { 267 result.append( " style=\"float:" ).append( align ).append( ";\"" ); 268 } 269 } 270 271 result.append( ">\n" ); 272 273 if( caption != null ) { 274 result.append( "<caption>" ).append( caption ).append( "</caption>\n" ); 275 } 276 277 // move css class and style to the container of the image, so it doesn't affect the caption 278 result.append( "<tr><td" ); 279 280 if( cssclass != null ) { 281 result.append( " class=\"" ).append( cssclass ).append( "\"" ); 282 } 283 284 if( style != null ) { 285 result.append( " style=\"" ).append( style ); 286 287 // Make sure that we add a ";" to the end of the style string 288 if( result.charAt( result.length()-1 ) != ';' ) { 289 result.append( ";" ); 290 } 291 292 result.append("\""); 293 } 294 295 result.append( ">" ); 296 297 if( link != null ) { 298 if( !context.getBooleanWikiProperty( MarkupParser.PROP_ALLOWHTML, false ) ) { 299 if (needsSanitization(link)) { 300 link = "http://invalid_url" + link; 301 } 302 } 303 result.append( "<a href=\"" ).append( link ).append( "\"" ); 304 if( target != null ) { 305 result.append( " target=\"" ).append( target ).append( "\"" ); 306 } 307 result.append(">"); 308 } 309 310 if(!context.getBooleanWikiProperty(MarkupParser.PROP_ALLOWHTML, false)) { 311 if (needsSanitization(src) && !LinkParsingOperations.isDataImage(src)) { 312 src = "http://invalid_url" + src; 313 } 314 } 315 result.append( "<img src=\"" ).append( src ).append( "\"" ); 316 317 if( ht != null ) { 318 result.append( " height=\"" ).append( ht ).append( "\"" ); 319 } 320 if( wt != null ) { 321 result.append( " width=\"" ).append( wt ).append( "\"" ); 322 } 323 if( alt != null ) { 324 result.append( " alt=\"" ).append( alt ).append( "\"" ); 325 } 326 if( border != null ) { 327 result.append( " border=\"" ).append( border ).append( "\"" ); 328 } 329 // if( map != null ) result.append(" map=\""+map+"\""); 330 331 result.append(" />"); 332 if( link != null ) { 333 result.append("</a>"); 334 } 335 result.append("</td></tr>\n"); 336 result.append("</table>\n"); 337 338 return result.toString(); 339 } 340 341 private boolean validTargetValue( final String s ) { 342 if( s.equals("_blank") 343 || s.equals("_self") 344 || s.equals("_parent") 345 || s.equals("_top") ) { 346 return true; 347 } else if( !s.isEmpty() ) { // check [a-zA-z] 348 final char c = s.charAt(0); 349 return Character.isLowerCase(c) || Character.isUpperCase(c); 350 } 351 return false; 352 } 353 354}