2022-07-11 17:57:33 +00:00
package main
import (
"fmt"
2022-11-02 16:42:25 +00:00
"strconv"
2022-07-12 08:17:38 +00:00
"strings"
2022-07-11 17:57:33 +00:00
"unicode"
2023-11-22 11:16:53 +00:00
"go.uber.org/multierr"
2022-10-29 15:03:57 +00:00
"srs.epita.fr/fic-server/admin/sync"
2022-07-11 17:57:33 +00:00
"srs.epita.fr/fic-server/libfic"
)
2023-11-22 11:16:53 +00:00
func EPITACheckKeyFlag ( flag * fic . FlagKey , raw string , _ * fic . Exercice , exceptions * sync . CheckExceptions ) ( errs error ) {
2022-07-11 17:57:33 +00:00
if ( flag . Label [ 0 ] == 'Q' || flag . Label [ 0 ] == 'q' ) && ( flag . Label [ 1 ] == 'U' || flag . Label [ 1 ] == 'u' ) ||
( flag . Label [ 0 ] == 'W' || flag . Label [ 0 ] == 'w' ) && ( flag . Label [ 1 ] == 'H' || flag . Label [ 1 ] == 'h' ) {
2023-11-22 11:16:53 +00:00
errs = multierr . Append ( errs , fmt . Errorf ( "Label should not begin with %s. This seem to be a question. Reword your label as a description of the expected flag, `:` are automatically appended." , flag . Label [ 0 : 2 ] ) )
2022-07-11 17:57:33 +00:00
flag . Label = flag . Label [ 1 : ]
}
2023-12-16 00:16:07 +00:00
label := [ ] rune ( flag . Label )
if flag . Label [ len ( flag . Label ) - 1 ] != ')' && flag . Label [ len ( flag . Label ) - 1 ] != '©' && ! unicode . IsLetter ( label [ len ( label ) - 1 ] ) && ! unicode . IsDigit ( label [ len ( label ) - 1 ] ) {
2023-11-22 11:16:53 +00:00
errs = multierr . Append ( errs , fmt . Errorf ( "Label should not end with punct (%q). Reword your label as a description of the expected flag, `:` are automatically appended." , flag . Label [ len ( flag . Label ) - 1 ] ) )
2022-07-11 17:57:33 +00:00
}
2022-07-12 08:17:38 +00:00
if strings . HasPrefix ( strings . ToLower ( raw ) , "cve-" ) && flag . Type != "ucq" {
2023-11-22 11:16:53 +00:00
errs = multierr . Append ( errs , fmt . Errorf ( "CVE numbers are required to be UCQ with choice_cost" ) )
2022-07-12 08:17:38 +00:00
}
2022-11-02 16:42:25 +00:00
if _ , err := strconv . ParseInt ( raw , 10 , 64 ) ; flag . Type == "key" && err == nil && ! exceptions . HasException ( ":not-number-flag" ) {
2023-11-22 11:16:53 +00:00
errs = multierr . Append ( errs , fmt . Errorf ( "shouldn't be this flag a number type? (:not-number-flag)" ) )
2022-11-02 16:42:25 +00:00
}
if flag . Placeholder == "" && ( strings . HasPrefix ( flag . Type , "number" ) || flag . Type == "key" || flag . Type == "text" || ( flag . Type == "ucq" && flag . ChoicesCost > 0 ) ) {
2023-11-22 11:16:53 +00:00
errs = multierr . Append ( errs , fmt . Errorf ( "no placeholder defined" ) )
2022-07-12 10:03:54 +00:00
}
2022-08-21 19:31:25 +00:00
if strings . HasPrefix ( flag . Type , "number" ) {
min , max , step , err := fic . AnalyzeNumberFlag ( flag . Type )
if err != nil {
2023-11-22 11:16:53 +00:00
errs = multierr . Append ( errs , err )
2022-08-21 19:31:25 +00:00
} else if min == nil || max == nil || step == nil {
2023-11-22 11:16:53 +00:00
errs = multierr . Append ( errs , fmt . Errorf ( "please define min and max for your number flag" ) )
2023-11-04 08:32:37 +00:00
} else if ( * max - * min ) / * step <= 10 {
2023-11-22 11:16:53 +00:00
errs = multierr . Append ( errs , fmt . Errorf ( "to avoid bruteforce, define more than 10 possibilities" ) )
2022-08-21 19:31:25 +00:00
}
}
2022-07-12 08:17:38 +00:00
return
}
2023-11-22 11:16:53 +00:00
func EPITACheckKeyFlagWithChoices ( flag * fic . FlagKey , raw string , choices [ ] * fic . FlagChoice , _ * fic . Exercice , exceptions * sync . CheckExceptions ) ( errs error ) {
2022-11-02 16:42:25 +00:00
if ! exceptions . HasException ( ":bruteforcable-choices" ) {
if len ( choices ) < 10 && flag . ChoicesCost == 0 {
2023-11-22 11:16:53 +00:00
errs = multierr . Append ( errs , fmt . Errorf ( "requires at least 10 choices to avoid brute-force" ) )
2022-11-02 16:42:25 +00:00
} else if len ( choices ) < 6 && flag . ChoicesCost > 0 {
2023-11-22 11:16:53 +00:00
errs = multierr . Append ( errs , fmt . Errorf ( "requires at least 10 choices to avoid brute-force" ) )
2022-11-02 16:42:25 +00:00
}
2022-07-12 08:17:38 +00:00
}
2022-07-11 17:57:33 +00:00
return
}