{"record":{"id":"36711169a34e8713","repo":"dgraph-io/dgraph","slug":"error-finding-sha-length-for-algo-v","errorCode":null,"errorMessage":"error finding sha length for algo %v","messagePattern":"error finding sha length for algo (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"x/acl_enc_keys.go","lineNumber":137,"sourceCode":"\t\tpk, err := jwt.ParseEdPrivateKeyFromPEM(key)\n\t\tif err != nil {\n\t\t\treturn nil, nil, errors.Wrapf(err, \"error parsing ACL key as EdDSA private key\")\n\t\t}\n\t\treturn pk.(crypto.Signer), pk.(ed25519.PrivateKey).Public(), nil\n\n\tdefault:\n\t\treturn nil, nil, errors.Errorf(\"unsupported signing algorithm: %v\", alg.Alg())\n\t}\n}\n\nfunc checkAclKeyLength(alg jwt.SigningMethod, key Sensitive) error {\n\tif !strings.HasPrefix(alg.Alg(), \"HS\") {\n\t\treturn nil\n\t}\n\n\tsl, err := strconv.Atoi(strings.TrimPrefix(alg.Alg(), \"HS\"))\n\tif err != nil {\n\t\treturn errors.Wrapf(err, \"error finding sha length for algo %v\", alg.Alg())\n\t}\n\n\t// SHA length has to be smaller or equal to the key length\n\tif sl > len(key)*8 {\n\t\treturn errors.Errorf(\"ACL key length [%v <= %v] bits for JWT algorithm [%v]\", len(key)*8, sl, alg.Alg())\n\t}\n\treturn nil\n}\n\nfunc RegisterAclAndEncFlags(flag *pflag.FlagSet) {\n\tregisterAclFlag(flag)\n\tregisterEncFlag(flag)\n\tregisterVaultFlag(flag, true, true)\n}\n\nfunc RegisterEncFlag(flag *pflag.FlagSet) {\n\tregisterEncFlag(flag)\n\tregisterVaultFlag(flag, false, true)","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/dgraph-io/dgraph/blob/759e242be62c91f8d084da06ad0c8d21256d9c07/x/acl_enc_keys.go#L119-L155","documentation":"checkAclKeyLength validates HMAC (HS*) keys by extracting the SHA size from the algorithm name (e.g. HS256 -> 256) via strconv.Atoi. If the suffix is not a valid integer, the conversion fails and this wrapped error is returned.","triggerScenarios":"checkAclKeyLength receiving a signing method whose Alg() starts with 'HS' but whose remainder does not parse as a number — e.g. a custom or corrupted signing method name like 'HSABC' or 'HS'.","commonSituations":"Very rare with stock golang-jwt methods; seen when a custom HS-prefixed signing method is registered or when algorithm strings are tampered with.","solutions":["Use a standard HMAC algorithm: HS256, HS384, or HS512","Remove custom jwt.SigningMethod registrations that use an HS prefix with a non-numeric suffix","Verify the jwt-alg flag value has no typos or trailing characters"],"exampleFix":"// before\njwt-alg=HSXYZ\n// after\njwt-alg=HS256","handlingStrategy":"validation","validationCode":"if strings.HasPrefix(algStr, \"HS\") {\n    n, err := strconv.Atoi(strings.TrimPrefix(algStr, \"HS\"))\n    if err != nil || (n != 256 && n != 384 && n != 512) {\n        return fmt.Errorf(\"alg %q invalid; use HS256/HS384/HS512\", algStr)\n    }\n}","typeGuard":null,"tryCatchPattern":"if _, err := x.GetEncAclKeys(flag, encKey); err != nil {\n    if strings.Contains(err.Error(), \"error finding sha length\") {\n        log.Fatalf(\"HS algorithm suffix must be numeric: HS256/384/512\")\n    }\n    return err\n}","preventionTips":["Only use canonical HS256/HS384/HS512 strings","Never let user input flow into jwt-alg unvalidated","Avoid custom HS-prefixed signing methods"],"tags":["jwt","acl","validation","hmac"],"backgroundTag":"unsupported-jwt-algorithm","analyzedSha":"759e242be62c91f8d084da06ad0c8d21256d9c07","analyzedAt":"2026-09-01T14:42:12.034Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}