security: constant-time token compare + num_steps/image caps
Browse files21 unmodified lines2223242526279 unmodified lines37383940414273 unmodified lines1161171181191201211221231241251261271284 unmodified lines13313413513613713813921 unmodified linesimport base64import ioimport osimport threadingfrom typing import Optional9 unmodified linesAUTH_TOKEN = os.environ.get("NORI_INFER_TOKEN") # REQUIRED — the rollout sends it# bf16 fits <16GB (A10G/L4). Set MOLMOACT_BF16=0 to run fp32 (~26GB, needs L40S/48GB).DTYPE = torch.bfloat16 if os.environ.get("MOLMOACT_BF16", "1") == "1" else torch.float32app = FastAPI(title="nori-molmoact2")_model = None73 unmodified lines@app.post("/act", response_model=ActResponse)def act(req: ActRequest, authorization: Optional[str] = Header(None)) -> ActResponse:if authorization != f"Bearer {AUTH_TOKEN}":raise HTTPException(status_code=401, detail="bad or missing bearer token")if _model is None:detail = f"model load failed: {_load_error}" if _load_error else "model not loaded yet"raise HTTPException(status_code=503, detail=detail)if len(req.images) < 1:raise HTTPException(status_code=422, detail="need at least one camera image")images = [_decode(b) for b in req.images]state = np.asarray(req.state, dtype=np.float32)with _lock, torch.no_grad():4 unmodified linesstate=state,norm_tag=NORM_TAG,inference_action_mode="continuous",num_steps=req.num_steps,normalize_language=True,enable_cuda_graph=True,)21 unmodified lines222324252627289 unmodified lines3839404142434445464773 unmodified lines1211221231241251261271281291301311321331341351364 unmodified lines14114214314414514614721 unmodified linesimport base64import ioimport osimport secretsimport threadingfrom typing import Optional9 unmodified linesAUTH_TOKEN = os.environ.get("NORI_INFER_TOKEN") # REQUIRED — the rollout sends it# bf16 fits <16GB (A10G/L4). Set MOLMOACT_BF16=0 to run fp32 (~26GB, needs L40S/48GB).DTYPE = torch.bfloat16 if os.environ.get("MOLMOACT_BF16", "1") == "1" else torch.float32# Defensive caps: a valid-token caller can't burn unbounded GPU via a huge solver# step count or a flood of images (the endpoint is public, token-gated).MAX_NUM_STEPS = 50MAX_IMAGES = 6app = FastAPI(title="nori-molmoact2")_model = None73 unmodified lines@app.post("/act", response_model=ActResponse)def act(req: ActRequest, authorization: Optional[str] = Header(None)) -> ActResponse:# Constant-time compare so a bad token can't be recovered via response timing.# Checked BEFORE any model work so unauthenticated calls never touch the GPU.if not authorization or not secrets.compare_digest(authorization, f"Bearer {AUTH_TOKEN}"):raise HTTPException(status_code=401, detail="bad or missing bearer token")if _model is None:detail = f"model load failed: {_load_error}" if _load_error else "model not loaded yet"raise HTTPException(status_code=503, detail=detail)if not 1 <= len(req.images) <= MAX_IMAGES:raise HTTPException(status_code=422, detail=f"need 1..{MAX_IMAGES} camera images")num_steps = max(1, min(int(req.num_steps), MAX_NUM_STEPS)) # clamp GPU costimages = [_decode(b) for b in req.images]state = np.asarray(req.state, dtype=np.float32)with _lock, torch.no_grad():4 unmodified linesstate=state,norm_tag=NORM_TAG,inference_action_mode="continuous",num_steps=num_steps,normalize_language=True,enable_cuda_graph=True,)
molmoact2_server.py+12-4