@@ -188,6 +188,7 @@ public static String host2ip(String host) {
188188 InetAddress IpAddress = InetAddress .getByName (host );
189189 return IpAddress .getHostAddress ();
190190 } catch (Exception e ) {
191+ logger .error ("host2ip exception " + e .getMessage ());
191192 return "" ;
192193 }
193194 }
@@ -198,45 +199,57 @@ public static String host2ip(String host) {
198199 * @return Octal ip returns true, others return false. 012.23.78.233 return true. 012.0x17.78.233 return false.
199200 */
200201 public static boolean isOctalIP (String host ) {
201- String [] ipParts = host .split ("\\ ." );
202- StringBuilder newDecimalIP = new StringBuilder ();
203- boolean is_octal = false ;
204-
205- // Octal ip only has number and dot character.
206- if (isNumberOrDot (host )) {
207-
208- // not support ipv6
209- if (ipParts .length > 4 ) {
210- throw new SSRFException ("Illegal ipv4: " + host );
211- }
212-
213- // 01205647351
214- if ( ipParts .length == 1 && host .startsWith ("0" ) ) {
215- decimalIp = Integer .valueOf (host , 8 ).toString ();
216- return true ;
217- }
202+ try {
203+ String [] ipParts = host .split ("\\ ." );
204+ StringBuilder newDecimalIP = new StringBuilder ();
205+ boolean is_octal = false ;
206+
207+ // Octal ip only has number and dot character.
208+ if (isNumberOrDot (host )) {
209+
210+ // not support ipv6
211+ if (ipParts .length > 4 ) {
212+ logger .error ("Illegal ipv4: " + host );
213+ return false ;
214+ }
218215
219- // 012.23.78.233
220- for ( String ip : ipParts ) {
221- if (! isNumber ( ip )){
222- throw new SSRFException ( "Illegal ipv4: " + host ) ;
216+ // 01205647351
217+ if ( ipParts . length == 1 && host . startsWith ( "0" ) ) {
218+ decimalIp = Integer . valueOf ( host , 8 ). toString ();
219+ return true ;
223220 }
224- if (ip .startsWith ("0" )) {
225- if (Integer .valueOf (ip , 8 ) >= 256 ){
226- throw new SSRFException ("Illegal ipv4: " + host );
221+
222+ // 012.23.78.233
223+ for (String ip : ipParts ) {
224+ if (!isNumber (ip )){
225+ logger .error ("Illegal ipv4: " + host );
226+ return false ;
227227 }
228- newDecimalIP .append (Integer .valueOf (ip , 8 )).append ("." );
229- is_octal = true ;
230- }else {
231- if (Integer .valueOf (ip , 10 ) >= 256 ) {
232- throw new SSRFException ("Illegal ipv4: " + host );
228+ // start with "0", but not "0"
229+ if (ip .startsWith ("0" ) && !ip .equals ("0" )) {
230+ if (Integer .valueOf (ip , 8 ) >= 256 ){
231+ logger .error ("Illegal ipv4: " + host );
232+ return false ;
233+ }
234+ newDecimalIP .append (Integer .valueOf (ip , 8 )).append ("." );
235+ is_octal = true ;
236+ }else {
237+ if (Integer .valueOf (ip , 10 ) >= 256 ) {
238+ logger .error ("Illegal ipv4: " + host );
239+ return false ;
240+ }
241+ newDecimalIP .append (ip ).append ("." );
233242 }
234- newDecimalIP .append (ip ).append ("." );
235243 }
244+ // delete last char .
245+ decimalIp = newDecimalIP .substring (0 , newDecimalIP .lastIndexOf ("." ));
236246 }
237- decimalIp = newDecimalIP .substring (0 , newDecimalIP .lastIndexOf ("." ));
247+ return is_octal ;
248+ } catch (Exception e ){
249+ logger .error ("SSRFChecker isOctalIP exception: " + e .getMessage ());
250+ return false ;
238251 }
239- return is_octal ;
252+
240253 }
241254
242255 /**
0 commit comments