Docsity

Backend Development REST APIs, Authentication, Middleware and Server Side Validation Exam, Exams of Nursing

Toggle sidebar
bg1
Backend Development: REST APIs,
Authentication, Middleware and Server-Side
Validation Examination - Newest Exam
Preparation With Complete Questions And
Correct Answers With Rationales
QUESTION 1
A REST API is being designed for a resource 'orders'. According
to REST principles, which HTTP method and URI combination is
most appropriate for creating a new order?
A. POST /orders
B. GET /orders/create
C. PUT /orders
D. POST /orders/create
CORRECT ANSWER
A. POST /orders
RATIONALE
In REST, POST to a collection URI is used to create a new resource
within that collection. The server assigns the new resource's URI. GET
should not be used for creation, and PUT is typically used to update or
create a resource at a known URI. Using a verb like 'create' in the URI
violates REST's uniform interface constraint.
QUESTION 2
A developer is implementing JWT-based authentication. The
token payload includes a 'role' claim. Which of the following is
the most critical security consideration regarding the storage
and transmission of this token?
1 | Page
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Backend Development REST APIs, Authentication, Middleware and Server Side Validation Exam and more Exams Nursing in PDF only on Docsity!

Backend Development: REST APIs,

Authentication, Middleware and Server-Side

Validation Examination - Newest Exam

Preparation With Complete Questions And

Correct Answers With Rationales

QUESTION 1

A REST API is being designed for a resource 'orders'. According to REST principles, which HTTP method and URI combination is most appropriate for creating a new order? A. POST /orders B. GET /orders/create C. PUT /orders D. POST /orders/create CORRECT ANSWER A. POST /orders RATIONALE In REST, POST to a collection URI is used to create a new resource within that collection. The server assigns the new resource's URI. GET should not be used for creation, and PUT is typically used to update or create a resource at a known URI. Using a verb like 'create' in the URI violates REST's uniform interface constraint. QUESTION 2 A developer is implementing JWT-based authentication. The token payload includes a 'role' claim. Which of the following is the most critical security consideration regarding the storage and transmission of this token?

A. The token should be stored in localStorage to prevent CSRF attacks. B. The token must be signed and transmitted over HTTPS to prevent tampering and interception. C. The token should be encrypted with a symmetric key to hide the role claim from the client. D. The token should be stored in a cookie with the HttpOnly flag to prevent XSS, but this alone is sufficient. CORRECT ANSWER B. The token must be signed and transmitted over HTTPS to prevent tampering and interception. RATIONALE JWTs are signed to ensure integrity; without verification, an attacker could alter the role claim. Transmission over HTTPS prevents interception. Storing in localStorage is vulnerable to XSS, and while HttpOnly cookies mitigate XSS, they are not sufficient alone (CSRF protection is also needed). Encrypting the token is not standard for JWTs and would require additional mechanisms like JWE, but the most critical is signing and HTTPS. QUESTION 3 In an Express.js application, middleware functions are executed in the order they are defined. Consider the following middleware stack: (1) logger, (2) authentication, (3) authorization, (4) route handler. If the authentication middleware calls next('route'), what is the effect? A. It skips the remaining middleware and route handlers for the current route and moves to the next route. B. It skips the authorization middleware and proceeds directly to the route handler. C. It stops the request-response cycle and sends an error response.

QUESTION 5

When implementing server-side validation for a user registration form, which of the following is the most important reason to perform validation on the server rather than relying solely on client-side validation? A. Server-side validation is faster than client-side validation. B. Client-side validation can be bypassed, so server-side validation ensures data integrity and security. C. Server-side validation provides a better user experience with immediate feedback. D. Client-side validation cannot handle complex validation rules. CORRECT ANSWER B. Client-side validation can be bypassed, so server-side validation ensures data integrity and security. RATIONALE Client-side validation is for user convenience but can be disabled or bypassed by malicious users. Server-side validation is essential to enforce business rules and security constraints, ensuring that only valid data is processed and stored. QUESTION 6 A developer is designing a RESTful API for a blog. They need to allow clients to retrieve comments for a specific post. Which URI design best follows REST conventions? A. GET /posts/{postId}/comments B. GET /comments?postId={postId} C. GET /getCommentsForPost/{postId} D. GET /posts/comments/{postId} CORRECT ANSWER

A. GET /posts/{postId}/comments RATIONALE RESTful URIs should represent resources hierarchically. Comments are sub-resources of a post, so /posts/{postId}/comments is the most natural and conventional. Query parameters can be used for filtering, but the hierarchical path is preferred for sub-resources. Using verbs in URIs is not RESTful. QUESTION 7 In the context of HTTP authentication, which of the following statements about the 'Bearer' token scheme is correct? A. Bearer tokens are typically used with the Basic authentication scheme. B. Bearer tokens must be sent in the Authorization header with the prefix 'Bearer '. C. Bearer tokens are inherently secure and do not require HTTPS. D. Bearer tokens are only used for OAuth 1.0. CORRECT ANSWER B. Bearer tokens must be sent in the Authorization header with the prefix 'Bearer '. RATIONALE Bearer tokens are used in OAuth 2.0 and are sent in the Authorization header as 'Bearer '. They are not part of Basic authentication (which uses Base64-encoded username:password). They must be transmitted over HTTPS to prevent interception. They are not limited to OAuth 1.0. QUESTION 8 A middleware function in an Express application is defined with four parameters: (err, req, res, next). What is the purpose of this

situation. Changing the method is irrelevant. QUESTION 10 When validating input on the server, which of the following practices is most effective in preventing SQL injection attacks? A. Using parameterized queries (prepared statements). B. Escaping special characters in user input. C. Validating input length and format. D. Using stored procedures with dynamic SQL. CORRECT ANSWER A. Using parameterized queries (prepared statements). RATIONALE Parameterized queries separate SQL code from data, preventing attackers from altering the query structure. Escaping is error-prone and not foolproof. Input validation helps but is not sufficient alone. Stored procedures with dynamic SQL can still be vulnerable if they concatenate input. QUESTION 11 A developer is implementing CORS (Cross-Origin Resource Sharing) for a REST API. Which HTTP header is used by the server to indicate which origins are allowed to access the resource? A. Access-Control-Allow-Origin B. Access-Control-Allow-Methods C. Access-Control-Allow-Headers D. Access-Control-Expose-Headers CORRECT ANSWER A. Access-Control-Allow-Origin

RATIONALE

Access-Control-Allow-Origin specifies the allowed origins. Access- Control-Allow-Methods specifies allowed HTTP methods, Access- Control-Allow-Headers specifies allowed headers, and Access-Control- Expose-Headers specifies which headers can be exposed to the client. The question asks for origins, so A is correct. QUESTION 12 In a REST API, which HTTP status code is most appropriate to return when a resource is successfully created? A. 200 OK B. 201 Created C. 204 No Content D. 202 Accepted CORRECT ANSWER B. 201 Created RATIONALE 201 Created indicates that the request has been fulfilled and resulted in a new resource being created. It often includes a Location header with the URI of the new resource. 200 OK is generic success, 204 No Content is for successful requests with no body, and 202 Accepted is for asynchronous processing. QUESTION 13 A developer is using JSON Web Tokens (JWT) for authentication. The token includes an expiration time (exp). What is the primary purpose of this claim? A. To specify the algorithm used for signing the token. B. To limit the token's validity period, reducing the risk of misuse if compromised.

QUESTION 15

A middleware function in an Express application is defined as: function middleware(req, res, next) { console.log('Request received'); next(); }. What happens if next() is not called? A. The request will hang and eventually time out because the response is not sent. B. The next middleware in the stack will be executed automatically. C. An error will be thrown immediately. D. The server will automatically send a 500 error response. CORRECT ANSWER A. The request will hang and eventually time out because the response is not sent. RATIONALE In Express, middleware must call next() to pass control to the next middleware or route handler, or send a response. If neither is done, the request will hang until it times out. No automatic error is thrown. QUESTION 16 Which of the following is an example of server-side validation that cannot be reliably performed on the client side? A. Checking that an email address is in a valid format. B. Ensuring that a password meets complexity requirements. C. Verifying that a username is unique in the database. D. Confirming that two password fields match. CORRECT ANSWER C. Verifying that a username is unique in the database. RATIONALE Uniqueness requires access to the database, which is on the server. Client-side validation can check format, complexity, and matching

fields, but cannot reliably check uniqueness without a server round- trip. Server-side validation is essential for uniqueness. QUESTION 17 A REST API returns a 403 Forbidden status code. Which of the following scenarios best describes when this status is appropriate? A. The client provided no authentication credentials. B. The client provided valid credentials but does not have permission to access the resource. C. The requested resource does not exist. D. The server is temporarily overloaded. CORRECT ANSWER B. The client provided valid credentials but does not have permission to access the resource. RATIONALE 403 Forbidden means the server understood the request but refuses to authorize it. Authentication is not the issue; the authenticated user lacks necessary permissions. 401 is for missing/invalid credentials, 404 for not found, and 503 for overload. QUESTION 18 In the context of API security, what is the primary risk of using HTTP Basic Authentication without HTTPS? A. Credentials are sent in plaintext and can be intercepted. B. The credentials are encrypted with a weak algorithm. C. The authentication token expires too quickly. D. The server cannot verify the credentials. CORRECT ANSWER

C. Sanitizing input to remove or escape dangerous characters. D. Checking that the Content-Type header is application/json. CORRECT ANSWER C. Sanitizing input to remove or escape dangerous characters. RATIONALE While all options are good practices, sanitizing input is critical to prevent injection attacks (e.g., SQL injection, XSS) by neutralizing dangerous characters. Well-formedness and type validation help but do not prevent injection if data is used in queries without proper handling. Content-Type checking is for correct parsing. QUESTION 21 A REST API is being designed for a resource 'orders'. Which HTTP method and status code combination is most appropriate for creating a new order and returning the created resource with its assigned ID? A. POST /orders with 201 Created and Location header B. PUT /orders with 200 OK and response body C. POST /orders with 200 OK and response body D. GET /orders with 201 Created and Location header CORRECT ANSWER A. POST /orders with 201 Created and Location header RATIONALE POST is used to create a new resource in a collection. A successful creation should return 201 Created and include a Location header pointing to the new resource. PUT is typically used for full updates or creation with a client-specified ID, but here the server assigns the ID. GET is safe and should not create resources. Returning 200 OK is less precise than 201 for creation.

QUESTION 22

In a RESTful API, which of the following best describes the principle of statelessness? A. The server stores session data for each client to maintain state across requests. B. Each request from client to server must contain all information necessary to understand and process the request. C. The client must store all application state, and the server only handles authentication. D. The server maintains a global state that is shared across all clients. CORRECT ANSWER B. Each request from client to server must contain all information necessary to understand and process the request. RATIONALE Statelessness means the server does not store any client context between requests. Each request must include all necessary information (e.g., authentication token, parameters) for the server to process it independently. This improves scalability and reliability. Options A and D describe stateful designs, and C is incorrect because the server may still manage resource state, but not client session state. QUESTION 23 A developer is implementing JWT-based authentication. Where should the JWT be securely stored on the client to mitigate XSS attacks while still allowing it to be sent with API requests? A. In localStorage B. In a JavaScript variable C. In an HttpOnly cookie D. In sessionStorage

functions? A. To define route handlers that send responses. B. To execute code, modify request and response objects, end the request-response cycle, or call the next middleware. C. To connect to a database and perform queries. D. To serve static files only. CORRECT ANSWER B. To execute code, modify request and response objects, end the request-response cycle, or call the next middleware. RATIONALE Middleware functions in Express have access to the request and response objects and the next middleware function. They can execute code, modify req/res, end the cycle, or call next(). They are not limited to route handlers, database connections, or static file serving, although they can perform those tasks. The defining characteristic is the ability to process the request in a pipeline. QUESTION 26 A middleware function is defined as (req, res, next) => { console.log('Logging'); next(); }. What happens if next() is omitted? A. The request will hang and eventually time out because the response is not sent. B. The next middleware will still be called automatically. C. An error will be thrown immediately. D. The response will be sent with a 200 status code. CORRECT ANSWER A. The request will hang and eventually time out because the response is not sent.

RATIONALE

If next() is not called, the request-response cycle is not completed, and control is not passed to the next middleware. The client will wait indefinitely until a timeout occurs. Express does not automatically call next(). No error is thrown unless explicitly done, and no response is sent. QUESTION 27 Which of the following is a critical security practice when implementing server-side validation? A. Relying solely on client-side validation for data integrity. B. Validating and sanitizing all input on the server, even if client-side validation exists. C. Disabling validation for trusted users. D. Using only regular expressions for validation. CORRECT ANSWER B. Validating and sanitizing all input on the server, even if client- side validation exists. RATIONALE Server-side validation is essential because client-side validation can be bypassed. All input must be validated and sanitized on the server to prevent injection attacks and ensure data integrity. Relying solely on client-side validation is insecure. Disabling validation for trusted users is risky. Regular expressions are a tool but not sufficient alone; validation should be comprehensive. QUESTION 28 In the context of REST APIs, what does the HATEOAS constraint entail? A. The API must use JSON exclusively.

is safe and should not modify resources. QUESTION 30 Which of the following is a common vulnerability that server-side validation helps prevent? A. Cross-Site Scripting (XSS) B. SQL Injection C. Cross-Site Request Forgery (CSRF) D. Man-in-the-Middle (MITM) attacks CORRECT ANSWER B. SQL Injection RATIONALE Server-side validation and parameterized queries help prevent SQL Injection by ensuring that user input is not interpreted as SQL code. XSS is primarily mitigated by output encoding and input sanitization, but server-side validation can help. CSRF is mitigated by anti-CSRF tokens. MITM is mitigated by HTTPS. SQL Injection is directly prevented by proper server-side input handling. QUESTION 31 In OAuth 2.0, which grant type is recommended for a single-page application (SPA) that cannot securely store a client secret? A. Authorization Code with PKCE B. Client Credentials C. Resource Owner Password Credentials D. Implicit CORRECT ANSWER A. Authorization Code with PKCE

RATIONALE

Authorization Code with PKCE (Proof Key for Code Exchange) is the recommended flow for public clients like SPAs because it mitigates authorization code interception without requiring a client secret. Client Credentials is for machine-to-machine. Resource Owner Password Credentials is deprecated and insecure. Implicit flow is deprecated due to token leakage risks. QUESTION 32 What is the purpose of the 'next' function in Express middleware? A. To send a response to the client. B. To pass control to the next middleware function in the stack. C. To restart the request-response cycle. D. To terminate the server. CORRECT ANSWER B. To pass control to the next middleware function in the stack. RATIONALE The next function is used to pass control to the next middleware function. If not called, the request will hang. It does not send a response (that is done via res.send or similar) nor restart the cycle or terminate the server. QUESTION 33 Which HTTP status code indicates that the server has successfully processed the request but there is no content to return? A. 200 OK B. 201 Created C. 204 No Content