Skip to content

Node-Registrar review #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
xmonader opened this issue Mar 13, 2025 · 1 comment
Open

Node-Registrar review #36

xmonader opened this issue Mar 13, 2025 · 1 comment
Assignees
Labels
node-registrar type_bug Something isn't working
Milestone

Comments

@xmonader
Copy link
Contributor

xmonader commented Mar 13, 2025

main.go

Image

this leaks the logic of the server into a place where it's whole responsibility is doing

if err:=server.Run(); err!=nil {

  ...
}

It shouldn't bother about the sighandlers or anything else

  • validation for sqllog level?

Image

  • validation for adminTwinID?that could never be zero, while it's set as a default value?
  • add timestamps to the console logger

Image

db.go

  • Image

Condition is wrong, should be ||, it's correctly checked in the cmd/main.go

  • PostgresHost
    Image

check should be if err!=nil

db/models.go

there's no mechanism to set node to Approved (should allow the farmer to approve the node to be part of their farm)

util.go

Does nothing but verify signature, rename to sigverifier.go


const PubKeySize = 32 // ED25519/SR25519 public key size

var (
    ErrInvalidInput    = errors.New("invalid input")
    ErrVerifyFailed    = errors.New("signature verification failed")
)
  • need to extract the pubkey size into a const and also extract the static errors
  • do validation on challenge, signature e.g != 0 before doing anything even
  • you can avoid the dupication e.g
func verifyWithScheme(scheme interface{}, publicKey, challenge, signature []byte) bool {
    key, err := scheme.FromPublicKey(publicKey)
    if err != nil {
        return false
    }
    return key.Verify(challenge, signature)
}

server.go

Image

  • there's no error handling
  • there's a leak of internals into main.go as mentioned above
  • for the Sthudown use a context with a Timeout
  • need to handle http.ErrServerClosed in ListenAndServe

server/auth middleware

  • reduce the window size to 1 minute
  • could be a good idea to include a nonce and track it to prevent more replay attacks (later on that)
  • log the exact error on the server side and return generic error in the response e.g
log.Warn().Err(err).Msg("public key decode failed")
abortWithError(c, http.StatusBadRequest, "invalid account configuration")
  • time skew (can be addressed later when we settle on the challenge validity, as it's right now valid for 5mins or will be for 1 after the modification)
const clockTolerance = 5 * time.Second
if time.Since(time.Unix(timestamp, 0)) > (ChallengeValidity + clockTolerance) {
  • use a unique context key

Image

  • also remove the todo, given that we support ed and sr now no?

routes

  • change updateFarmsHandler -> updateFarmHandler
  • make also the api to be served on /api/v1, right now that's only /v1
  • please don't depend on the order of router registration before and after the middleware
// Farms
publicFarmRoutes := v1.Group("farms")
publicFarmRoutes.GET("/", s.listFarmsHandler)
publicFarmRoutes.GET("/:farm_id", s.getFarmHandler)

protectedFarmRoutes := v1.Group("farms", s.AuthMiddleware())
protectedFarmRoutes.POST("/", s.createFarmHandler)
protectedFarmRoutes.PATCH("/:farm_id", s.updateFarmsHandler)

handlers.go

return after sending error in the response, because that will incorrectly send 200 ok

Image

check errors with Is or As not ==

Image

Image

return after sending the json

name updateFarmsHandler => updateFarmHandler

createFarm, registerNode are missing validations e.g twin ID or farmID

  • remove the commented functions e.g

Image

  • isValidPublicKey only checks on length not format?

Image

  • status codes not optimal e.g this should be an internal server error not a bad request

Image

  • logging request.Body without copying?
    Image
@Eslam-Nawara Eslam-Nawara self-assigned this Mar 18, 2025
@Eslam-Nawara Eslam-Nawara added type_bug Something isn't working node-registrar labels Mar 18, 2025
@Eslam-Nawara
Copy link
Collaborator

Eslam-Nawara commented Mar 19, 2025

WIP
Addressed most of the points here, but few are left will be addressed separately

  • adding mechanism to set node to Approved (should allow the farmer to approve the node to be part of their farm) add mechanism to set node to Approved #47
  • include a nonce and track it to prevent more replay attacks

@Eslam-Nawara Eslam-Nawara moved this to In Verification in 3.16.x Apr 6, 2025
@Eslam-Nawara Eslam-Nawara added this to the 0.17.x milestone Apr 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
node-registrar type_bug Something isn't working
Projects
Status: In Verification
Development

No branches or pull requests

2 participants