@@ -850,16 +850,28 @@ pub(crate) fn suspicious_function_call(checker: &mut Checker, call: &ExprCall) {
850
850
// MarkSafe
851
851
[ "django" , "utils" , "safestring" | "html" , "mark_safe" ] => Some ( SuspiciousMarkSafeUsage . into ( ) ) ,
852
852
// URLOpen (`Request`)
853
- [ "urllib" , "request" , "Request" ] |
853
+ [ "urllib" , "request" , "Request" ] |
854
854
[ "six" , "moves" , "urllib" , "request" , "Request" ] => {
855
- // If the `url` argument is a string literal, allow `http` and `https` schemes.
855
+ // If the `url` argument is a string literal or an f string , allow `http` and `https` schemes.
856
856
if call. arguments . args . iter ( ) . all ( |arg| !arg. is_starred_expr ( ) ) && call. arguments . keywords . iter ( ) . all ( |keyword| keyword. arg . is_some ( ) ) {
857
- if let Some ( Expr :: StringLiteral ( ast:: ExprStringLiteral { value, .. } ) ) = & call. arguments . find_argument ( "url" , 0 ) {
857
+ match call. arguments . find_argument ( "url" , 0 ) {
858
+ // If the `url` argument is a string literal, allow `http` and `https` schemes.
859
+ Some ( Expr :: StringLiteral ( ast:: ExprStringLiteral { value, .. } ) ) => {
858
860
let url = value. to_str ( ) . trim_start ( ) ;
859
861
if url. starts_with ( "http://" ) || url. starts_with ( "https://" ) {
860
862
return None ;
861
863
}
862
-
864
+ } ,
865
+ // If the `url` argument is an f-string literal, allow `http` and `https` schemes.
866
+ Some ( Expr :: FString ( ast:: ExprFString { value, .. } ) ) => {
867
+ if let Some ( ast:: FStringElement :: Literal ( ast:: FStringLiteralElement { value, .. } ) ) = value. elements ( ) . next ( ) {
868
+ let url = value. trim_start ( ) ;
869
+ if url. starts_with ( "http://" ) || url. starts_with ( "https://" ) {
870
+ return None ;
871
+ }
872
+ }
873
+ } ,
874
+ _ => { }
863
875
}
864
876
}
865
877
Some ( SuspiciousURLOpenUsage . into ( ) )
@@ -868,27 +880,52 @@ pub(crate) fn suspicious_function_call(checker: &mut Checker, call: &ExprCall) {
868
880
[ "urllib" , "request" , "urlopen" | "urlretrieve" ] |
869
881
[ "six" , "moves" , "urllib" , "request" , "urlopen" | "urlretrieve" ] => {
870
882
if call. arguments . args . iter ( ) . all ( |arg| !arg. is_starred_expr ( ) ) && call. arguments . keywords . iter ( ) . all ( |keyword| keyword. arg . is_some ( ) ) {
871
- if let Some ( arg ) = & call. arguments . find_argument ( "url" , 0 ) {
883
+ match call. arguments . find_argument ( "url" , 0 ) {
872
884
// If the `url` argument is a string literal, allow `http` and `https` schemes.
873
- if let Expr :: StringLiteral ( ast:: ExprStringLiteral { value, .. } ) = arg {
885
+ Some ( Expr :: StringLiteral ( ast:: ExprStringLiteral { value, .. } ) ) => {
874
886
let url = value. to_str ( ) . trim_start ( ) ;
875
887
if url. starts_with ( "http://" ) || url. starts_with ( "https://" ) {
876
888
return None ;
877
889
}
878
- }
890
+ } ,
891
+
892
+ // If the `url` argument is an f-string literal, allow `http` and `https` schemes.
893
+ Some ( Expr :: FString ( ast:: ExprFString { value, .. } ) ) => {
894
+ if let Some ( ast:: FStringElement :: Literal ( ast:: FStringLiteralElement { value, .. } ) ) = value. elements ( ) . next ( ) {
895
+ let url = value. trim_start ( ) ;
896
+ if url. starts_with ( "http://" ) || url. starts_with ( "https://" ) {
897
+ return None ;
898
+ }
899
+ }
900
+ } ,
879
901
880
902
// If the `url` argument is a `urllib.request.Request` object, allow `http` and `https` schemes.
881
- if let Expr :: Call ( ExprCall { func, arguments, .. } ) = arg {
903
+ Some ( Expr :: Call ( ExprCall { func, arguments, .. } ) ) => {
882
904
if checker. semantic ( ) . resolve_qualified_name ( func. as_ref ( ) ) . is_some_and ( |name| name. segments ( ) == [ "urllib" , "request" , "Request" ] ) {
883
- if let Some ( Expr :: StringLiteral ( ast:: ExprStringLiteral { value, .. } ) ) = arguments. find_argument ( "url" , 0 ) {
905
+ match arguments. find_argument ( "url" , 0 ) {
906
+ // If the `url` argument is a string literal, allow `http` and `https` schemes.
907
+ Some ( Expr :: StringLiteral ( ast:: ExprStringLiteral { value, .. } ) ) => {
884
908
let url = value. to_str ( ) . trim_start ( ) ;
885
909
if url. starts_with ( "http://" ) || url. starts_with ( "https://" ) {
886
910
return None ;
887
911
}
888
-
912
+ } ,
913
+
914
+ // If the `url` argument is an f-string literal, allow `http` and `https` schemes.
915
+ Some ( Expr :: FString ( ast:: ExprFString { value, .. } ) ) => {
916
+ if let Some ( ast:: FStringElement :: Literal ( ast:: FStringLiteralElement { value, .. } ) ) = value. elements ( ) . next ( ) {
917
+ let url = value. trim_start ( ) ;
918
+ if url. starts_with ( "http://" ) || url. starts_with ( "https://" ) {
919
+ return None ;
920
+ }
921
+ }
922
+ } ,
923
+ _ => { }
889
924
}
890
925
}
891
- }
926
+ } ,
927
+
928
+ _ => { }
892
929
}
893
930
}
894
931
Some ( SuspiciousURLOpenUsage . into ( ) )
0 commit comments