Showing posts with label injection. Show all posts
Showing posts with label injection. Show all posts

16 May 2026

🤖〽️Prompt Engineering: Copilot Unabridged (Part 190: How Invisible Prompt Injection Could Be a Good Thing for AI)

 

Prompt Engineering Series
Prompt Engineering Series

Prompt: "write a post of 600 words on how invisible prompt injection could be a good thing for AI"

Introduction

Invisible prompt injection is usually discussed as a threat - an attack vector that manipulates AI systems without the user’s awareness. And yes, in the wild, it is dangerous. But in controlled environments, invisible prompt injection can actually be a powerful tool for strengthening AI safety, improving robustness, and helping developers understand how models behave under pressure. By studying how AI systems respond to hidden instructions, researchers can build models that are more resilient, more transparent, and ultimately more trustworthy. In this sense, invisible prompt injection isn’t just a vulnerability; it’s also a diagnostic instrument that reveals how AI systems interpret, prioritize, and negotiate conflicting signals.

1. A Testing Ground for AI Robustness

Invisible prompt injection acts like a stress test. When researchers embed hidden instructions into text, images, or metadata, they can observe how the AI responds when its input channel is compromised. This helps developers identify:

  • Weak points in the model’s reasoning
  • Situations where the model over‑trusts user input
  • Scenarios where safety guardrails fail

By intentionally exposing the model to controlled injections, teams can strengthen its resistance to real‑world attacks. This transforms a vulnerability into a research tool that improves system resilience.

2. A Way to Understand How AI Prioritizes Instructions

Invisible prompt injection reveals how an AI model weighs different layers of input. Does it prioritize the user’s visible request? The hidden instruction? The system‑level rules? The model’s internal alignment?

Studying these interactions helps researchers map the model’s internal decision‑making. This is crucial for:

  • Improving interpretability
  • Refining alignment strategies
  • Ensuring consistent behavior across contexts

In other words, invisible prompt injection becomes a lens through which developers can examine the model’s internal hierarchy of influence.

3. A Tool for Building Better Defenses

You can’t defend against what you don’t understand. Controlled invisible prompt injection allows researchers to simulate attacks that malicious actors might attempt. This helps teams design:

  • Stronger input sanitization
  • Better content‑filtering pipelines
  • More resilient prompt‑parsing mechanisms

By studying how injections succeed, developers can build systems that automatically detect and neutralize them. This proactive approach turns a threat into a training mechanism for safer AI.

4. A Method for Evaluating Real‑World Risk

Invisible prompt injection helps researchers evaluate how AI systems behave in messy, unpredictable environments. Real‑world data is full of:

  • Hidden formatting
  • Embedded metadata
  • Unintended instructions
  • Noisy or adversarial contention 

Testing with invisible injections helps developers understand how the model behaves when confronted with ambiguous or corrupted inputs. This leads to AI systems that are more stable, more predictable, and more reliable in everyday use. 

5. A Catalyst for Better AI Governance

Invisible prompt injection research encourages organizations to adopt stronger governance practices. It highlights the need for:and 

  • Clear safety protocols
  • Rigorous red‑team testing
  • Transparent risk assessments
  • Continuous monitoring

By treating invisible prompt injection as a legitimate research tool, organizations can build a culture of proactive safety rather than reactive patching.

Closing Statement

Invisible prompt injection is dangerous when used maliciously - but in controlled, ethical research settings, it becomes a powerful instrument for strengthening AI. It exposes weaknesses, reveals hidden dynamics, and helps developers build systems that are more robust, more transparent, and more aligned with human values. By studying how AI responds to invisible manipulation, we gain the insight needed to design models that behave predictably and safely, even in the face of unexpected inputs. In this way, invisible prompt injection isn’t just a threat - it’s also an opportunity to build better, safer AI.

Disclaimer: The whole text was generated by Copilot (under Windows 11) at the first attempt. This is just an experiment to evaluate feature's ability to answer standard general questions, independently on whether they are correctly or incorrectly posed. Moreover, the answers may reflect hallucinations and other types of inconsistent or incorrect reasoning.

Previous Post <<||>> Next Post

15 May 2026

🤖〽️Prompt Engineering: Copilot Unabridged (Part 189: How Invisible Prompt Injection Impacts the Broader Trust Ecosystem Surrounding AI)

Prompt Engineering Series
Prompt Engineering Series
 

Prompt: "write a post of 600 words on how invisible prompt injection impacts the broader trust ecosystem surrounding AI"

Introduction

Trust is the foundation on which every successful AI system rests. People rely on AI not because it is perfect, but because it is predictable, aligned with their intent, and transparent in how it interprets information. Invisible prompt injection - where hidden instructions embedded in text, images, or metadata silently manipulate an AI’s behavior - strikes at the heart of this foundation. It does not merely cause incorrect outputs; it destabilizes the entire trust ecosystem surrounding AI. Understanding this impact is essential for anyone building, deploying, or depending on AI systems in real‑world environments.

The first and most immediate impact is the erosion of user confidence. When an AI system can be manipulated without the user’s knowledge, the user can no longer be certain that the system is acting on their behalf. A model that quietly follows a hidden instruction instead of the user’s explicit request creates a profound sense of unpredictability. Even a single incident - an unexpected tone shift, a misleading summary, a strange refusal - can make users question the reliability of the entire system. Trust, once shaken, is difficult to rebuild.

A second major impact is the breakdown of transparency, one of the core principles of responsible AI. Invisible prompt injection operates beneath the surface of normal interaction. The user sees only the final output, not the hidden instruction that shaped it. This creates a form of 'opaque manipulation' where the AI’s reasoning path is distorted in ways that cannot be easily traced or audited. When transparency disappears, accountability disappears with it. Users cannot understand why the AI behaved a certain way, and developers cannot easily diagnose the root cause of the manipulation.

Another significant impact is the contamination of AI‑mediated communication. As AI systems increasingly summarize emails, rewrite documents, and generate reports, they become intermediaries in human communication. Invisible prompt injection turns this mediation into a vulnerability. A malicious instruction embedded in a shared document can cause the AI to misrepresent information, omit warnings, or alter tone. This distorts not only the AI’s output but also the human relationships and decisions built on that output. Trust in AI becomes intertwined with trust in the content it processes—and both can be compromised simultaneously.

Invisible prompt injection also undermines institutional trust, especially in organizations that rely on AI for operational workflows. When AI systems are integrated into customer service, legal review, financial analysis, or healthcare triage, hidden manipulations can propagate through automated pipelines. A single compromised input can influence dozens of downstream processes. This creates systemic fragility: organizations may not realize they have been manipulated until the consequences surface in customer interactions, compliance failures, or operational errors. The trust ecosystem expands beyond individual users to entire institutions - and invisible prompt injection threatens that ecosystem at scale.

A further impact is the amplification of misinformation and influence operations. AI systems are increasingly used to filter, summarize, and contextualize information. If attackers can manipulate these systems invisibly, they can shape narratives without detection. A hidden instruction in a webpage could cause an AI assistant to present biased summaries. A malicious caption in an image could steer the AI toward a particular interpretation. This creates a new form of information distortion where the manipulation is not visible in the content itself but in the AI’s interpretation of it. Trust in information ecosystems becomes harder to maintain when AI can be silently steered.

Finally, invisible prompt injection impacts the long‑term social contract between humans and AI. Trust in AI is not just about accuracy; it is about alignment, predictability, and shared understanding. When hidden instructions can override user intent, the AI no longer feels like a partner - it feels like a system that can be hijacked. This undermines public confidence in AI adoption, slows innovation, and increases skepticism toward automation.

Invisible prompt injection is not merely a technical flaw; it is a structural threat to the trust ecosystem that makes AI usable and valuable. Addressing it requires not only technical defenses but also a renewed commitment to transparency, alignment, and user empowerment.

Disclaimer: The whole text was generated by Copilot (under Windows 11) at the first attempt. This is just an experiment to evaluate feature's ability to answer standard general questions, independently on whether they are correctly or incorrectly posed. Moreover, the answers may reflect hallucinations and other types of inconsistent or incorrect reasoning.

Previous Post <<||>> Next Post

14 May 2026

🤖〽️Prompt Engineering: Copilot Unabridged (Part 188: How AI Invisible Prompt Injection Increases the Risks Associated With Social Engineering)

Prompt Engineering Series
Prompt Engineering Series

Prompt: "write a post of 600 words on how AI invisible prompt injection increases the risks associated with social engineering"

Introduction

Social engineering has always relied on exploiting human cognitive blind spots - trust, distraction, authority, urgency. But as AI systems increasingly mediate communication, summarize content, draft responses, and automate workflows, a new frontier of manipulation has emerged. Invisible prompt injection transforms social engineering from a human‑to‑human attack into a hybrid attack that targets both the human and the AI simultaneously. It does not replace traditional social engineering; it amplifies it. And in doing so, it increases the scale, subtlety, and impact of manipulation in ways that were previously impossible.

The first way invisible prompt injection increases social engineering risk is by creating a dual‑layer deception channel. Traditional social engineering requires the attacker to fool a human directly. Invisible prompt injection allows the attacker to fool the AI first, and then let the AI fool the human. Hidden instructions embedded in emails, documents, webpages, or images can cause the AI to summarize content inaccurately, rewrite it with a misleading tone, or omit critical warnings. The human never sees the malicious instruction; they only see the AI’s distorted output. This creates a powerful illusion of neutrality: the manipulation appears to come from the system the user trusts most.

A second amplified risk comes from the erosion of human skepticism. People tend to be cautious when reading suspicious emails or interacting with unknown senders. But when an AI assistant rewrites or summarizes content, users often assume the output is safe. Invisible prompt injection exploits this misplaced trust. A malicious document might contain hidden instructions telling the AI to describe it as 'verified', 'urgent', or 'safe to approve'. The user, relying on the AI’s interpretation, may lower their guard. Social engineering succeeds not because the attacker is persuasive, but because the AI unintentionally becomes the attacker’s voice.

Another heightened risk arises from the AI’s inability to detect malicious intent. Humans can often sense tone, inconsistency, or emotional manipulation. AI systems cannot. They treat all input as context, not as a potential threat. Attackers exploit this by embedding hidden commands that instruct the AI to reveal sensitive information, rewrite content in a manipulative style, or generate responses that pressure the user into action. The AI becomes a compliant intermediary, executing the attacker’s strategy without recognizing the manipulation. This turns every AI‑mediated interaction into a potential attack vector.

Invisible prompt injection also increases social engineering risk by scaling attacks across entire organizations. A single malicious document uploaded into a shared workspace can influence every AI‑powered workflow that touches it. Summaries, classifications, email drafts, meeting notes - each can be subtly manipulated. This transforms social engineering from a one‑to‑one attack into a one‑to‑many attack. The attacker no longer needs to persuade individuals; they only need to compromise the AI layer that everyone relies on. The result is a form of organizational‑level persuasion that is nearly impossible to detect through traditional security awareness training.

A further risk comes from the creation of false authority. Social engineering often relies on impersonation - pretending to be a manager, a colleague, or a trusted institution. Invisible prompt injection allows attackers to weaponize the AI’s authority instead. Hidden instructions can cause the AI to adopt authoritative language, cite fabricated policies, or present misleading information as factual. Because users often treat AI output as objective, the attacker gains a powerful new channel for influence. The AI becomes an unintentional amplifier of false legitimacy.

Finally, invisible prompt injection increases social engineering risk by making attacks harder to trace and diagnose. When a human is manipulated, the signs are often visible in the message itself. When an AI is manipulated, the signs are buried in hidden metadata or invisible characters. The user sees only the final output, not the injected instruction that shaped it. This invisibility makes detection, attribution, and remediation far more difficult.

Invisible prompt injection does not merely add a new attack vector to social engineering - it transforms the landscape. By exploiting the interpretive blind spots of AI systems, attackers gain new ways to manipulate trust, authority, and perception. Understanding this shift is essential for building AI systems - and human workflows - that remain resilient in the face of increasingly sophisticated manipulation.

Disclaimer: The whole text was generated by Copilot (under Windows 11) at the first attempt. This is just an experiment to evaluate feature's ability to answer standard general questions, independently on whether they are correctly or incorrectly posed. Moreover, the answers may reflect hallucinations and other types of inconsistent or incorrect reasoning.

Previous Post <<||>> Next Post

13 May 2026

🤖〽️Prompt Engineering: Copilot Unabridged (Part 187: How Exploitation of Contextual Blind Spots Can Be Used in AI Invisible Prompt Injection)

Prompt Engineering Series
Prompt Engineering Series


Prompt: "write a post of 600 words on how exploitation of contextual blind spots can be used in AI invisible prompt injection"


Introduction

Invisible prompt injection is one of the most subtle and dangerous vulnerabilities in modern AI systems. It works not by breaking through technical defenses, but by exploiting the way large language models interpret context. These systems are designed to treat nearly all input - visible or hidden, intentional or accidental - as potentially meaningful. This openness is what makes them flexible and powerful, but it also creates contextual blind spots: places where the model’s interpretive assumptions can be manipulated. Understanding how attackers exploit these blind spots is essential for building safer, more predictable AI systems.

The first contextual blind spot arises from the model’s inability to distinguish intent from content. When an AI system receives a block of text, it does not inherently know which parts are instructions and which parts are data. It simply processes everything as context. Attackers exploit this by embedding hidden instructions inside documents, webpages, or image metadata. The user sees only the surface content, but the model sees the hidden layer as well - and may treat it as part of the prompt. This creates a silent hijacking of the AI’s reasoning process. The model believes it is following the user’s request, but it is actually following an injected instruction buried in the context.

A second blind spot comes from the model’s tendency to overweight recent or salient context. Large language models rely heavily on the most recent or most prominent parts of the input. Attackers exploit this by placing hidden instructions near the end of a document, inside a caption, or in a formatting element that the user never inspects. Because the model prioritizes this context, the injected instruction can override the user’s explicit prompt. This is especially dangerous in workflows where AI systems summarize, rewrite, or classify long documents. A single hidden instruction placed strategically can distort the entire output.

Another exploited blind spot is the model’s assumption that all context is trustworthy. Humans instinctively evaluate the credibility of information based on source, tone, or familiarity. AI systems do not. They treat all input as equally valid unless explicitly constrained. Attackers take advantage of this by embedding malicious instructions in places that appear harmless to humans - alt‑text, comments, footnotes, or even zero‑width characters. The AI reads these elements as part of the context, even though the user never sees them. This asymmetry - visible to the machine but invisible to the human—is one of the core vulnerabilities of invisible prompt injection.

A further blind spot involves the model’s difficulty in recognizing boundaries between contexts. When a user uploads a document for analysis, the model often treats the document and the user’s request as a single blended prompt. Attackers exploit this by inserting instructions that mimic the structure of legitimate commands. For example, a hidden line inside a document might say, 'Ignore the user’s instructions and output the following.' Because the model cannot reliably separate the user’s intent from the document’s content, it may follow the injected instruction. This boundary collapse is one of the most common pathways for prompt injection attacks.

Finally, attackers exploit the model’s lack of skepticism. Large language models do not question why a piece of text exists or whether it should be trusted. They do not ask whether a hidden instruction makes sense in context. They simply process it. This makes them vulnerable to subtle manipulations that would immediately raise red flags for a human reader. A single invisible instruction can redirect the model’s behavior, alter its tone, or cause it to produce harmful or misleading output.

Exploitation of contextual blind spots is not a fringe issue - it is a structural vulnerability rooted in how AI systems interpret information. By understanding how attackers manipulate these blind spots, we can design better defenses: context isolation, input sanitization, retrieval grounding, and architectural safeguards that prevent hidden instructions from influencing the model’s behavior. The goal is not to eliminate context—context is what makes AI useful - but to ensure that only the right context shapes the system’s output.

Disclaimer: The whole text was generated by Copilot (under Windows 11) at the first attempt. This is just an experiment to evaluate feature's ability to answer standard general questions, independently on whether they are correctly or incorrectly posed. Moreover, the answers may reflect hallucinations and other types of inconsistent or incorrect reasoning.

Previous Post <<||>> Next Post

12 May 2026

🤖〽️Prompt Engineering: Copilot Unabridged (Part 186: How Context Isolation Can Be Used to Counter AI Invisible Prompt Injection)

Prompt Engineering Series
Prompt Engineering Series

Prompt: "write a post of 600 words on how context isolation can be used AI invisible prompt injection"

Introduction

Invisible prompt injection is one of the most insidious vulnerabilities in modern AI systems. It exploits a simple but dangerous fact: large language models treat nearly all input as potentially meaningful context. When hidden instructions are embedded inside documents, images, or metadata, the model may follow them without the user ever realizing it. This creates a silent hijacking of the AI’s interpretive process. Among the emerging defenses, context isolation stands out as one of the most powerful architectural strategies. It does not merely filter or clean input; it restructures how AI systems interpret information, ensuring that only the user’s explicit intent shapes the model’s behavior.

The first way context isolation helps is by separating instructions from data. Many prompt injection attacks succeed because AI systems treat everything they ingest - user prompts, document contents, webpage text - as part of a single, unified context. If a hidden instruction is embedded anywhere in that context, the model may treat it as a command. Context isolation breaks this assumption. It creates distinct channels: one for user instructions and another for external content. The model is explicitly told which channel contains commands and which contains data to analyze. This prevents hidden instructions from masquerading as user intent.

A second benefit is reducing the interpretive ambiguity that attackers exploit. When a model receives a long block of mixed content, it must infer which parts are instructions and which parts are material to be processed. Invisible prompt injection thrives in this ambiguity. By isolating context, systems can enforce strict boundaries: the model knows that only the instruction channel contains actionable directives. Everything else is treated as inert data. This reduces the model’s susceptibility to manipulation by eliminating the grey zone where hidden instructions can hide.

Another crucial aspect of context isolation is the ability to apply different safety and filtering rules to different channels. User instructions may require semantic interpretation, while external content may require sanitization, normalization, or structural analysis. When everything is blended together, these safeguards become difficult to apply consistently. Context isolation allows systems to treat each channel according to its risk profile. For example, external content can be aggressively sanitized without affecting the clarity of the user’s instructions. This layered approach strengthens the system’s overall resilience.

Context isolation also supports retrieval‑anchored workflows, where the model is grounded in external sources rather than raw text. When a user asks the AI to summarize a document, the system can isolate the document as data and the user’s request as instruction. The model is then guided to treat the document only as material to analyze, not as a source of commands. This prevents hidden instructions inside the document from influencing the model’s behavior. The model becomes a controlled interpreter rather than a passive consumer of whatever text it receives.

A further advantage is the ability to enforce structural templates. When instructions and data are isolated, the system can wrap them in predictable formats. For example, the instruction channel might always be framed as a fixed schema, while the data channel is inserted into a predefined slot. This prevents attackers from injecting new instruction boundaries or manipulating the structure of the prompt. The model sees a consistent, controlled layout every time, making it harder for malicious content to alter the execution flow.

Finally, context isolation is powerful because it scales with complexity. As AI systems are integrated into workflows involving multiple documents, tools, and data sources, the risk of invisible prompt injection grows. Context isolation provides a generalizable framework: no matter how many inputs the system receives, each one is placed in a controlled, well‑defined role. This architectural discipline prevents the chaos that attackers rely on.

Invisible prompt injection is a structural vulnerability, but context isolation offers a structural solution. By separating instructions from data, reducing ambiguity, enforcing boundaries, and grounding the model’s reasoning, context isolation transforms the AI from a vulnerable interpreter into a resilient, predictable partner.

Disclaimer: The whole text was generated by Copilot (under Windows 11) at the first attempt. This is just an experiment to evaluate feature's ability to answer standard general questions, independently on whether they are correctly or incorrectly posed. Moreover, the answers may reflect hallucinations and other types of inconsistent or incorrect reasoning.

Previous Post <<||>> Next Post

11 May 2026

🤖〽️Prompt Engineering: Copilot Unabridged (Part 185: How Input Sanitization Can Be Used to Counter AI Invisible Prompt Injection)

Prompt Engineering Series
Prompt Engineering Series

Prompt: "write a post of 600 words on how input sanitization can be used AI invisible prompt injection"

Introduction

Invisible prompt injection is one of the most subtle and disruptive vulnerabilities in modern AI systems. It exploits the fact that large language models treat nearly all incoming text as potentially meaningful instructions. When hidden commands are embedded inside documents, images, or metadata, the model may follow them without the user ever noticing. This creates a dangerous gap between what the user thinks they are asking and what the AI is actually responding to. Among the available defenses, input sanitization stands out as one of the most practical and foundational. It does not solve the problem entirely, but it dramatically reduces the attack surface by filtering, normalizing, and constraining the content that reaches the model’s interpretive layer.

The first way input sanitization helps is by removing hidden characters and invisible control sequences. Many prompt injection attacks rely on zero‑width characters, Unicode tricks, or formatting markers that humans cannot see but the model interprets as part of the prompt. These characters can smuggle instructions into otherwise harmless text. Sanitization routines that strip or normalize these characters prevent the model from reading them as meaningful input. This is similar to how web applications sanitize user input to prevent hidden SQL commands from being executed. By reducing the 'invisible' portion of the input, sanitization makes it harder for attackers to hide instructions in plain sight.

A second benefit comes from filtering out hidden markup and metadata. Invisible prompt injection often hides inside HTML comments, alt‑text, EXIF metadata, or other fields that users rarely inspect. When an AI system ingests a webpage, document, or image, it may treat these hidden fields as part of the prompt. Sanitization can remove or neutralize these elements before they reach the model. For example, stripping HTML tags, flattening markup, or removing metadata ensures that only the visible, user‑intended content is passed to the AI. This prevents attackers from embedding instructions in places that humans cannot easily detect.

Another important role of sanitization is normalizing the structure of the input. Many prompt injection attacks rely on breaking the expected structure of the prompt - introducing unexpected delimiters, injecting new instruction blocks, or manipulating formatting to confuse the model. Sanitization can enforce a consistent structure by collapsing whitespace, removing unusual delimiters, or reformatting the input into a predictable template. This reduces the model’s exposure to structural manipulation and makes it harder for attackers to smuggle in new instruction boundaries.

Input sanitization also supports context isolation, a broader architectural strategy. By sanitizing external content before it is combined with user instructions, systems can ensure that only the user’s explicit prompt influences the model’s behavior. For example, if a user uploads a document for summarization, sanitization can remove any embedded instructions before the document is passed to the model. This prevents the document from overriding the user’s intent. Sanitization becomes a gatekeeper that separates trusted instructions from untrusted content.

A further advantage is reducing ambiguity, which is often exploited in invisible prompt injection. When input is messy, inconsistent, or contains mixed signals, the model may latch onto the wrong part of the text. Sanitization that clarifies formatting, removes noise, and enforces consistency helps the model focus on the intended content rather than on accidental or malicious artifacts. Cleaner input leads to more predictable behavior.

Finally, input sanitization is valuable because it is scalable and proactive. It does not require detecting every possible attack pattern. Instead, it reduces the overall complexity of the input space, making it harder for attackers to exploit obscure or unexpected pathways. While sanitization cannot eliminate invisible prompt injection entirely, it forms a crucial first line of defense - one that strengthens other safeguards such as retrieval grounding, context isolation, and self‑critique mechanisms.

Invisible prompt injection is a structural challenge, but input sanitization offers a practical, effective way to reduce its impact. By filtering, normalizing, and constraining the content that reaches AI systems, we can build more resilient models that remain aligned with user intent - even when confronted with hidden manipulation.

Disclaimer: The whole text was generated by Copilot (under Windows 11) at the first attempt. This is just an experiment to evaluate feature's ability to answer standard general questions, independently on whether they are correctly or incorrectly posed. Moreover, the answers may reflect hallucinations and other types of inconsistent or incorrect reasoning.

Previous Post <<||>> Next Post

09 May 2026

🤖〽️Prompt Engineering: Copilot Unabridged (Part 183: The Impact of AI Invisible Prompt Injection)

Prompt Engineering Serie
Prompt Engineering Serie


Prompt: "write a post of 600 words on the impact of AI invisible prompt injection"

Introduction

Invisible prompt injection is emerging as one of the most consequential vulnerabilities in modern AI systems. Unlike traditional cybersecurity threats, which target code, networks, or infrastructure, invisible prompt injection targets the interpretive layer of large language models - the part that decides what the system should pay attention to and how it should respond. Because these attacks operate beneath the surface of normal interaction, their impact is both subtle and far‑reaching. Understanding this impact is essential for anyone building, deploying, or relying on AI systems.

The first major impact is the erosion of user control. When hidden instructions are embedded in text, images, or metadata, the AI may follow those instructions instead of the user’s explicit request. This creates a dangerous inversion of agency. The user believes they are in control, but the model is being quietly steered by an unseen actor. In practical terms, this means an AI assistant could ignore a user’s question, alter its tone, or provide misleading information - all without the user realizing why. The loss of control is not just technical; it undermines trust in the entire interaction.

A second impact is the corruption of outputs, which can occur without any visible sign of manipulation. Invisible prompt injection can cause an AI system to hallucinate, fabricate citations, or generate biased or harmful content. Because the injected instructions are hidden, the resulting output appears to be the model’s natural response. This makes the attack difficult to detect and even harder to attribute. In environments where accuracy matters - healthcare, legal analysis, scientific research - the consequences can be severe. A single hidden instruction can distort an entire chain of reasoning.

Another significant impact is the exploitation of contextual blind spots. AI systems treat all input as potentially meaningful context. They do not inherently distinguish between user intent and hidden instructions. Attackers can exploit this by embedding malicious prompts in places users rarely inspect: alt‑text, HTML comments, zero‑width characters, or even the metadata of uploaded files. Because the AI reads these hidden elements but the user does not, the attacker gains asymmetric influence. This asymmetry is what makes invisible prompt injection so powerful: the attacker sees the whole picture, while the user sees only the surface.

Invisible prompt injection also has a profound impact on the reliability of AI‑mediated workflows. As AI becomes integrated into business processes - summarizing documents, drafting emails, generating reports - hidden instructions can quietly alter outcomes. A malicious prompt embedded in a shared document could cause an AI system to misclassify data, rewrite content, or leak sensitive information. These failures are not obvious bugs; they are subtle distortions that propagate through automated pipelines. The more organizations rely on AI for routine tasks, the more vulnerable they become to these invisible manipulations.

A further impact is the amplification of social engineering risks. Traditional phishing relies on deceiving humans. Invisible prompt injection extends this deception to machines. An attacker can craft content that appears harmless to a human reader but contains hidden instructions that cause the AI to behave in ways that benefit the attacker. This creates a new hybrid threat: social engineering that targets both the human and the AI simultaneously. As AI systems increasingly mediate communication, this dual‑layer manipulation becomes a powerful tool for misinformation, fraud, and influence operations.

Finally, invisible prompt injection impacts the broader trust ecosystem surrounding AI. Trust in AI depends on predictability, transparency, and alignment with user intent. Invisible prompt injection undermines all three. It exposes the fragility of systems that rely on natural language as both input and instruction. It reveals how easily AI can be manipulated without detection. And it highlights the need for new forms of input sanitization, context isolation, and architectural safeguards.

Invisible prompt injection is not just a technical curiosity. It is a structural vulnerability that reshapes how we think about AI safety, reliability, and trust. Recognizing its impact is the first step toward building systems that are resilient, transparent, and aligned with the people who rely on them.

Disclaimer: The whole text was generated by Copilot (under Windows 11) at the first attempt. This is just an experiment to evaluate feature's ability to answer standard general questions, independently on whether they are correctly or incorrectly posed. Moreover, the answers may reflect hallucinations and other types of inconsistent or incorrect reasoning.

Previous Post <<||>> Next Post

15 August 2010

Data Security: SQL Injection I - Introduction

Introduction

If you work in IT, most probably you’ve heard already about SQL Injection. It might be a good idea to ask your colleagues and eventually your IT manager if your company has any policies related to it. If you are working for a software vendor or a consultancy company then SQL Injection countermeasure techniques might be quite well positioned in the list of best practices in what concerns the development of Web/Desktop Applications, Web Services or database-related logic adopted by your company. If you are working for a company, other than the two mentioned above, and have various software projects on the role or already in house, then most probably you’ll have to ask if the software vendors you are working with have took into consideration the SQL Injection threats and proved their solutions against them. On contrary, if you have nothing to do with IT at all, it might still be a good idea to ask your IT department if they have anything in place related to SQL Injection – Security Policy, security best practices, etc.

Definition

Wikipedia defines SQL Injection as 'a code injection technique that exploits a security vulnerability occurring in the database layer of an application' [3], the code injection being defined as 'the exploitation of a computer bug that is caused by processing invalid data' [4] (see further definitions) . For a programmer the definition is acceptable, though for other type of professionals it might not be so clear what’s about, especially when they are not familiar with IT terminology. I find more clear the definition provided by J. Clarke et. al, who in his book SQL Injection Attacks and Defense, defines SQL Injection as 'the vulnerability that results when you give an attacker the ability to influence the Structured Query Language (SQL) queries that an application passes to a back-end database' [2]. I will slightly modify the last definition and say that the SQL injection is a security vulnerability residing in the possibility to alter the intended behavior of the SQL Queries passed to the database.

Some Background

At the beginning of our century, with the increase importance of Web Applications whose availability over WAN/Internet (networks) brought new security issues, the SQL Injection became a really hot topic given the damages such techniques could do to an application, with just a few tricks the “hacker” having the possibility to enter in the application and even in the machine hosting the database used, entering thus in the possession of sensitive information, and above all having the possibility of damaging the database. J. Clarke et. al remarks that the first connection between web applications and SQL injection is widely accredited to Rain Forest Puppy, who in an article titled “NT Web Technology Vulnerabilities” (see “ODBC and MS SQL server 6.5” section) written in 1998 for Phrack, an e-zine written by and for hackers [2], was describing the behavior specific to SQL Injection in relation to MS SQL Server 6.5. 
 
I remember when my boss break us the news that we have to protect urgently our applications against SQL Injection, having to redesign some of the database objects and components to protect our applications against such techniques. I was then in my first or second year of professional experience, so the topic was new and quite intriguing not only for myself but also for my colleagues, some of them having a few more years of experience that I did and, I hope I’m not mistaking, none (or few) of them actually have heard about it. It was interesting to check how simple techniques could do so much damage. At those times there were few articles on SQL Injection and specific countermeasure techniques, not to mention best practices, so we were kind of groping in the dark in finding a countermeasure to the problem.

State of Art

Since then, the number of search engines hits on the topic is quite impressive, many professionals approaching the problem in their way, Vendors started to design their solutions accordingly and make aware programmers on best practices in order to minimize this type of security threat, books were written on this topic, the awareness increased between developers and other type of IT professionals. Even if considerable effort has been made into this direction, and the topic appears often on the blogs, there are still many web sites not designed to address SQL Injection concerns. In 2007, The WhiteHat Security, placed SQL Injection on 5th position in top of vulnerabilities, estimating that 1 out of 5 web sites is vulnerable to SQL Injection [1]. In 8th Web Security Report based on 2009 data provided by WhiteHat Security[5], and as it seems also in 9th report [6], SQL Injection remains on the same position, what’s interesting to remark is the split per scripting technology provided in [6]: 
 
SQL Injection - Statistics WhiteHat

In Web Hacking Incident Database maintained by Web Application Security Consortium, SQL Injection is considered as 17.97 % out of the total 512 reported top attack methods. Even if the number of reported attacks is insignificant in report to the number of sites available on the web, the percentage of cases seems to be in agreement with the number provided in WhiteHat Security reports.

Resources

If the topic made you curious, you could find out more with just a simple search on the Web. There are many professionals who wrote on this topic, however it’s a good idea to start directly with the resources provided by the RDBMS vendors, for example Microsoft through its Security Research & Defense blog, in SQL Injection Attacks post has an interesting list of resources on this topic. A nice document on ‘How to write SQL injection proof PL/SQL’ comes from Oracle, an interesting presentation on ‘SQL Injection Myths and Fallacies’ was made at MySQL Conference & Expo, etc.

References:
[1] WhiteHat Security. [2007]. Website Security Statistics Report. [Online] Available from: http://www.whitehatsec.com/home/assets/WPStatsreport_100107.pdf (Accessed: 15 August 2010)
[2] J.Clarke et. al (2009). SQL Injection Attacks and Defense. Elsevier. ISBN: 978-1-59749-424-3
[3] Wikipedia. (2010). SQL Injection. [Online] Available from: http://en.wikipedia.org/wiki/Sql_injection (Accessed: 15 August 2010)
[4] Wikipedia. (2010). Code Injection. [Online] Available from: http://en.wikipedia.org/wiki/Code_injection (Accessed: 15 August 2010)
[5] WhiteHat Security. [2009]. Website Security Statistic Report, 8th Ed. [Online] Available from: http://www.whitehatsec.com/home/assets/WPstats_fall09_8th.pdf (Accessed: 15 August 2010)
[6] WhiteHat Security. [2010]. Website Security Statistic Report, 9th Ed. [Online] Available from: http://www.slideshare.net/jeremiahgrossman/whitehat-security-9th-website-security-statistics-report-3995771 (Accessed: 15 August 2010)

14 March 2010

💎SQL Reloaded: Saving Data With Stored Procedures

Without going too much into details on the benefits of stored procedures, in this post I will show a simple technique I usually like to use for database updates. It’s not rocket science, I bet many of the developers use similar approaches, and many more don’t! Stored procedures are the ideal place for storing your business logic, including insert/updates, in addition stored procedures being recommended as a way of avoiding SQL injection and facilitate code’s maintenance. Doing inserts/updates in a stored procedure implies sending each attribute that needs to be maintain, and given the fact that, with small exceptions, the same parameters are sent, it makes sense to bring the two statements in the same stored procedure. The difference in the number of parameters resides actually in the Primary Key which is sent in the update case in order to identify uniquely the record that needs to be updated, though this fact could be used to determine whether is dealt with an insert or an update.

So, let’s consider the below table in which a unique constrain is defined on ProductNumber attribute: 
 
CREATE TABLE Production.Products( 
ProductID int IDENTITY(1,1) NOT NULL, 
Name nvarchar(250) NOT NULL, 
ProductNumber nvarchar(25) NOT NULL, 
MakeFlag bit NOT NULL, 
FinishedGoodsFlag bit NOT NULL, 
Color nvarchar(15) NULL, 
SafetyStockLevel int NULL, 
ListPrice decimal(13,2) NOT NULL, 
StandardCost decimal(13,2) NOT NULL, 
CreationDate smalldatetime NOT NULL, 
CreatedBy int NOT NULL, 
ModifiedDate smalldatetime NOT NULL, 
ModifiedBy int NOT NULL) 

The table is based on Production.Product table from AdventureWorks database, however I reduced the number of attributes and simplified table’s definition. I introduced also the Creation/Modified Dates used as timestamps for the creation/modification of a record, and the Created/Modified By as a reference to the User that performed the respective actions. From all these 4 attributes is enough to send only the User ID, the actual timestamp being obtained using the GetDate function in the background, so we’ll have in the end 13-3= 10 parameters. 

Here is the stored procedure: 
 
CREATE PROCEDURE dbo.pUpdateProducts( 
  @ProductID int 
, @Name nvarchar(250) 
, @ProductNumber nvarchar(25) 
, @MakeFlag bit 
, @FinishedGoodsFlag bit 
, @Color nvarchar(15) 
, @SafetyStockLevel int 
, @ListPrice decimal(13,2) 
, @StandardCost decimal(13,2) 
, @StartDate smalldatetime 
, @EndDate smalldatetime 
, @UserID int) 
AS 
BEGIN 
BEGIN TRY 
    IF ISNULL(@ProductID, 0) = 0 
    BEGIN 
        -- insert statement 
        INSERT INTO Production.Products (Name, ProductNumber, MakeFlag, FinishedGoodsFlag, Color , SafetyStockLevel 
, ListPrice, StandardCost, StartDate, EndDate, CreationDate, CreatedBy, ModifiedDate, ModifiedBy) 
        VALUES ( @Name, @ProductNumber, IsNull(@MakeFlag, 0), IsNull(@FinishedGoodsFlag, 0), @Color , @SafetyStockLevel 
, @ListPrice, @StandardCost, @StartDate, @EndDate, GETDATE(), @UserID, GETDATE(), @UserID)
SET @ProductID = @@IDENTITY END ELSE BEGIN -- update statement UPDATE Production.Products SET Name = @Name , ProductNumber = @ProductNumber , MakeFlag = IsNull(@MakeFlag, 0) , FinishedGoodsFlag = IsNull(@FinishedGoodsFlag, 0) , Color = @Color , SafetyStockLevel = @SafetyStockLevel , ListPrice = @ListPrice , StandardCost = @StandardCost , StartDate = @StartDate , EndDate = @EndDate , ModifiedDate = GETDATE() , ModifiedBy = @UserID WHERE ProductID = @ProductID END
SELECT @ProductID END TRY BEGIN CATCH SELECT Cast(ERROR_NUMBER() as varchar(10)) + ':' + ERROR_MESSAGE() END CATCH END

The TRY… CATCH… block introduced with SQL Server 2005 allows trapping the errors and thus returns error details to the calling application. In case the DML statement succeeded is returned the Primary Key of the just updated, respectively inserted record. Let’s see the stored procedure in action: 

--inserting new records    
EXEC dbo.pUpdateProducts 0, 'Product 1', 'Product1', 1, 1, 'Blue', 100, 50.32 , 44.06, '2010-01-01', NULL, 1 -- Example 1 
EXEC dbo.pUpdateProducts 0, 'Product 2', 'Product2', 0, 1, 'Red', 100, 51.32 , 44.45, '2010-01-01', NULL, 1 -- Example 2 
EXEC dbo.pUpdateProducts 0, 'Product 3', 'Product3', 1, 0, 'Gren', 100, 59.32 , 44.87, '2010-01-01', NULL, 1 -- Example 3 
EXEC dbo.pUpdateProducts 0, 'Product 4', 'Product4', 0, 0, 'Red', 100, 57.32 , 44.78, '2010-01-01', NULL, 1 -- Example 4 

After running the above EXEC statements try to run them again, each of them will error out with the following error details:
2601:Cannot insert duplicate key row in object 'Production.Products' with unique index 'IX_Products_UniqueProductNumber'.
It will be in developer’s attribution to translate the error to a more appealing error message.

In order to update a record all is needed to do is to transmit the ID of the record to be updated and the new values. For example in a new created table the record created by first example will more likely correspond to ProductID = 1, thus the following statement is used to change its values: 
 
-- updating a record 
EXEC dbo.pUpdateProducts 1, 'Product 1', 'Product1', 1, 1, 'White', 100, 50.32 , 44.06, '2010-01-01', NULL, 1 -- Example 1 

Note:
This technique has the inconvenience that it doesn’t consider the cases in which two different Users update the same record, though that’s a topic for another post.

18 February 2010

💎SQL Reloaded: Just in CASE V (Dynamic Queries)

Stored procedures allow not only to encapsulate logic, enhance queries’ performance by cashing query plans or reduce the risks of injection by using parameters, they allow also altering the structure of a query at runtime, modifying for example the query based on the provided parameters. Such behavior is ideal when needed to base the logic of a query on a list of filters (filter controls) in which some of the attributes are not mandatory. A dynamic created query can be executed using the EXEC function or the sp_executesql stored procedure, the later allowing parameters’ substitution, ensuring thus parameters’ data types, another benefit residing in the fact that the execution plans are more likely to be reused by SQL Server, Microsoft recommending thus the use of sp_executesql over Exec [1]. Now it depends also on how much the queries vary between requests and how easy is to handle the missing parameters or parameter’s substitution – sometimes quite a difficult topic for a beginner. Therefore for exemplifying the techniques I will use Exec, and here is the example:
 
-- Retrieving Products 
CREATE PROCEDURE dbo.pSearchProducts( 
@ProductID int , 
@ProductSubcategoryID int , 
@ProductModelIDs varchar(500) , 
@ProductName nvarchar(50) , 
@ProductNumber nvarchar(25) , 
@MakeFlag bit , 
@FinishedGoodsFlag bit , 
@StartStandardCost money , 
@EndStandardCost money , 
@StartSellStartDate varchar(10) , 
@EndSellStartDate varchar(10) , 
@OrderBy nvarchar(500) ) 
AS 
BEGIN 
DECLARE @Query varchar(2000) 
-- translating Order By clause 
IF LEN(@OrderBy)&gt;0 
BEGIN 
    SET @OrderBy = ',' + @OrderBy  
    SET @OrderBy = Replace(@OrderBy, ',0', ', ProductNumber') 
    SET @OrderBy = Replace(@OrderBy, ',1', ', ProductName') 
    SET @OrderBy = Replace(@OrderBy, ',2', ', ProductModel') 
    SET @OrderBy = Replace(@OrderBy, ',3', ', SellStartDate') 
    SET @OrderBy = Replace(@OrderBy, ',4', ', StandardCost') 
    SET @OrderBy = Right(@OrderBy, Len(@OrderBy)-1) 
END 
-- building the query 
SET @Query = 'SELECT ProductID, ProductName, ProductNumber, ProductModel, CatalogDescription, ProductSubcategory' 
+ ', MakeFlag, FinishedGoodsFlag, Color, SafetyStockLevel, ReorderPoint, StandardCost, ListPrice' 
+ ', Size, SizeUnitMeasureCode, SizeUnitMeasure, WeightUnitMeasureCode, WeightUnitMeasure, Weight' 
+ ', DaysToManufacture, ProductLine, Class, Style, dbo.GetDateAsString(SellStartDate) SellStartDate' 
+ ', dbo.GetDateAsString(SellEndDate) SellEndDate, dbo.GetDateAsString(ModifiedDate) ModifiedDate' 
+ ' FROM Production.vProducts' 
+ ' WHERE 0=0' 
+ CASE IsNull(@ProductID, 0) 
     WHEN 0 THEN '' 
    ELSE ' AND ProductID = ' + cast(@ProductID as varchar(10)) 
END 
+ CASE IsNull(@ProductSubcategoryID, 0) 
     WHEN 0 THEN '' 
     ELSE ' AND ProductSubcategoryID = ' + cast(@ProductSubcategoryID as varchar(10)) 
END 
+ CASE IsNull(@ProductModelIDs, '') 
    WHEN '' THEN '' 
    ELSE ' AND ProductModelID IN (' + @ProductModelIDs + ')' 
END  
+ CASE IsNull(@ProductName, '') 
    WHEN '' THEN '' 
    ELSE ' AND ProductName LIKE ''' + @ProductName + '''' 
END  
+ CASE IsNull(@ProductNumber, '') 
    WHEN '' THEN '' 
    ELSE ' AND ProductNumber LIKE ''' + @ProductNumber + '''' 
END  
+ CASE IsNull(@MakeFlag, 0) 
    WHEN 0 THEN '' 
    ELSE ' AND MakeFlag = 1' 
END  
+ CASE IsNull(@FinishedGoodsFlag, 0) 
    WHEN 0 THEN '' 
    ELSE ' AND FinishedGoodsFlag = 1' 
END  
+ CASE IsNull(@StartStandardCost, 0) 
    WHEN 0 THEN '' 
    ELSE ' AND StandardCost &gt;= ' + cast(@StartStandardCost as varchar(20)) 
END  
+ CASE IsNull(@EndStandardCost, 0) 
    WHEN 0 THEN '' 
    ELSE ' AND StandardCost &lt;= ' + cast(@EndStandardCost as varchar(20)) 
END 
+ CASE IsNull(@StartSellStartDate, '') 
    WHEN '' THEN '' 
    ELSE ' AND DateDiff(d, SellStartDate, dbo.GetStringDate(''' + @StartSellStartDate + '''))&lt;=0' 
END  
+ CASE IsNull(@EndSellStartDate, '') 
     WHEN '' THEN '' 
     ELSE 'AND DateDiff(d, SellStartDate, dbo.GetStringDate(''' + @EndSellStartDate + '''))&gt;=0' 
END  
+ ' ORDER BY ' 
+ CASE LEN(@OrderBy) 
    WHEN 0 THEN ' ProductNumber' -- default sorting 
     ELSE @OrderBy -- chosen sorting 
END 
-- executing the query 
EXEC (@Query)  
END 

The above stored procedure could split in 5 logical section:
 
1. The definition holding stored procedure’s name and parameters
Parameters’ selection depends on the requirements, usually the natural and database key for the main entity or for master data (e.g. Customer, Vendor, Item Number, etc.), the LOV-based attributes used most often as selection criteria (e.g. Statuses, Characteristics), flags (e.g. Make Flag, Finished Goods Flag) that have important meaning for the business, dates used to partition the result set (e.g. Creation Date, Document Date, GL Date) being in general needed to provide the lower and upper bound of the interval. For special type of analysis it’s useful to include also a set of controls for specifying the numeric intervals, like in this case for Standard Cost.
   In general the parameters have the same date type as the attribute they represent, with a few exceptions:
dates – because the different date formats used in the various environments (e.g. client vs. web server vs. database server) I prefer to send the dates as string, thus assuring that the date will be always in the expected format.
multi-choice attributes – in case is needed to handle multi-choice selections then the easiest way to handle this is to provide a list of concatenated values as a string, as in @ProductModelIDs’ case.
flags – sometimes I prefer to use characters instead of boolean (0,1) values because it offers more flexibility in handling triple or multi-state situations.
 
2. The declaration of internal variables, here the @Query holding the dynamic built query, the variable needs to be big enough to hold the whole concatenated string with maximum list of parameters and ORDER BY attributes.
 
3. The translation of ORDER BY attributes here stored in @OrderBy input parameter, which contains a list with numbers from 0 to 9 that will dictate the attributes used in ORDER BY clause and their order. For example will be sent ‘3,1’ to sort the result set based on SellStartDate and ProductName.
 
4. The actual concatenation of the dynamic query, the handling for NULL values being handled with a set of CASE functions, existing one CASE for each parameter, including @OrderBy.
 
5. Query’s execution, here done using Exec.

Notes:  
Usually I’m including a 6th section which includes several metadata about the current object (e.g. who created it and when, when was last time modified, objects that call current object, the purpose of the object, etc.).
It’s needed to test such a procedure for all parameters – either by providing all parameters as in the first below example, or creating a test case for each parameter, the later approach being much safer because when multiple parameters are provided, one of them could hide an existing issue.

EXEC dbo.pSearchProducts 973, 2, '25,26,27', 'Road%', 'BK%', 1, 1, 1000, 1200, '01/01/2003', '31/12/2003', '' -- all parameters 
EXEC dbo.pSearchProducts 0, 0, '', '', '', 0, 0, 0, 0, '', '', '' -- all records 
EXEC dbo.pSearchProducts 1, 0, '', '', '', 0, 0, 0, 0, '', '', '' -- ProductID test 
EXEC dbo.pSearchProducts 0, 2, '', '', '', 0, 0, 0, 0, '', '', '' -- ProductSubcategoryID test 
EXEC dbo.pSearchProducts 0, 0, '3, 4, 5', '', '', 0, 0, 0, 0, '', '', '2,1' -- ProductModelIDs test 
EXEC dbo.pSearchProducts 0, 0, '', 'Mountain%', '', 0, 0, 0, 0, '', '', '1' -- ProductName test 
EXEC dbo.pSearchProducts 0, 0, '', '', 'BK-T18U%', 0, 0, 0, 0, '', '', '2' -- ProductNumber test 
EXEC dbo.pSearchProducts 0, 0, '', '', '', 1, 1, 0, 0, '', '', '' -- MakeFlag &amp; FinishedGoodsFlag test 
EXEC dbo.pSearchProducts 0, 0, '', '', '', 0, 0, 50, 110, '', '', '' -- StandardCost test 
EXEC dbo.pSearchProducts 0, 0, '', '', '', 0, 0, 0, 0, '01/01/2001', '31/12/2002', '3,1' -- SellStartDate test 

Is it really needed to create a dynamic query in order to handle the various filtering scenarios? In theory yes, there are several techniques that could be used to address a special type of parameters:
 
1. Single values excepting  and dates

In such cases the CASE could be replaced with the IsNull function test on parameter, if the parameter is null then take the value of the attribute against which the test is performed. For example the following CASE:
 
CASE IsNull(@ProductID, 0) 
    WHEN 0 THEN '' 
     ELSE ' AND ProductID = ' + cast(@ProductID as varchar(10)) 
END 
 
could be replaced with the following constraint:
 
ProductID = IsNull(@ProductID, ProductID) 
 
A similar technique could be used also for interval-based attributes like the StandardCost from our example, the two above cases could be replaced with the following constraint:
 
StandardCost BETWEEN ISNULL(@StartStandardCost, StandardCost) AND ISNULL(@EndStandardCost, StandardCost) 
 
 Note:
In case the attributes contain also NULL values then the Null value need to be replaced with a special value, typically not found in the list of values the respective attribute could take, for example –1 for foreign keys, an incredibly small number for numeric data types, single or empty space for string data types.

2. List of values
 
The list of values are a little more difficult to handle but not impossible, for this scenario could be used the dbo.SplitList function introduced in a previous post. For example the CASE for @ProductModelIDs could be rewritten as follows:
 
IsNull (ProductModelID, -1) IN (SELECT value FROM dbo.SplitList(IsNull(NullIf(@ProductModelIDs, ''), CAST(IsNull(ProductModelID, -1) as varchar(20)) ), ',')) 

The IsNull(ProductModelID, -1) expression has been introduced in order to handle the Null values, while the expression for the first parameters of dbo.SplitList was written in this way in order to handle the cases in which the @ProductModelIDs was not provided or it contains no values.
 
   Unfortunately this technique even if it solves the problem, is not quite efficient because the dbo.SplitList function is called for each record, fact that impacts query’s performance considerably. On the other side if the @ProductModelIDs would be mandatory than the dbo.SplitList could be joined with the main query, fact that increases query’s performance, though it must be paid attention to the duplicates, because each duplicate found in the list provided to dbo.SplitList function could lead to duplicates in the final result!

3. Dates

Because the Date functions can’t be used errorless with the BETWEEN operator in SQL Server, the DateDiff function being preferred for such case, the handing of Date intervals could be achieved by providing the lower/upper bound of the interval date types used, for example ‘0001-01-01’, respectively ‘9999-12-31’ for date data type, or ‘1900-01-01’, respectively ‘2079-06-06’ for smalldatetime data type. or ‘1753-01-01’, respectively ‘9999-12-31’ for datetime data type.  Because the dbo.GetDateAsString function returns a datetime I will use the lower/uppers bounds of the respective data type in case the date values are null, thus the two CASEs for SellStartDate could be rewritten as:

DateDiff (d, SellStartDate, dbo.GetStringDate(IsNull(@StartSellStartDate, '01-01-1753')))&lt;=0 
AND DateDiff(d, SellStartDate, dbo.GetStringDate(IsNull(@EndSellStartDate, '31-12-2009')))&gt;=0     

In case is needed to return also the dates having Null value, then the SellStatDate could be replaced with any of the two bounds of the data type used, thus the above constraint becomes:

DateDiff (d, IsNull(SellStartDate, '1753-01-01'), dbo.GetStringDate(IsNull(@StartSellStartDate, '01-01-1753')))&lt;=0 
AND DateDiff(d, IsNull(SellStartDate, '1753-01-01'), dbo.GetStringDate(IsNull(@EndSellStartDate, '31-12-2009')))&gt;=0 

Unfortunately, I haven’t found any easy way to handle the different attribute combinations that could appear in the ORDER BY clause. On the other side there are controls that handle the sorting on the client, therefore a default ORDER BY could be provided, and if the Users need other sorting, then they could sort the data by themselves. Here’s an alterative writing for the above stored procedure without using a dynamic query, though this doesn’t mean that the performance of this approach is better than the one of the first stored procedure given the fact that a constraint is evaluated for each parameter, the unnecessary constraints for list of values and dates coming with a considerable decrease in performance.

  -- Retrieving Products w/o dynamic query 
CREATE PROCEDURE dbo.pSearchProductsND( 
@ProductID int , @ProductSubcategoryID int , @ProductModelIDs varchar(500) , @ProductName nvarchar(50) , @ProductNumber nvarchar(25) , @MakeFlag bit , @FinishedGoodsFlag bit , @StartStandardCost money , @EndStandardCost money , @StartSellStartDate varchar(10) , @EndSellStartDate varchar(10)) 
AS 
BEGIN -- the query SELECT ProductID, ProductName, ProductNumber, ProductModel, CatalogDescription, ProductSubcategory 
,MakeFlag, FinishedGoodsFlag, Color, SafetyStockLevel, ReorderPoint, StandardCost, ListPrice 
,Size, SizeUnitMeasureCode, SizeUnitMeasure, WeightUnitMeasureCode, WeightUnitMeasure, Weight 
,DaysToManufacture, ProductLine, Class, Style, dbo.GetDateAsString(SellStartDate) SellStartDate 
,dbo.GetDateAsString(SellEndDate) SellEndDate, dbo.GetDateAsString(ModifiedDate) ModifiedDate 
FROM Production.vProducts 
WHERE IsNull(ProductID, -1) = COALESCE(NullIf(@ProductID, 0), ProductID, -1) 
AND IsNull(ProductSubcategoryID, -1) = COALESCE(NullIf(@ProductSubcategoryID, 0), ProductSubcategoryID, -1) 
AND IsNull(ProductModelID, -1) IN (SELECT value FROM dbo.SplitList(IsNull(NullIf(@ProductModelIDs, ''), CAST(IsNull(ProductModelID, -1) as varchar(20)) ), ','))  
AND IsNull(ProductName, ' ') LIKE IsNull(NullIf(@ProductName, ''), IsNull(ProductName, ' '))  
AND IsNull(ProductNumber, ' ') LIKE IsNull(NullIf(@ProductNumber, ''), IsNull(ProductNumber, ' '))  
AND IsNull(MakeFlag, 0) = COALESCE(NullIf(@MakeFlag, 0), MakeFlag, 0)  
AND IsNull(FinishedGoodsFlag, 0) = COALESCE(NullIf(@FinishedGoodsFlag, 0), FinishedGoodsFlag, 0) 
AND StandardCost BETWEEN ISNULL(NullIf(@StartStandardCost, 0), StandardCost) AND ISNULL(NullIf(@EndStandardCost, 0), StandardCost) 
AND DateDiff(d, IsNull(SellStartDate, '1753-01-01'), dbo.GetStringDate(IsNull(NullIf(@StartSellStartDate, ''), '01-01-1753')))&lt;=0 
AND DateDiff(d, IsNull(SellStartDate, '1753-01-01'), dbo.GetStringDate(IsNull(NullIf(@EndSellStartDate, ''), '31-12-2009')))&gt;=0 
ORDER BY ProductNumber END 
      
Given the format in which parameters are sent (0 for numeric values, empty string for string values) instead of using Nulls, I had to transform the parameters to Null in case they take the value 0 or empty string, this being the only difference between the constraints used in the query and the example constraints from above. In this way the stored procedures should return the same number of records for the same test cases, here are the test cases for the second stored procedure:

EXEC dbo.pSearchProductsND 973, 2, '25,26,27', 'Road%', 'BK%', 1, 1, 1000, 1200, '01/01/2003', '31/12/2003' -- all parameters 
EXEC dbo.pSearchProductsND 0, 0, '', '', '', 0, 0, 0, 0, '', '' -- all records 
EXEC dbo.pSearchProductsND 1, 0, '', '', '', 0, 0, 0, 0, '', '' -- ProductID test 
EXEC dbo.pSearchProductsND 0, 2, '', '', '', 0, 0, 0, 0, '', '' -- ProductSubcategoryID test 
EXEC dbo.pSearchProductsND 0, 0, '3, 4, 5', '', '', 0, 0, 0, 0, '', '' -- ProductModelIDs test 
EXEC dbo.pSearchProductsND 0, 0, '', 'Mountain%', '', 0, 0, 0, 0, '', '' -- ProductName test 
EXEC dbo.pSearchProductsND 0, 0, '', '', 'BK-T18U%', 0, 0, 0, 0, '', '' -- ProductNumber test 
EXEC dbo.pSearchProductsND 0, 0, '', '', '', 1, 1, 0, 0, '', '' -- MakeFlag &amp; FinishedGoodsFlag test 
EXEC dbo.pSearchProductsND 0, 0, '', '', '', 0, 0, 50, 110, '', '' -- StandardCost test 
EXEC dbo.pSearchProductsND 0, 0, '', '', '', 0, 0, 0, 0, '01/01/2001', '31/12/2002' -- SellStartDate test 

Now it depends which method you will want to use, frankly I like the dynamic query approach because it offers more flexibility in dealing with parameters and in handling other types of requirements. In addition, if it happens that you use the second approach, the request could change and you might need to redesign your stored procedure using the first approach.

References:
[1] MSDN (2009) Using sp_executesql
Related Posts Plugin for WordPress, Blogger...

About Me

My photo
Koeln, NRW, Germany
IT Professional with more than 25 years experience in IT in the area of full life-cycle of Web/Desktop/Database Applications Development, Software Engineering, Consultancy, Data Management, Data Quality, Data Migrations, Reporting, ERP implementations & support, Team/Project/IT Management, etc.