Penetration Testing and Ethical Hacking

Chapter 8
Web Application and API Penetration Testing

Diploma in Cyber security
Penetration Testing and Ethical Hacking   •   Diploma

Web Application and API Penetration Testing

Introduction

Web applications and application programming interfaces (APIs) are central to modern digital services. They process personal data, authorize financial transactions, connect mobile applications to cloud platforms, and integrate organizational systems. Their exposure to untrusted users makes them important targets for security assessment.

Web application and API penetration testing is an authorized, evidence-based process for identifying and validating security weaknesses. It combines automated discovery, manual analysis, code and configuration review, controlled exploitation, and risk communication. The objective is not merely to find technical defects, but to determine whether an attacker could violate confidentiality, integrity, availability, privacy, or business trust.

The OWASP Top 10 provides a widely used awareness framework for web application risks, while the OWASP API Security Top 10 emphasizes API-specific problems such as broken object-level authorization, excessive data exposure, unrestricted resource consumption, and unsafe consumption of APIs. These frameworks guide testing, but they do not replace professional judgment, application knowledge, or an agreed scope.

Learning Objectives

By the end of this chapter, a learner should be able to:

  • explain the attack surface of web applications and APIs;
  • assess authentication, authorization, sessions, input handling, and file processing;
  • recognize injection, insecure-design, business-logic, client-side, and API-specific weaknesses;
  • plan safe and legally authorized penetration tests;
  • validate findings without causing unnecessary harm;
  • recommend secure validation, monitoring, remediation, and retesting practices.

1. Scope, Authorization, and Testing Method

A professional assessment begins with written authorization. The rules of engagement should identify domains, API versions, mobile clients, test accounts, permitted hours, prohibited actions, data-handling requirements, emergency contacts, and reporting expectations. Testing a system without permission may violate computer-misuse, privacy, or telecommunications law even if the tester intends to improve security.

A typical workflow is:

  1. Planning: define objectives, scope, constraints, contacts, and success criteria.
  2. Reconnaissance: identify application functions, endpoints, parameters, technologies, roles, and trust boundaries.
  3. Threat modelling: consider assets, actors, entry points, abuse cases, and likely impacts.
  4. Testing: assess controls using low-risk requests before escalating validation.
  5. Evidence collection: record requests, responses, account roles, timestamps, and business impact while minimizing sensitive data.
  6. Analysis and reporting: distinguish confirmed vulnerabilities from observations and false positives.
  7. Remediation and retesting: verify that fixes work and have not introduced regressions.

Testing should use synthetic data where possible. Production testing requires special care because apparently harmless actions—such as changing an address, uploading a file, or triggering a password reset—may have real consequences.

2. Application Architecture and Trust Boundaries

A web application commonly includes a browser, web server, application services, databases, identity providers, storage systems, queues, and third-party services. An API may expose REST, GraphQL, SOAP, or event-based interfaces. Each component can apply different authentication, authorization, validation, logging, and rate-limiting rules.

A trust boundary exists where data or control passes between components with different assumptions. Examples include:

  • browser to web server;
  • API gateway to microservice;
  • application to database;
  • organization to payment provider;
  • user-controlled file to document-processing service.

A tester should ask: What does this component trust? Which identity is propagated? Is authorization enforced again at the service that owns the data? Are security headers, validation rules, and logging consistent across routes?

Web application and API trust boundaries BrowserUntrusted input Web/API layerAuthenticationValidationAuthorization ServicesBusiness logic DataRecords/files Security controls must be enforced at every trust boundary
Web application and API trust boundaries
Figure: Web application and API trust boundaries

Figure: A simplified web application architecture showing major trust boundaries.

3. Authentication and Identity

Authentication establishes who or what is making a request. Testing should examine registration, login, logout, password reset, multifactor authentication, account recovery, federation, and service-to-service credentials.

Important checks include:

  • whether passwords are protected by an appropriate slow password-hashing algorithm and unique salts;
  • whether login and recovery responses avoid revealing whether an account exists;
  • whether repeated attempts are controlled without creating an easy denial-of-service condition;
  • whether multifactor authentication is required for sensitive actions, not merely at initial login;
  • whether recovery tokens are random, single-use, short-lived, and invalidated after use;
  • whether session or token credentials are exposed in URLs, logs, referrers, or client storage;
  • whether disabled, deleted, or deprovisioned accounts lose access promptly;
  • whether OAuth or OpenID Connect redirect URIs, state values, nonce values, scopes, and token audiences are validated.

Authentication is not authorization. A valid identity may still be forbidden from viewing another customer’s invoice or performing an administrative action.

4. Authorization and Access Control

Authorization determines whether an authenticated identity may perform an operation on a resource. A useful model is to evaluate the tuple:

Subject → Action → Resource → Context

For example, a support agent may read a ticket assigned to the agent’s organization but may not export all customer records. Context can include tenant, ownership, time, device assurance, transaction state, or approval status.

Testing should use multiple controlled accounts representing different roles and tenants. Compare responses for:

  • horizontal access: one ordinary user accessing another user’s object;
  • vertical access: an ordinary user invoking an administrator function;
  • cross-tenant access: one organization accessing another organization’s data;
  • function-level access: direct requests to hidden or undocumented administrative endpoints;
  • method-level access: changing HTTP methods or content types to reach an unintended operation;
  • object-property access: modifying fields such as owner, role, price, approval state, or account identifier.

The safest design is deny-by-default, server-side enforcement, least privilege, and centralized policy evaluation. Client-side hiding of buttons is not authorization.

5. Sessions, Tokens, and State

A session binds successive requests to an authenticated identity. Session identifiers should be unpredictable, transmitted only over protected channels, and protected with appropriate cookie attributes: Secure, HttpOnly, and a suitable SameSite policy. Session identifiers should be rotated after authentication or privilege change and invalidated on logout, expiry, password reset, and account disablement.

For bearer tokens, possession generally grants access. Testing therefore considers storage, lifetime, audience, issuer, signature verification, algorithm handling, refresh-token rotation, revocation, and scope enforcement. A server must not accept a token merely because it is well-formed; it must validate its cryptographic integrity and claims.

Cross-site request forgery (CSRF) is relevant when browsers automatically attach credentials. Defenses may include SameSite cookies, anti-CSRF tokens, origin checks, and appropriate method design. CORS is not an authentication mechanism. A permissive CORS policy can expose responses to an untrusted origin when credentials are also permitted.

6. Input Handling and Injection

All external data should be treated as untrusted, including HTTP headers, JSON fields, path parameters, uploaded files, cookies, message queues, and data returned by third parties. Validation should be performed server-side and should be based on the expected type, length, range, syntax, and business meaning.

A robust validation strategy combines:

  • allowlisting: accept only known formats and values;
  • canonicalization: normalize equivalent representations before validation;
  • contextual encoding: encode output for HTML, JavaScript, URL, SQL, shell, or other contexts;
  • parameterized interfaces: separate data from commands;
  • size and resource limits: prevent excessive memory, processing, or storage use.

Injection occurs when data is interpreted as commands or expressions in another language. Relevant categories include SQL, NoSQL, operating-system command, LDAP, template, XPath, expression-language, and client-side script injection. Error messages, response differences, timing, and controlled test markers can help establish whether interpretation occurs, but testing should avoid destructive payloads.

For SQL, parameterized queries are the primary defense. Escaping alone is fragile because its correctness depends on encoding, database dialect, and context. For output-based risks such as cross-site scripting, the appropriate defense depends on context; HTML encoding is not automatically safe inside JavaScript or URLs. Content Security Policy can reduce impact but should not substitute for correct output handling.

7. Insecure Design and Business Logic

Some weaknesses cannot be fixed by adding a filter because the intended workflow is unsafe. Insecure design includes missing abuse controls, unsafe defaults, excessive trust in the client, and workflows that permit contradictory states.

Business-logic testing asks whether the application preserves important invariants. Examples include:

  • a discount being applied more than once;
  • a refund exceeding the original payment;
  • an order being shipped before payment is confirmed;
  • approval being performed by the same identity that created a request;
  • a user cancelling an operation after receiving its benefit;
  • a workflow step being skipped by calling a later endpoint directly;
  • concurrent requests causing a balance or inventory inconsistency.

The tester should model legitimate workflows, identify state transitions, and test each transition with authorized test data. Race-condition testing must be carefully controlled to avoid duplicate financial or operational effects. Recommendations may include server-side state machines, transaction controls, idempotency keys, atomic updates, segregation of duties, and event reconciliation.

8. File Upload, Download, and Processing

File functionality creates risks at multiple stages: upload, storage, retrieval, preview, conversion, scanning, and deletion. File type should not be trusted merely because of a filename extension or client-supplied MIME type.

Assessment areas include:

  • extension, MIME, magic-byte, and content validation;
  • maximum file size, archive expansion limits, and processing timeouts;
  • randomized storage names and storage outside executable web paths;
  • authorization checks on every download and preview;
  • protection against path traversal and unintended file overwrite;
  • malware scanning and quarantine workflows;
  • safe handling of office documents, images, archives, and active content;
  • prevention of server-side request forgery through document converters or URL fetchers;
  • safe error handling and deletion of temporary files.

Downloads should use controlled response headers, avoid reflecting attacker-controlled filenames unsafely, and enforce tenant and user authorization. A file scanner is valuable but is not a complete security boundary.

9. API-Specific Testing

APIs often expose more functionality than the graphical interface. Testing begins with an inventory of documented and observed endpoints, versions, HTTP methods, schemas, authentication mechanisms, pagination, filtering, and error formats.

Important API risks include:

Risk areaTesting focusSecure practice
Broken object-level authorizationChange object identifiers using separate accountsEnforce ownership or policy on every request
Broken function-level authorizationInvoke administrative routes as ordinary usersCentral, server-side role and policy checks
Excessive data exposureCompare returned fields with business needExplicit response schemas and data minimization
Mass assignmentSubmit protected fields such as role or ownerAllowlist writable properties
Unrestricted resource consumptionTest pagination, filtering, uploads, and expensive queriesRate, size, cost, and concurrency limits
Improper inventory managementDiscover old versions and undocumented routesVersion governance and retirement controls
Unsafe third-party API useSupply controlled external responses or URLsValidate, authenticate, constrain, and monitor dependencies

GraphQL assessments should consider introspection exposure, resolver-level authorization, query depth and complexity, batching, aliases, and sensitive fields. REST assessments should consider method consistency, content negotiation, pagination limits, and predictable identifiers. API gateways can help with rate limits and authentication, but downstream services must still enforce authorization.

10. Client-Side Security

Client-side code is untrusted. It may contain business rules, API endpoints, feature flags, source maps, embedded identifiers, and assumptions that an attacker can alter. Testing should inspect DOM manipulation, unsafe sinks, third-party scripts, source-map exposure, browser storage, postMessage handlers, service workers, and security headers.

Common controls include context-aware output encoding, safe DOM APIs, strict Content Security Policy, dependency governance, Subresource Integrity where appropriate, frame-embedding controls, and careful origin validation for messaging. Sensitive authorization decisions must remain on the server.

11. Secure Validation and Evidence-Based Reporting

A finding should be reproducible, scoped, and tied to impact. A useful report includes:

  • title and affected endpoint or component;
  • vulnerability category and security control involved;
  • preconditions and test accounts;
  • concise reproduction steps using non-destructive data;
  • sanitized request and response evidence;
  • confidentiality, integrity, availability, privacy, and business impact;
  • likelihood and affected population;
  • remediation guidance and references;
  • retest status and residual risk.

Severity systems such as CVSS can support prioritization, but a numerical score does not replace business context. A medium technical weakness in a payment approval workflow may deserve more urgent treatment than a high-scoring issue in an isolated test service.

Validation should include positive and negative tests. For example, a corrected authorization control should allow a user to access their own record, deny access to another tenant’s record, and continue to support legitimate administrator workflows. Tests should also check logs, alerts, rate-limit behavior, and fail-closed operation.

Applied Case: Illustrative Multi-Tenant Invoice API

Background

A fictional company, Northstar Billing, provides invoicing software to several organizations. Its web portal uses a REST API. Each tenant has administrators, billing staff, and read-only users.

Problem

During an authorized assessment, testers observed that invoices were retrieved through a route containing an invoice identifier. The user interface displayed only the current tenant’s invoices, and no obvious error occurred during normal use.

Analysis

Two test accounts were created in separate tenants. The tester confirmed that Account A could retrieve its own invoice. With the same authenticated session, the tester requested a controlled invoice identifier belonging to Account B. The response contained the invoice total, customer name, and line items. The application authenticated the user but did not authorize access to the specific object.

A second test showed that the JSON update endpoint accepted a writable field representing the invoice owner. The interface did not expose that field, but the server accepted it. This created a mass-assignment risk in addition to broken object-level authorization.

The tester did not modify production records. Evidence consisted of test tenants, sanitized identifiers, response metadata, and timestamps.

Approach and Solution

The recommended solution was to enforce tenant and object authorization in the service responsible for invoices, rather than relying on the user interface or API gateway. Writable properties were changed to an explicit allowlist. Database queries were constrained by both invoice identifier and authorized tenant context. Administrative actions required a separate policy check and were logged.

The organization also introduced automated tests for horizontal access, cross-tenant access, protected properties, and deprecated API versions. Monitoring was configured for repeated cross-tenant authorization failures.

Outcome

Retesting confirmed that Account A could access its own invoice, could not access Account B’s invoice, and could not change ownership. Authorized administrators retained required functionality. The case demonstrates that authentication, interface visibility, and object authorization are distinct controls.

Learning Points

  • Every object request requires a server-side authorization decision.
  • Hidden fields are not protected fields.
  • Multi-tenant boundaries should be represented in code, queries, tests, and monitoring.
  • Safe testing can prove impact without altering real business records.

Challenges, Limitations, and Governance

No penetration test provides proof that an application is secure. Time limits, incomplete credentials, undocumented services, environmental differences, and unavailable source code can create blind spots. Automated scanners may miss business logic, produce false positives, or generate excessive traffic. Manual testing is stronger for workflows but depends on tester skill and application knowledge.

Privacy governance is essential. Testers may encounter personal, financial, health, or authentication data. Organizations should minimize collection, restrict access, encrypt evidence, define retention periods, and securely destroy data. Findings should be disclosed only to authorized recipients.

Modern development introduces additional challenges. APIs are deployed through continuous delivery, infrastructure is distributed across cloud services, and AI features may process prompts, documents, or personal data. AI-assisted testing can help identify unusual response patterns or generate test cases, but it can also expose confidential source code or produce unreliable conclusions. Human review, authorization boundaries, and data-protection controls remain necessary.

Best-Practice Checklist

  • obtain written authorization and define scope;
  • map endpoints, roles, data flows, and trust boundaries;
  • test authentication separately from authorization;
  • use multiple users and tenants to test access boundaries;
  • validate server-side input and output in the correct context;
  • use parameterized interfaces and explicit property allowlists;
  • test sessions, tokens, recovery, and multifactor workflows;
  • assess file handling, resource limits, and third-party integrations;
  • examine business workflows and state transitions;
  • apply rate, size, cost, and concurrency controls to APIs;
  • collect minimal, reproducible evidence;
  • report impact and remediation clearly;
  • retest fixes and add regression tests.

Chapter Summary

Web application and API penetration testing evaluates whether security controls withstand realistic, authorized interaction. Effective assessment begins with scope and threat modelling, then examines identity, sessions, authorization, input handling, injection, file processing, business logic, API design, and client-side behavior. The most important distinction is between what a client displays and what the server enforces. Authentication identifies a requester; authorization decides what that requester may do.

OWASP guidance provides useful risk categories, but professional testing also requires architectural understanding, safe evidence collection, privacy protection, and business-context analysis. Multi-tenant authorization, mass assignment, excessive data exposure, unsafe workflows, and resource abuse are particularly significant in API-driven systems. Secure remediation combines deny-by-default policies, parameterized operations, contextual encoding, explicit schemas, least privilege, rate limits, strong session management, monitoring, and regression testing.

Key Terms

Authentication: The process of establishing the identity of a user, service, or device.

Authorization: A decision about whether an identity may perform an action on a resource.

API: A defined interface through which software components exchange requests and responses.

Broken object-level authorization: Failure to verify that a requester may access a specific object.

Business logic: Rules governing valid operations and state transitions in an application.

CSRF: An attack causing a browser to submit an unwanted authenticated request.

CORS: Browser policy controlling permitted cross-origin web requests.

Input validation: Checking data against expected type, structure, range, and meaning.

Injection: Untrusted data being interpreted as commands or expressions.

Mass assignment: Unintended modification of protected object properties through submitted fields.

Multi-tenancy: Architecture in which one service supports multiple logically separated organizations.

OWASP: Open Worldwide Application Security Project, a nonprofit security community and guidance publisher.

Penetration testing: Authorized security testing that validates vulnerabilities and their impact.

Race condition: A vulnerability caused by unsafe results from concurrent operations.

Session: Server- or token-maintained state associating requests with an identity.

Threat modelling: Structured analysis of assets, threats, trust boundaries, and controls.

Trust boundary: A point where data or authority crosses between different security assumptions.

Zero trust: A security approach that continuously verifies access rather than trusting network location.

Review Questions

Short Answer Questions

  1. How does authentication differ from authorization?
  2. Why should authorization be enforced on the server rather than only in the client interface?
  3. State two security properties of a password-reset token.
  4. What is mass assignment in an API?
  5. Why are parameterized queries preferred to escaping alone?

Long Answer Questions

  1. Explain a professional methodology for testing a web application from planning through retesting.
  2. Discuss how a tester can evaluate authorization in a multi-tenant API using controlled accounts.
  3. Explain the security risks associated with file upload and download functionality.
  4. Compare injection vulnerabilities with insecure-design and business-logic vulnerabilities.
  5. Describe how sessions, bearer tokens, CSRF protections, and CORS interact in a browser-based application.

Analytical / Application Questions

  1. A read-only user cannot see an administrator button but can call the administrator endpoint directly. Analyse the security weakness and propose controls.
  2. An API returns complete customer objects when the mobile client needs only a name and account status. Identify the risk, explain possible impact, and recommend a design correction.
  3. An online store permits a user to submit two refund requests at nearly the same time, producing a refund greater than the purchase value. Explain the likely class of flaw and propose testing and remediation approaches.

Multiple Choice Questions with Answers

Question 1

Which statement best distinguishes authentication from authorization?

A. Authentication determines identity; authorization determines permitted actions.

B. Authentication encrypts data; authorization compresses data.

C. Authentication limits traffic; authorization records logs.

D. Authentication validates files; authorization validates passwords.

Correct Answer: A. Authentication determines identity; authorization determines permitted actions.

Question 2

Which control most directly prevents SQL injection?

A. Hiding database error messages

B. Using parameterized queries

C. Adding a client-side input mask

D. Changing the database port

Correct Answer: B. Using parameterized queries

Question 3

What is the main purpose of testing with accounts from different tenants?

A. To measure page-load performance

B. To test password complexity

C. To identify cross-tenant authorization failures

D. To verify browser compatibility

Correct Answer: C. To identify cross-tenant authorization failures

Question 4

Which cookie attribute helps prevent client-side scripts from reading a session cookie?

A. Secure

B. HttpOnly

C. SameSite=None

D. Domain

Correct Answer: B. HttpOnly

Question 5

Which API weakness occurs when a user can alter a protected owner or role field by submitting it in JSON?

A. Mass assignment

B. Clickjacking

C. Session fixation

D. Open redirect

Correct Answer: A. Mass assignment

Question 6

Which is the strongest general approach to file-type validation?

A. Trust the filename extension

B. Trust the client-supplied MIME type

C. Use server-side content validation with controlled processing

D. Allow every file and rely only on antivirus software

Correct Answer: C. Use server-side content validation with controlled processing

Question 7

A refund is issued twice when two requests arrive concurrently. What class of issue is most relevant?

A. Race condition or unsafe concurrency handling

B. Cross-site scripting

C. Weak password hashing

D. Information disclosure through headers

Correct Answer: A. Race condition or unsafe concurrency handling

Question 8

Why is client-side hiding of an administrative button insufficient?

A. Browsers cannot render hidden buttons

B. A user may call the server endpoint directly

C. Hidden buttons always disable encryption

D. It prevents all logging

Correct Answer: B. A user may call the server endpoint directly

Question 9

Which practice best supports secure API responses?

A. Return every database field for flexibility

B. Use explicit response schemas and minimize returned data

C. Allow clients to select arbitrary internal fields

D. Disable authentication for read-only endpoints

Correct Answer: B. Use explicit response schemas and minimize returned data

Question 10

What should a penetration-test report primarily provide?

A. Only a list of tool output

B. Reproducible evidence, impact, affected scope, and remediation guidance

C. Unverified assumptions about the source code

D. A guarantee that no vulnerabilities exist

Correct Answer: B. Reproducible evidence, impact, affected scope, and remediation guidance

Penetration Testing and Ethical Hacking
Chapter 8 of 15