Ruby on Rails

Rails Security

CSRF protection, SQL injection, XSS, mass assignment, secrets management, HTTPS

22 câu hỏi phỏng vấn·
Senior
1

What mechanism does Rails use by default to protect against CSRF attacks?

Câu trả lời

Rails generates a unique CSRF token per session and automatically includes it in forms via a hidden field. This token is verified server-side for every non-GET request. The form_with helper automatically inserts this token, and protect_from_forgery is enabled by default in ApplicationController.

2

How to disable CSRF protection for a specific action in an API controller?

Câu trả lời

The skip_before_action :verify_authenticity_token method disables CSRF verification for specific actions. This is common for APIs using token-based authentication (JWT, API key) rather than sessions. It's recommended to limit this exception to strictly necessary actions using the only option.

3

What vulnerability is exploited in this code: User.where("name = '#{params[:name]}'")?

Câu trả lời

Direct params interpolation in a SQL query allows SQL injection. An attacker can send name="'; DROP TABLE users; --" to execute arbitrary SQL code. Use ActiveRecord placeholders instead: User.where(name: params[:name]) or User.where("name = ?", params[:name]).

4

Which ActiveRecord method automatically protects against SQL injection?

5

How does Rails automatically protect against XSS attacks in ERB views?

+19 câu hỏi phỏng vấn

Nắm vững Ruby on Rails cho lần phỏng vấn tiếp theo

Truy cập tất cả câu hỏi, flashcards, bài kiểm tra kỹ thuật, bài tập code review và mô phỏng phỏng vấn.

Bắt đầu miễn phí